3 ms·
The reason that a byte array is in reality layed out as a (mostly empty) long array in Java, is actually for performance. Computers tend to have their memory al
by misja111 1y ago
The reason that a byte array is in reality layed out as a (mostly empty) long array in Java, is actually for performance.
Computers tend to have their memory aligned at 8 byte intervals and accessing such an address is faster than accessing an address that's at an offset of an 8 byte interval.
Of course it depends on your use case, in some cases a compact byte array performs better anyway, for instance because now you're able to fit it in your CPU cache.
- john-h-k 1y agoBut you can load any byte by loading 8 bytes and shift (v cheap)
- ackfoobar 1y ago> a byte array is in reality layed out as a (mostly empty) long array in Java Are you saying each byte takes up a word? That is the case in the `char array` in OCaml, but not Java's `byte[]`. AFAIK The size of a byte array is rounded up to words. Byte arrays of length 1-8 all have the same size in a 64-bit machine, then length 7-16 take up one more word. https://shipilev.net/jvm/objects-inside-out/ https://shipilev.net/jvm/objects-inside-out/