Introduction

Back in 2014 Java developers heard an announcement that was on its course to change the history of software development, or so we believe.

Project Valhalla is an experimental OpenJDK project led by Brian Goetz. The first prototype was released on 05th July 2019. It was aimed at bringing prominent new programming language features such as Generic Specialization, Enhanced Volatiles and possibly Reified Generics to Java 10 and its future versions. Brian Goetz and his team of top developers wanted to facilitate Java coders in discovering and applying advanced Java Virtual Machine (JVM) features in their programs. This way, developers will be able to create and utilize value types, now known as inline classes or non-reference values.

In this announcement, Goetz shared that in a period of five years, his team of elite software engineers had developed five different prototypes of Project Valhalla. Today, it seems they are on a formal path to enhance the Java Programming Language and virtual machines with value types by providing a compatible path for migration of the existing value-based classes to inline classes and the current generic classes to specialized generics.

The Motivation Behind Project Valhalla

In one of his interviews, Brian Goetz mentioned that one of the primary motivations behind  Project Valhalla was to fulfil the vision of adapting the Java Language and runtime to modern hardware. It has been more than 25 years now since Java was introduced and there has been a dramatic change on both aspects since then. Java has been termed a ‘pointer-heavy language’ by software engineers as most of Java’s data structures are objects or reference types.

The issue with this is that in Java, the identity of an object points to pointers and then pointers lead to indirections. These come with performance drawbacks. As a result of all this, identifying a better method to resolve this became inevitable.

Project Valhalla aims to introduce value types to discard the type overhead both in memory as well as in computation. Goetz said that his team wants to facilitate developers by giving them the control to match data layouts with the performance model of current hardware. This will provide an easier path towards cache-efficient and memory-efficient data layouts without sacrificing the abstraction, performance or even compromising with type safety.

Inline Classes

This feature is the evolution of what was previously known as “value types.” Research and development (R&D) for this feature is still a work in progress. It is one of the primary work streams within Project Valhalla. It aims to help top developers to improve the compatibility of Java programs with modern hardware. This can be achieved by going to the very fundamental part of Java, that is the amendments in the model of Java’s data values.

Since its earlier versions, Java has had only two types of values: Primitive types and object references. This model is relatively simple and easy to understand for every Java developer. It doesn’t matter whether the developer is a beginner or a professional. But, as mentioned before, this model has performance issues. For instance, while dealing with arrays of objects, it involves unavoidable indirections which result in processor cache misses.

Many top software developers who want control over performance prefer the ability to work with data that utilizes memory more efficiently. The improved layout proposed under Project Valhalla will result in fewer indirections. These will cause fewer cache misses along, resulting in higher performance.

The following are some of the sub-features offered by inline classes that are interconnected and complement each other:

A Class Without An Identity:

Inline classes are very similar to current classes. They can have properties, methods, constructors, and so on. But the unique difference that Project Valhalla brings is that instances of inline classes or inline objects do not have an identity. This is the only thing that can distinguish them from other objects. This is why operations like synchronization are not possible with inline objects as they are identity- sensitive. But inline classes can solve the cache missing problem.

Data Aggregation Within A Class:

Inline classes allow Java coders to represent pure data aggregates. We can get rid of the features of regular objects.  The result? We can have pure data, without identity. Although we will have to give up the features we could implement using object identity, we can give up on pointers and change the general memory layout of value types, as we do not have object identity in this case.

Here is an example of the memory layout of the class “class01” and the corresponding inline class “class01”. 

Following is the code snippet showing memory layout of a regular “class01” class:

final class class01 {

final int a;

final int b;}

 And now, this code snippet shows the memory layout of an inline “class01” class:

value class class01 {

int a;

int b;}

It also enables Java Virtual Machines to pass value types on the stack instead of having to allocate them on the heap. Ultimately, this means that we will be getting data aggregates that have the same runtime behavior as Java Primitives, such as int or float.

Specialized Generics

Currently, if a Java developer wishes to generify over language primitives, he will have to use a boxed type, such as Integer for int or Float for float. This boxing creates an extra layer of indirection, which will significantly affect the performance, defeating the purpose of using primitives for performance enhancement.

To prevent this, there are several dedicated specializations made by top software engineers in Java frameworks and libraries. Their sole purpose is to retain the enhanced performance achieved via primitives.

The specialized generics feature is an effort to eliminate the need to use third-party libraries or frameworks to maintain performance levels. Instead, Java is aiming to let Java coders use generic types for almost everything like object references, primitives, and value types without the cost of performance.

Conclusion

We have discussed the new features that Project Valhalla will bring to the Java Programming Language. Two of the main things the project can deliver are enhanced performance and less leaky abstractions.

Soon a stable version will be released in the upcoming Java version.

See Also: How Does Project Amber Affect The Java Community?

The introduction of initiatives like Project Valhalla shows the ‘Kaizen’ or continuous improvement mindset of the Java community. It’s no surprise that the elite talent working behind projects like Project Valhalla and others are the ones to bring improvements and new features across the Java platform.

Author

Shaharyar Lalani is a developer with a strong interest in business analysis, project management, and UX design. He writes and teaches extensively on themes current in the world of web and app development, especially in Java technology.

Write A Comment