Whether calling functions that return specialized Optional types allow due to increased gas pressure when used in the hot code mode.
It depends on the. In general, Optional types are just regular objects, and therefore heaps are allocated. But under certain circumstances, JIT compilers can decompose them into their fields (i.e. the value they store), and push them onto the stack.
This happens if a leak analysis can determine that the object is not globally on the heap.
It also seems that there are additional experimental optimizations hidden behind the flags ( -XX:+AggressiveUnboxing , part -XX:+AggressiveOpts ). As far as I understand, they can violate the guarantee of identity for Integer.valueOf(int) and, therefore, do not fully comply with the specification.
Tangent: The Graal experimental / research compiler even goes one step further and partial deletion analysis , which can delay allocation to those code paths where the objects run, leaving them scalarly expanded on the stack when they don’t. This is not yet part of regular virtual machines and is mainly used by non-java languages running on the JVM, such as JRuby and Nashorn.
Since all of these things are runtime optimizations, there is no guarantee that this will happen. Thus, you will have to measure effects or track compiler decisions using diagnostic flags.
The reason I don't just assume that they will perform heap allocation, like any other class, is because Optional et al. Are "value-based classes"
I think that at the moment these properties are simply designed to ensure that objects are immutable and thread safe. I don’t know if any optimization outside EA uses interchangeability.
I suspect that the specification language is mainly intended to provide direct compatibility with the proper type values when they eventually come to Java as part of the valhalla project .
Following these rules also makes escape / stack analysis easier.