Value type alignment in array ?

John Rose john.r.rose at oracle.com
Mon Jul 2 07:13:14 UTC 2018


On Jul 1, 2018, at 11:54 PM, Remi Forax <forax at univ-mlv.fr> wrote:
> 
> Let say i've a 64 bits VM with compressed oops,
> as far as i know, a heap allocate instance of a class will be aligned to 64bits because it can contain a long or a double that need to be 64 bits aligned,
> now let suppose i declare a value type with no long or double, if the value type is allocated in the heap it will be aligned to 64 bits but if the value type is a cell of an array, in that case will it be 32 bits aligned ?

As a matter of quality, the flattened array should be as dense as possible,
but the JVM doesn't actually specify how much padding it inserts around
array elements.

I expect we will eventually mimic the way C handles structs:  The C struct
size includes per-struct padding and therefore determines array element
padding.  That size is computed as a multiple of the most demanding
alignment requirement within the struct.  Thus, if you have a struct of
three bytes, your arrays will have a strict of three.  But if you have a
struct of one byte and a short, your array will have a stride of four.

Value type arrays should do something close to this, while balancing
index arithmetic speed and cache friendliness, as needed.  But they
don't need to, in the short term.  And, someone besides me can say
what they really do, but I suspect there is a round-up to a power of
two bytes at present.  See ValueArrayKlass::array_layout_helper.
That's optimizing for indexing speed, at the expense of density.
So both of the examples above will live in four bytes, even though
the first struct could live in three.

— John



More information about the valhalla-dev mailing list