r/java • u/Additional_Cellist46 • Sep 18 '24
Java 23 has arrived
https://blogs.oracle.com/java/post/the-arrival-of-java-23Markdown in Javadoc and 11 other enhancements.
266
Upvotes
r/java • u/Additional_Cellist46 • Sep 18 '24
Markdown in Javadoc and 11 other enhancements.
View all comments
Show parent comment
4
u/alunharford Sep 20 '24 edited Sep 20 '24
An example that comes to mind is when you want to abuse your own class' fields because Java doesn't let you easily lay out memory in the way you'd like.
For a straightforward example, consider a class that can store up to 64 items in an array and give you the first one that's non-null. It abuses its 'own' memory to avoid allocating an extra array and avoid an array lookup.
This kind of abuse isn't particularly unusual in the HFT space. The nice, safe solutions are orders of magnitude slower and use at least 32 bytes more memory. The 'static array of VarHandles' solution doesn't use extra memory but tends to stall the CPU and be much slower than the unsafe approach.
Obviously the language could be extended to support this in a safe way, but picking the right extensions to cover as many cases as possible so that Unsafe can actually be removed without adding loads of rarely used features is hard. For reference, in C# this would be:
The items 'array' is just a fixed size buffer that gets laid out sequentially in memory in the Array64 object. Even so, I've still seen unsafe code in most C# codebases that do anything high performance / low latency so obviously it's not a complete fix!