Array fields in value objects

Archie Cobbs archie.cobbs at gmail.com
Tue May 20 13:52:11 UTC 2025


On Mon, May 19, 2025 at 11:28 PM John Bossons <jbossons at gmail.com> wrote:

> This works and can be flattened, but is rather ugly and laborious. So
> maybe I'm asking the question the wrong way. Maybe what I should be asking
> for is a subsequent JEP that (using *record* as a model) gets the
> compiler to generate detailed code such as the above from a specification
> like
>
> *    fixedSmallArray* PostCodeCA { byte[6] asciiBytes }     // immutable
> array thanks to encapsulation
>

I had the same thought, but if this is your goal I see the implication
differently: this is now "just" an optimization opportunity. You could
write a bytecode transformation that looks for array fields that are
private, final, non-escaping, never modified, and have a length known at
compile time and automatically "inlines" them (surely such a thing already
exists?). Or, the JVM could be enhanced to do this at runtime. Either way,
there is no need any more for a JEP or language change.

-Archie

-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-dev/attachments/20250520/211251e6/attachment-0001.htm>


More information about the valhalla-dev mailing list