JEP 502 introduces the StableValue api (previously known as ComputableConstant). The api can be used to create constants with delayed (at-most-once) initialisation. The unique selling point of this api is that the JIT will trust the values for constant folding.
At a glance, this api seems like the perfect compilation target for Scala’s lazy vals. I can imagine two benefits over the current compilation strategy:
Simpler generated code; the whole ‘checking whether a lazy value is already initialised’ logic is delegated to the StableValue api.
Lazy immutable data structures may see some performance improvements in hot paths.
Having said that; the StableValue api is still in preview, and officially it doesn’t even target JDK 25 yet. Is there a precedent of the Scala compiler using preview apis, or is this out of the question (even for performance benchmarks)?
I’m afraid it’s basically out of the question. We can’t even use stable features of recent JDKs, as we have to support older JDKs.
It may be interesting to evaluate what performance we could, theoretically, gain. It’s possible for someone to alter the codegen in a branch, and do some measurements with that. However, it is very unlikely that we will merge such a branch, because it will be effectively dead code for the next 5 to 10 years.