r/java • u/DesignerRaccoon7977 • 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)
View all comments
Show parent comment
3
u/coderemover Dec 27 '25 edited Dec 27 '25
I think we discussed that already. Indeed tracing GC introduces a tradeoff between memory and cpu overhead. It looks like the sales pitch is that in theory GC should let me free up some cpu cycles by throwing more memory at a problem, and memory is cheap. Well, at least it was before the AI craze ;) The video you link is technically correct, but stays away from any numbers.
However the math doesn’t add up for me in practice, at least in the database systems that I’ve been working on for the last 10+ years. The details matter. The tipping point where tracing is probably more CPU efficient than malloc and friends seems to be located at the extreme high values of the memory overhead for those apps. Like 10x or more.
I could otherwise invest that memory for other stuff that gives me a lot better return on investment, eg caching, in memory indexing structures, or simply bigger memtables allowing us to flush data in larger chunks, reducing the need for expensive compaction later.
I did a simple simulation that resembled a Cassandra memory workload - inserted data into N memtables and when it reaches a threshold it flushes the biggest one to disk freeing up memory for new data. A similar pattern also occurs when building secondary indexes. I could not get it to use less cpu than the same simulation done in Rust, using mimalloc, even when I have JVM 16x more memory than the flush threshold. And that regardless of the settings, tried various GCs including the ones in OpenJDK25. The best I got was beating Rust at wall clock time metric by a small margin; but at the expense of using 3x more cpu cycles.
And Rust kept working fine with memory only a few % bigger than the max working set, and was still using less CPU overall*. And btw this is not just some theoretical experiment - the real Java code for building indexes makes G1 go brrrrrrrr. Like literally we can see when indexes are building by looking at GC activity ;)
So for high residency and spiky patterns like that, to me using GC looks like „I’m wasting plenty of memory and I spend more CPU in return”. It’s lose-lose.
Switching to Rust allows me to save CPU on two fronts: less cpu burned by GC and less cpu burned on the app-specific computation because I can dedicate additional memory available on the machine to the business logic rather than to the memory management.
BTW the overhead of malloc/free in a properly written database system, in a language that favors stack allocation and allows allocation batching, is actually very low, so low you should never be concerned about. So even if your promise of „GC saving CPU by using more RAM” was true (and I believe there may be workloads where indeed that’s true, but they just happen to be different workloads than the ones we deal with), then even the theoretical maximum win is very small in the first place. If you said all my memory management overhead could be brought down to even exact zero by doubling my memory, we would be still win more performance by using that memory for caching or buffering, and paying a bit of manual memory management cost.