r/java • • 21d 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.

41 Upvotes

18 comments sorted by

View all comments

Show parent comment

0

u/TheStrangeDarkOne 21d 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 21d 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 21d 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 21d ago edited 21d 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.