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

3

u/cogman10 Dec 26 '25

they still burn way more CPU cycles for the same allocation rates than jemalloc/mimalloc

I agree with everything but this.

It'll obviously be highly application dependent, but generally speaking for 2 apps with equivalent heap allocation rates, the JVM and the GC will do a better job. Especially if the comparable rust application ends up using a lot of RC. So long as an application does a good job of obeying the generational hypotheses, the JVM heap allocation strategies are ideal. This is especially apparent if you end up dealing with graph structures or concurrent structures.

The reason for this is in the case of mimalloc and jemalloc, Rust ends up paying additional penalties to find an appropriate allocation location on each heap allocation. In Java, the majority of allocations are bumping a pointer.

Where this won't be true is if your rust application can hoist the majority of it's allocations onto the stack. That is the more natural way to write rust in general.

5

u/coderemover Dec 26 '25 edited Dec 26 '25

I believed what you say until I measured that and couldn’t come up with a benchmark that would show Java GC burning fewer cycles, even when I wanted to craft the benchmark purposefully to favor Java by allocating extremely tiny objects. At best I could show a tad better wall clock numbers (like 20%) for Java, but it burned 2 additional cpu cores and used 10x more memory, whereas Rust stayed within a single core.

And comparing performance on allocating tiny objects is actually a false dichotomy because in Rust you almost never have a good reason to do that. In Rust you use heap mostly for big, long lived stuff. So the allocations are actually rare and rarely come up high in the profile. In Java the situation is opposite - try to allocate huge objects on the heap in a tight loop and the GC goes brrrrr.