SSerguey Asael Shinder
Java coding notes: the JVM, and writing software that lasts

Serguey Asael Shinder: A ticker in one int: Inside Java builds a Valhalla value class and measures a 1.47x array scan

· by Serguey Asael Shinder / Serguey Shinder

On 9 November Inside Java published A Valhalla Ticker in One int by Per-Ake Minborg of the Java team at Oracle. It is a small, complete example of what value classes from JEP 401 look like in practice, and it is unusually careful about what the performance numbers do and do not show.

The class represents a stock ticker symbol such as ORCL or GOOGL. It is declared public value class Ticker, so it has no identity, and its only field is a private final int packed. The alphabet is 37 characters: the period, the ten digits and the 26 uppercase letters. Six bits are enough for 37 codes, so five characters fit into 30 of the int's 32 bits. Code zero is reserved for padding, which is how toString() finds the end of a shorter symbol without a separate length field. The article's own example: new Ticker("ORCL") stores 0x1A74E5C0, which I checked by hand from the codes it lists, 26, 29, 14 and 23.

The encoding has a neat property. The alphabet is ordered the way String.compareTo orders these characters, the first character sits in the highest slot, and shorter symbols are padded with zeros. Because the top two bits are always zero, every packed value is non-negative, so Integer.compare(packed, other.packed) gives the same order as comparing the strings. Sorting tickers never has to decode them. The class also implements CharSequence, so length() and charAt() read the slots directly, and a StringBuilder can append a ticker through append(CharSequence) without calling toString() first.

Serguey Asael Shinder: A ticker in one int: Inside Java builds a Valhalla value class and measures a 1.47x array scan
A ticker in one int: Inside Java builds a Valhalla value class and measures a 1.47x array scan — Serguey Asael Shinder

The performance section is the part I would show to anyone expecting magic. At the language level a Ticker is still a reference. In hot code compiled by C2 it may be scalarized, so its int travels through locals, arguments and returns without an allocation, but the article calls this an optimisation, not a guaranteed calling convention, and notes that calls through CharSequence or Comparable can hide the concrete type. A Ticker[] can get a flat layout, yet the elements can still be null, so four bytes of data do not mean four bytes per element. A CharSequence[] generally cannot be flattened at all.

The benchmark is narrow by design. On an Apple M4 Pro with an internal JDK 28 build, scanning a value-class array took a median 0.261 ns per element against 0.385 ns for an otherwise identical identity class, a median speedup of 1.47x across five JVMs, with a separate run giving 1.40x. It measures traversal of large concrete arrays only, not construction, decoding or toString().

To try it you need --enable-preview --release 28 for javac and --enable-preview for java. The article also notes a limit for later: in JDK 28, value objects with 64 bits or more of field data cannot normally be flattened in nullable fields or arrays.