Value Types & List<int> coming for Java 10 ?

Oracle, with lead engineer Brian Goetz, have launched an experimental OpenJDK project to bring long-awaited features to the Java platform.

Major enhancements to generics & new ‘value types’ are planned. Highlights include:

  • Value Types;  highly-efficient small ‘objects’ without inheritance.
  • Reified Generics;  retaining their actual type at runtime.
  • Generic Specialization;  List<int> would be valid & highly efficient.
  • ‘volatile’ enhancements.

The effort is named Project Valhalla. Here’s a preview:

List<int> — Generic Specialization

Valhalla looks to extend Java generics support beyond Object types to include primitives and value types.

While datatypes such as Integer and Long are currently supported, these ‘boxed’ datatypes require extra allocation & memory overhead.

A particular concern for high-performance processing is the pointer overhead of accessing ‘boxed’ values, which almost always causes a cache miss. This reduces fast loops on int[] to an endless series of cache misses.

Generic specialization would look to resolve this, and further extend Java’s popularity for high-performance & financial processing.

Specialization would need to be implemented at both the compiler and JVM levels. Generic types would have bytecode marked for possible specialization, and ‘reified type’ information would enable the JVM to “specialize” that bytecode.

According to early proposals, the JVM might instantiate an ArrayList<int> by automatically converting the bytecode of ArrayList<T extends Object>. Bytecodes & method signatures with type T would be replaced with their ‘int’ equivalents.

One interesting wrinkle in this proposal, is that List<int> would no longer descend from List<Object>. List<int> would not even descend from it’s raw type, List!

This is due to Java having no ‘Any’ type, able to unify both objects & primitives. As currently proposed, there is no common ancestor.. This is bound to be a subject of discussion.

Value Types

These have long been of interest to enable Java functions to return multiple (tuple) results, and for scientific & graphics calculation efficiency.

Value Types would be memory-efficient, comparable with a C/C++ ‘struct’, and likely to take no more memory than the fields or primitives they are composed of.

Synchronization and inheritance features would be excluded for Value Types. These would no longer have object identity, or the memory & pointer overheads associated.

Removing java.lang.Object’s VMT & synchronization structures will simplify memory layout, eliminating the necessity of an allocation & pointer dereference for each instance.

The proposal suggests Value Types will also able to assume a ‘boxed’ representation,  compatible with Object & passed by reference. This would give interoperability.

Cursors and Iterators are mentioned as a possible applications of Value Types, but one technical hurdle will be to resolve how these can be compatible with the existing java.util.Iterator interface.

Biggest gains from Value Types are expected to be arrays and generic collections, which will be able to be use arrays of the new value types.

When do I get these new features?!!

Project Valhalla is still experimental. Generics, in particular, pose many challenges. None of this will be in Java 9.

This also ties in with people such as Stephen Colebourne (Joda Time)’s push for #BIjava — backwards incompatible Java, to ditch legacy features & move forward.

Generic specialization presents significant change. Method declaration & language enhancements will be needed to solve tricky problems, such as distinguishing  List.remove(int index) from List.remove(Object item).

As the project progresses, classfile formats will change & libraries need to be recompiled.

Essentially, this is a long-term technical effort. Project Valhalla is underway.. we can probably expect results near Java 10.

Are you excited about Project Valhalla? Post your thoughts now.

Read more:

2 thoughts on “Value Types & List<int> coming for Java 10 ?”

    1. While Java seems more popular , and has much richer open-source ecosystems & innovation around it, it’s true that C# is a good language & has some great features.

      LINQ is my favorite, but ‘typed generics’ are a good technical feature. However, Java’s implementation of ‘typed generics’ will be able to benefit by learning from some of C#’s mistakes in this area; eg. static fields.

      Enjoy the platform the work with & don’t feel the need to cast shade. Maybe even consider opening your horizons?

Leave a Reply to Tom Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>