On Tue, 1 Sep 2026 04:34:43 GMT, Johan Sjölen <[email protected]> wrote:

>> src/hotspot/share/oops/layoutKind.hpp line 161:
>> 
>>> 159: 
>>> 160:   // Returns default value if missing
>>> 161:   int size_in_bytes_of(LayoutKind lk, int default_value = -1) const {
>> 
>> Why is there a default_value argument? Looks dangerous to return a value 
>> when no value is available. An error should be detected instead.
>
> Often, we are not concerned with the exact size, but simply wants the largest 
> size available among a few layouts. Then, we can do something like what we do 
> here: 
> https://github.com/openjdk/jdk/pull/32276/changes#diff-bdfdbe1685717bf0c72c691043ced9bbe148364cbe7bf055857c72eb07f4ad62L1306
> 
> The reason that I went for having a `default_value` is that I think it's more 
> dangerous to depend on the exact choice of sentinel value.
> 
> We might want to introduce a helper `largest_layout_of(LK::A, ..., LK::N)`, 
> but I'd really like to hold off on that for a future RFE.

Flat layout sizes have to be exact, polymorphism is not supported by flattening.
The notion of "largest size available among a few layouts" is a red flat. 
Either the exact kind of the layout is known, and there's only one size 
associated with it, or the layout kind is not known and no size should be 
returned.
Returning a size bigger than the exact size for a flat value is taking the risk 
to overwrite another field or another array element next to the flat value.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/32276#discussion_r3918308110

Reply via email to