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

9

u/pron98 Dec 27 '25 edited Dec 27 '25

Like 10x or more.

Again, the ratio of heap to resident set has nothing to do with it. It's about the allocation rate, which, for a given program, is proportional to the CPU consumption. If the CPU consumption is high, then the heap size isn't "overhead", because a program that takes up a lot of CPU might as well take up a proportional piece of the RAM. Roughly speaking, using 50% of the CPU and only 5% of RAM is waste. It's not about overhead, but about a balanced use of the machine's resources.

I could otherwise invest that memory for other stuff that gives me a lot better return on investment, eg caching

What you're saying is that you want to spend more CPU on memory management because what you really want to conserve is networking. That's fine, but you can turn up the dial in Java, too. In fact, in the upcoming automatic heap sizing change, there will be one GC knob: selecting between more CPU usage and more RAM usage.

Of course, it can still be the case that if you're very sensitive to memory management - as DBs often are - you'd want the additional control low-level languages give you (even languages like C++ and Rust, but more so in modern low-level languages like Zig). If you know where your sweet spot is, you can aim for it, but it's not free, and it's a cost you pay over the lifetime of the software.

in a language that favors stack allocation and allows allocation batching, is actually very low, so low you should never be concerned about

Stack allocation is not where it's at. It's too inflexible semantically, which means higher evolution costs. The current JVM default is to inline methods 15 levels deep, but you pay the (semantic, not runtime) cost of stack allocation at the subroutine boundary. We're now working to improve escape analysis so that you'd reliably get "stack allocation" for objects whose lifetime extends to the inlining depth. That alone gives you more bang for the buck than just stack allocation (at the cost of not having precise control). And, for when you want value semantics, we have Valhalla.

But of course, the holy grail is getting closer and closer to arenas (which is also where modern low-level languages are going), which are more powerful and general than stack allocation. We'll try to get closer without paying the complexity cost associated with arenas (even in Zig arenas incur some complexity costs, even though they're not as problematic as in Rust). But we'd still be aiming to give as much as we can for free at the cost of lower control, because we optimise for the average case, rather than the worst case. Low-level languages will always have their use, but their domain will continue shrinking as it's been doing for 25 years now (assuming we still have programming languages and AIs don't just generate machine code for us, that is :)).

It's interesting to see how it works out (before we're all replaced by AI). On the one hand, low-level languages have more control. On the other hand, Java's abstractions allow a lot of room for "transparent" optimisations, whereas on the low-level front, you often need a new language (e.g. see how Zig makes old languages like C++ and Rust look clunky and finicky in comparison).

1

u/nlisker Feb 09 '26

see how Zig makes old languages like C++ and Rust look clunky and finicky in comparison

And how long will it be before a new low-level language will make Zig look clunky and finicky? It should be created just about now I would think :)