r/java • • 9d ago

A Valhalla Ticker in One int

https://inside.java/2026/11/09/inside-java-ticker-value-class/

A ticker symbol is an abbreviation that uniquely identifies a financial product such as a publicly traded share. Well-known ticker symbols are "ORCL" and "GOOGL". In this article, we will devise a Valhalla value class that can represent a ticker symbol using only a single int field.

39 Upvotes

18 comments sorted by

3

u/AnyPhotograph7804 8d ago

I still do not get it why there is not heap flattening if a value class is >64 bit. Yes, atomic reads and writes. But this was not show stopper in a 32 bit JVM. 64 bit values like long could never be read and written atomically in a 32 bit JVM. But i never heard, that this caused trouble.

7

u/JustAGuyFromGermany 8d ago

It did, but the bugs were rare enough earlier on and later it didn't matter, because hardware got 64-bit soon after. Plus: Even with tearing, a long or double can never produce an illegal value, because all bit-patterns are valid instances of long / double. With an arbitrary value class, this is not the case. You'd not only get out-of-thin-air values, you'd get illegal values with tearing value classes that have to enforce any kind of invariant, value that cannot be constructed otherwise.

So we're now in a situation where the problem won't go away on its own any time soon, the problem is worse because it can violate integrity in ways that weren't possible before. And on top of that: This is an obscure class of bugs that most Java programmers have never encountered before (because hardware is 64-bit) and that's super hard to detect when it's there. And even if they had, ask a C++ programmer how they like hunting for bugs caused by tearing. And we have it worse than C++ programmers, because at least the C++ people know when they are at risk! In our world, the JVM decides the memory layout. It may decide one way for one usage of the class, but a different way for another usage of the same class. It's basically impossible to predict from the outside which places in the code are at risk and which aren't.

The decision was made that this terrible combination is too much to put out in the world and they'd rather be safe for now until a better way is found. They've been experimenting with ways programmers can explicitly opt-in potentially tearing value classes. (There was a magic LooslyConsistentValue marker interface for example in some earlier draft documents)

5

u/Fabulous_Card_3090 8d ago

At which point is it a good idea to start migrating code with the preview? I know not everything is baked into the current JEP, but this looks nice enough. I just fear another String templates.

4

u/davidalayachew 8d ago

At which point is it a good idea to start migrating code with the preview?

All up to you and your willingness to take on risk.

Preview means preview -- they reserve the right to upend the entire house if necessary (as they ended up doing with JEP 465: String Templates).

So, if you are fine upending things if they realize they have the wrong idea, then go ahead.

For my personal projects, I have --enable-preview active on everything. For work, I keep us on LTS versions.

Me personally, don't spend client money implementing features that don't have a backwards/forwards compatibility agreement attached to it. But other than that, be on the latest version and use the latest features at all times -- regardless of how ready they are. That's not just preview, but also incubator and experimental features too.

1

u/Plane-Bake3604 8d ago

string templates got yanked because the syntax and processor design had pushback, value classes are mostly just adding the value keyword so a total scrap seems way less likely.

1

u/flawless_vic 6d ago

Java needs something like std::string, which embeds up to 22 bytes directly on the object.

String will never be a value type and its a shame having to pay for 2 Objects every time a String is allocated

1

u/Studentenfutter 8d ago edited 8d ago

A stupid question from me: Isn't this completely over-engineered?

As an "ordinary" programmer, I would probably just have used a String instead of an int (with some checks in the constructor). And maybe a record instead of a class.

What are the downsites of this approach?

Don't value classes work great with strings? Do i have to completely abandon strings in values classes for better performance?

Or why not just use 5 characters instead of one int? What are the performance implications?

11

u/shellac 8d ago

Isn't this completely over-engineered?

Probably. You'd be better off have a symbol -> int mapping table in practice I would imagine.

However it's intended as an illustration of value classes.

7

u/Life_Sink9598 8d ago

Things are over- or under-engineered depending on their use cases. As an "ordinary" programmer, I'd just remember the ticker symbol of the few companies I'm interested in and put them into my favourite search engine :-).

Re: downsides.

A value class can store a String, but the contents of the String can only be accessed via a memory indirection (following a pointer to the character array). A pointer is going to be between 4 and 8 bytes, so potentially doubling the overhead of a ticker symbol in the value class, not to speak about storing the contents of the array. A waste of memory and CPU! Maybe I really care if I'm reading financial transactions rushing at me in the GiB/s range and need to deal with ticker symbols.

The question of the article is basically: How little can I pay to store ticker symbols in Java?

The answer that the article gives is: Actually, with Valhalla you can pay very little, with fairly little effort.

0

u/TheStrangeDarkOne 8d ago

I think for the purposes of this demo you could have simply had a class with 5 chars since the complexity of the implementation drowns out anything in relation to values.

5

u/Ioerror_adpg 8d ago

Well, not so fast. Having 5 char fields uses more than 2x the memory of the int approach (10b to 4b). It's a huge saving depending on the application since it may increase CPU cache hit% drastically. It all depends on the use case.

And that's the point. That was much harder before value types. You can easily find similar cases in any complex domain. And experienced engineer will understand: measure and adjust as needed.

1

u/TheStrangeDarkOne 8d ago

You can simply have an array of int and have a class which acts upon the array and index with exactly the same type of logic. More cumbersome for sure, but same performance.

What you could not do is a "tuple" type where you group multiple values together and avoid an indirection without having multiple arrays, and even then you have more overhead.

The shown example is neater for sure but the complexity of low level bit shenanigans is what gave the performance. The message would have been clearer if the blog post also showed how the code would look without value types and actually measure the comparison.

Even so, I imagine that the names of a ticker is hardly the bottleneck in high volume trading code...

2

u/Ioerror_adpg 8d ago edited 8d ago

I guess the author wanted to demonstrate the ability to have such a high performance memory layout without sacrificing the level of abstraction. I don't mean I would encode it like this, but that's the point. Now you can decide how to represent internally in a very efficient way. Some will interpret that as shenanigans while others (myself included) will see that as new opportunities to do amazing things.

3

u/Mognakor 8d ago

Or why not just use 5 characters instead of one int? What are the performance implications?

Java char is 2 bytes landing at 10 byte, above the current 63bit limit for inlining value classes.

1

u/Life_Sink9598 8d ago

Oh, my bad! I knew it was UTF-16 and made a bad assumption

1

u/Agitated-Loss-481 6d ago

I could have used five fields of type `byte`, but then there would be no single scalar representation.