←back to thread

83 points hyperbrainer | 3 comments | | HN request time: 0s | source
Show context
rbjorklin ◴[] No.43965319[source]
Does anyone have some hard numbers on the expected performance uplift when using GADTs? Couldn't see any mentioned in the article.
replies(1): >>43965690 #
ackfoobar ◴[] No.43965690[source]
The example here is basically an 8-fold memory saving going from `long[]` from `byte[]` - while still retaining polymorphism (whereas in Java the two are unrelated types).

Hard to say exactly how much performance one would get, as that depends on access patterns.

replies(1): >>43966244 #
1. misja111 ◴[] No.43966244[source]
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.

replies(2): >>43966389 #>>43966582 #
2. john-h-k ◴[] No.43966389[source]
But you can load any byte by loading 8 bytes and shift (v cheap)
3. ackfoobar ◴[] No.43966582[source]
> 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/