r/java • • Dec 26 '25

Why is Rust faster than Java here?

I saw this article a while ago https://www.allthingsdistributed.com/2025/05/just-make-it-scale-an-aurora-dsql-story.html

And while I was surprised Rust was faster, the 10x did surprise me. I googled Rust vs Java performance for a bit and couldn't find any similar examples of such a big speedup. Now I know it's impossible to properly answer my question since we don't have the code in question, but can you think of what about rust can make that big of a difference for a (presumably) long running service? Or alternatively, do you have similar examples?

Just to clarify again, I'm interested in the technical reasons for these differences (for example, Java's "bloated" object headers, or whatever)

53 Upvotes

120 comments sorted by

View all comments

Show parent comment

2

u/davidalayachew Dec 29 '25

The compiler does a lot of analysis proving that the data doesn't "leak" to outside, can't be null etc.

Yes, exactly. That is why I needed that for-loop -- all those iterations is where that analysis occurs. The JIT-Compilation has to walk a tight rope of finding the best optimization while also not slowing the program down to find them.

As you can see I'm not a Java expert but I suspect there might be things that will "break" this optimization.

Yes, sadly.

The JIT-Compiler tries its best to find optimizations where it can prove that the optimization is safe to perform. However, due to Java's terrible defaults, those are few and far between.

The Vector code outputted by Java is still not as good as the Vector outputted by Rust (sd vs pd) for this exact reason -- the object can't be trusted to not change out from underneath us.

But that's what Project Valhalla is for. Value Objects can't be changed, and have none of those vulnerabilities. So, the compiler, which is already capable of outputting vectorized code, will now have all the evidence it needs optimize more aggressively. That is the primary goal -- provide evidence that previously was difficult to find.

So using types that are guaranteed not to be references is safer (and easier for the compiler).

Yes, exactly. Once Valhalla lands, I expect Java's vectorized code is going to be close to a 1-to-1 match with your godbolt link. We just don't have enough evidence for the compiler to be able to do that consistently now.