Dev News Daily ENDE

A Valhalla value class packs a ticker symbol into one int, and a flat array scans it 1.47 times faster

Per-Ake Minborg of the Java team at Oracle published an article on Inside Java on 9 November that builds a small value class for JEP 401, Value Objects, a preview feature in JDK 28. The class, Ticker, stores a stock ticker symbol such as ORCL or GOOGL in a single int field.

The encoding is simple. The allowed characters are the period, the digits and the uppercase letters, 37 symbols, so each needs six bits and five characters fit in 30 bits. Code zero is reserved for padding, which marks the end of shorter symbols without a separate length field. Because the alphabet follows the same order as String.compareTo for those characters, and the two top bits are always zero, comparing two packed ints gives the same result as comparing the strings. compareTo becomes a single Integer.compare. The article also implements CharSequence and shows an arithmetic constructor that replaces the alphabet lookup with range checks.

The performance section is careful about what a value class does and does not promise. At the language level a Ticker is still a reference. In hot code compiled by HotSpot's C2 compiler it may be scalarized, so the int travels through locals, arguments and return values without an object; Minborg calls this an optimisation, not a guaranteed calling convention, and notes that calls through CharSequence or Comparable can hide the concrete type. A concrete Ticker[] array can be laid out flat, but the layout must also represent null, so four bytes of field data do not mean four bytes per element.

To give indicative numbers he compared a minimal value ticker with an identical identity class on an Apple M4 Pro using an internal OpenJDK 28 build. Scanning arrays of eight million packed values took a median of 0.261 nanoseconds per element for the value array and 0.385 for the identity array, a median speed-up of 1.47 times across five JVM runs, with individual runs between 1.44 and 1.52 and a separate run at 1.40. An internal check reported the value array as flat and the identity array as not flat. He states that the figure covers only traversal, not construction, decoding or toString.

Compiling the example requires --enable-preview --release 28 for javac and --enable-preview for java. The article also notes a JDK 28 limit: value objects with 64 bits or more of field data cannot normally be flattened in nullable fields or arrays.

A Valhalla value class packs a ticker symbol into one int, and a flat array scans it 1.47 times faster
A Valhalla value class packs a ticker symbol into one int, and a flat array scans it 1.47 times faster — Dev News Daily

What it means

The article is a useful template for reading Valhalla results. A flat array can make scans markedly faster, but the gain is conditional on the JVM's layout choices and on code that keeps the concrete type visible. Measuring your own workload with JMH, as the author recommends, remains the only reliable answer.

Written by Victoria Shinder.