Do specialized optional types (OptionalInt, OptionalDouble, etc.) have heap allocation execution?

Do calling functions that return specialized Optional types allow for increased gas pressure when used in the hot code mode, as opposed to returning a primitive value indicating absence ( Integer.MIN_VALUE , for example)?

Edit

The reason why I do not just assume that they will perform heap allocation, like any other class, is because Optional and others are “ value-based on classes, ” which apparently means that they can behave differently than traditional classes backstage.

+5
source share
2 answers

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.

+6
source

Yes.

These classes extend Object and, therefore, are reference types. They will occupy a lot of space similar to how you used previously available wrapper classes, for example Integer .

In addition, these types encourage the use of lambda expressions, which themselves create new instances of anonymous types. Depending on which lambda values ​​are “closed”, you may end up with those objects that refer to other objects, which will have some effect on the GC.

Therefore, in those places where you want to avoid instances of class instances, box / unboxing, etc. for performance reasons, you still want to avoid these optional types.

Update

Java 8 introduces the concept of value-based classes as a reliable step-by-step for Java 10 value types.

InfoQ: let's talk a bit about the possibility of Optional as a type of proto-value.

Goetz: There was probably some slight optimism about the idea of ​​porting a Java 8 reference type to a Java 10 value type. Basically, we laid the foundations by pointing out that developers should not rely on certain properties (such as identity checks) that are necessarily true for types that may become value types in the future. Whether this migration is a proven practice in practice remains to be seen.

http://www.infoq.com/articles/Java-9-and-Beyond-Goetz-Rose-Interview

So, as with Java 8, these classes still work like regular classes, but now by creating certain restrictions on them, it is hoped that they can be converted to value types when Java 10 gets this function.

If this happens, I see two main advantages for converting these types to value types:

  • Improved performance, since you do not need to use a lot of space.
  • Value types are never null, so if you have a field of type OptionalDouble and you never initialize it, you still won't get a null pointer exception.

Shameless plugin: I used these attributes of struct types in C # for type Maybe<> in the library I wrote.

+4
source

Source: https://habr.com/ru/post/1234224/


All Articles