For more than 3 years, Project Valhalla is the buzzword in Java community, though even with such kind of anticipation as well as foreboding, surprisingly just little has been published on the significant project. For a few people, it means the ability for creating the value types, and also for others, it means the reified types of generic runtime.

But with all such desire and confusion, Project Valhalla has a specific purpose: For ceasing the requirement which is chosen by Java developers between abstraction and performance. Let us now clear up confusion for Project Valhalla and it also brings to the table. In doing this, we will also examine what is include and what does not get included in this project and also delve into the reasoning which is behind key exclusions and inclusions.

An overview of Project Valhalla

Project Valhalla is the OpenJDK project which was started in the year 2014 and it was headed by Brian Goetz with the sole purpose to introduce the value-based optimizations for the JDK or Java Development Kit 10 or even the release of future Java. This project is mainly focused on allowing the developers to utilize and to create the value types, or even the non-reference values which simply act as they are the primitives.

The Key advantage of value types against the reference types is mainly about the removal of the overhead that is a reference type, both in the memory and also in the computation. For instance, in spite of the fact and the actual size overhead that are associated with the key object is particular to the JVM also known as Java Virtual Machine and the implementation, all the JVMs consist of bytes to store detailed information about object, that includes polymorphism information, synchronization information, identification information and also the garbage collection of the metadata like the reference counters. As an addition, when an object is associated with the reference type should be accessed, reference should also be dereferenced, imparting such level of indirection. On the other hand, in some of the cases, the overhead is redundant, as benefits of an object like identification, synchronization, and polymorphism are not required.

Even though the value types even deviate from the objects in key crucial characteristics–that you can see shortly–as they help to maintain a proper level of abstraction which is akin to the classes. For instance, value types might even have fields and methods, both with the visibility modifiers which facilitate encapsulation. The key differences, in the current release of Java JDK 9 at time of the writing are an inability of the value types known as the primitives for using used as the generic type of arguments; it means that the List<int> does not stay to be valid type in recent Java release.

Even though, the primitive kind such as int may also be autoboxed to the reference types like Integer that also has a key drawback: It also reintroduces overhead of the objects. On the other hand, this has also been an obstacle in the Java generics after the introduction in the JDK 5, it also has become quite prevalent in the Project Valhalla, as the developers will also be permitted to create the new or updated value types. On the other hand, to solve this kind of issue, Valhalla has even tasked with offering mechanism for permitting the value kinds to be supplied being the valid generic arguments, though still maintaining recent type-erased of the general semantics of the Java. The complete solution recently reached by the team is about utilizing the generic specialization that we will also delve for after the much detailed look for the value types.

How does Value Type works?

The Value types usually are groups of the data where immediate value gets stored in the memory, instead of the reference (or even the pointer) to data. Also, the Value types may, hence, be thought about consuming adequate memory for storing the aggregate of data that is contained in their field without any additional overhead. Theoretically, the primitives are also a relatable instance of the value types where just int consumes 32 bits without any additional bytes for storing the metadata.

Taking the data and then placing it directly in its value in the memory (instead of the reference) is known as flattening and their benefits are much acutely demonstrated through the arrays. In such an array of objects, every element in array stores the reference to an object that is mainly associated with such element, needing that dereferences to get performed before accessing the object. On the other hand, an array of the value types, values get placed directly into an array and they are also guaranteed to be in the most contiguous memory.

Use of the value types ahead of the reference kind that also has key benefits, that includes:

– Reducing the usage of memory: Without any additional memory, it is used for storing the object metadata, like flags that are facilitating synchronization, garbage collection and identity. For a small object like Integer, the overhead for the object can also match or also surpass the size of data.

– Reducing the indirection: As the objects are also stored as the reference kind in Java, every single time an object gets accessed should be dereferenced, leading to some additional instructions to get executed. Also, flattened data that is well associated with the value types are instantly present in the location where they are required and hence, it does not require dereferencing.

– Enhanced locality: Value of Flattened objects remove the indirection that also enhances the likelihood that the values are stored adjacently in the memory–particularly for the arrays or different structures of contiguous memory such as classes.

Conclusion:

The most thrilling feature of Inline Classes is a way through which the arrays get created. During the primitive stage, every position has a direct representation of type; for instance, 64 bits long when the array is also long. In the case of the referenced object, the array will consist of a reference to the object that has allocated memory on the heap. Project Valhalla has a particular purpose that is about ceasing the requirement which is chosen by Java developers between abstraction and performance.

Kitty Gupta