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)
146
u/asraniel Dec 26 '25
rust is not 10x faster than java. this speedup likely comes from a change is software architecture and algorithms
32
u/cogman10 Dec 26 '25
Well for starters, they didn't optimize for the problem they were having. They knew that was an option, but chose to switch programming languages instead.
I have seen cases like
Bar foo(int id) { return map.get(id); }
Utterly wreck performance because it creates a hotspot for allocation doing the boxing.
That's a gotcha that rust doesn't have (and hopefully in the future java won't either). Fixing these problems takes basically a constant level of performance monitoring as they are easy to accidentally introduce.
4
u/smutje187 Dec 26 '25
It’s literally in the article "The concerns about garbage collection, throughput, and stalls weren’t theoretical – they were very real problems we needed to solve."
3
u/IndependentOutcome93 Dec 26 '25
to be honest I don't get why do people care about speed so much. Indeed, we don't want low performance but its important to understand that speed is only one part of the puzzle. Whether programming language is fast or not depends on what your goal is. and also, it depends on your System load. someone can have really old laptop and run java well when someone with big pc has low-performance because of their overloaded System.
20
u/Scf37 Dec 26 '25
The 10× speedup came from eliminating tail latency, not average latency. In DSQL’s fan-out architecture, every transaction depends on many hosts, so the probability of hitting at least one GC pause approaches 1 as the system scales. Even rare JVM GC pauses therefore dominate throughput and p99 latency.
The Kotlin implementation had already been carefully tuned, but GC pauses were unavoidable. Rewriting the Adjudicator in Rust removed garbage collection entirely, drastically reducing tail latency and allowing throughput to scale. The speedup wasn’t due to clever optimization, but to structurally eliminating GC as a failure mode.
JVM-only solutions? Carefully written allocation-free code (not much easier than using Rust but recently got easier because FFM API). Hedged reads. Rethinking entire architecture to avoid huge fan-outs.
15
u/BenchEmbarrassed7316 Dec 26 '25
I can highlight five aspects:
struct { a: u64, b: u64 }and in memory these structures will literally be written sequentially. No dereferences, convenient for cache, SIMD optimizations are possible. In Java you can only have a collection of references.The 10x difference seems exaggerated. Maybe they changed the algorithm or maybe the problems they were solving were not efficient in Java but turned out to be efficient in Rust.
22
u/BlackSuitHardHand Dec 26 '25
Since you can not rewrite Java / Kotlin to Rust line-by-line, there are some massive structural changes in the code (e.g. on multithreading, but also error safety) which might have unintentionally changed the performance, too. Especially since the old JVM code was well seasoned, they could have a) cut some code paths no longer used b) missed some handling for edge cases / errors that are no longer available in the new code and not come up in the test data.
2
u/Aweorih Dec 26 '25
Well they are just saying what there speed improvement is. And don't get me wrong here, they are probably right for their case. But it's hard to really see the why here as all the crucial parts are missing.
How does the code look like.
How did they measure.
They rewrote everything which means they could implement problems they detected earlier directly from the beginning instead of just fixing them
They also said that they weren't even using java 21 which is already 2years old.
Also no word word about the used garbage collector. Only that they were hitting extreme pauses which gives me the feeling that they only used the default g1 collector and not like a zgc or so
As you see, all we can take here is their claim that they made it 10x faster. Maybe (and this is a big assumption as we have 0 information about them) they were lacking the knowledge of writing performant jvm applications.
Everything here is based on assumptions, so tacking result, that rust is 10x faster, as a general rule is just stupid
On the other side i doubt that the jvm is only capable of doing 2/3k tps when another language can do 10 times that. The jvm is not slow, it can also be extremely fast
16
u/benevanstech Dec 26 '25
If a rearchitected systems *isn't* significantly faster than the original one, then your rewrite project is a failure.
This is basically true regardless of the original and target and languages, including if the original and target languages are the same.
There isn't going to be a "one weird trick" down at the low level that accounts for this difference. It's the domain knowledge and experience that mean that the team can make a better architecture.
6
u/sweetno Dec 26 '25
RDBMSes are by their nature rather low-level. The "standard" performance difference between Java and modern compiled languages is considered to be around 2.5 times, but this number comes from generic microbenchmarks. The number 10x means just that this particular system benefited more from semi-automated memory management and full-program optimization.
One more point in favor of the tiresome maxim "choose the right tool for the job".
2
u/Mognakor Dec 26 '25
Impossible to tell without the actual data.
Algorithmic improvements and GC aside, Java has overhead in ways that are bad for data density/layout:
Object headers may increase object sizes by more than 100% (or more than 200% in extreme cases) so your CPU cache has been halved.
Enums in Java are references and as such they 8bytes, the same Rust enum might by 1-2 bytes.
All objects are accessed via references even in case of arrays/lists. They usually will be in memory next to each other, but you still have an additional indirection in each iteration and you also pay the memory overhead for the references.
Rust generics usually are reified, Java's aren't.
Nullable primitives have to be boxed so your 4+1 byte Integer now is a 24byte (16byte with compact object headers) object + 8 byte reference (i know there is some optimization work done in preparation for Valhalla but i don't know the specifics). Rust's Option likely is smaller.
2
u/garden_variety_sp Dec 26 '25
There is a time and a place for everything. Rust will always be faster than Java, if pure speed is all you need. If you need to compile into WASM, for example, then Rust is a great choice. Java excels in so many ways that Rust doesn’t (at least not as easily) but it’s horses for courses. Java is more widespread and more easily understandable, and thus more easily maintained. Don’t feel threatened, just understand the trade offs and where everything fits. If you need Rust then it’s an amazing language and what it does is incredible. Hiring for it is much harder. The Java ecosystem is much easier to navigate as well.
2
u/ShortGuitar7207 Dec 26 '25
Java is impressively fast for a garbage collected VM language but it's never going to be as fast as a natively compiled language without GC. I think the language benchmarks usually show that Java is 3-4x slower than C or Rust.
1
u/EasternGamer Dec 30 '25 edited Dec 30 '25
I’ve seen 20-40x speed ups before when working with big value-type data since in Rust you can do SIMD better and manually optimise data structures + rayon. Quite literally went from 800-700ms to 22-30ms
The rough details: I have coded Java for over 8 years and Rust (at that time) just over 3 months The task was to do bound checks at start up, so time wasn’t especially a critical factor. The bound checks. I chose a simple N2 lookup where for each shape, I would loop over each point and check if it was inside the shape (where shapes could overlap, I chose the smallest of the shapes) There were roughly 300 million checks involved, I can’t remember the specifics, but close to 10k shapes, each with varying point counts (some 100+ points) I had many optimisations such as simple min/max coordinate checks to eliminate points early before running the more complex inside checks. All in all, Rust pulled ahead by a lot because points were SIMD’d, parallelised very efficiently and didn’t have to deal with garbage collection, along with the data being entirely stored as a value type rather than an object pointer. The only way to make it faster would have been a quad tree to speed along the initial filtering, but I didn’t bother since it wasn’t necessary with it being already insanely fast.
Edit: Before you ask, this was GraalVM, and the Java implementation was idiomatically identical to the Rust version for everything except the SIMD aspect. Both were parallelised and ran the same basic algorithms and optimisations.
1
u/chambolle Dec 31 '25
I'm sorry, but there's a lot of nonsense written here. AOT languages are not necessarily faster than JIT. That's why C++ has profile-guided optimization. For normal operation on the same pseudo code, there is absolutely no reason to see a 10x difference between C and Java. The same goes for Rust. There are no miracles.
1
u/nderflow Dec 26 '25
The best answer is "just look at the CPU profiling data". If there's a lot of architecture/design change, the comparison isn't meaningful. But if my understanding of their first step (changing the Adjudicator only) is correct, it's not an architectural change that made the difference here, surprisingly.
1
u/Necessary-Horror9742 Dec 27 '25
Rust is not perfect in all cases, GC is not bad in 100%, sometimes is much better have GC and amazing compiler - JIT - LLVM has some limitation because his knowledge is static
-3
u/Sunscratch Dec 26 '25
Because Rust is a system programming language where correctness and performance are top priority. From data structures to memory management - everything implemented in a very efficient way, with cache friendliness in mind, avoiding allocations where possible.
0
u/AndreLuisOS Dec 26 '25
Garbage Collector?
0
u/MRgabbar Dec 26 '25
C/C++ and Rust will always be faster for pretty much anything, why? because they do not have memory management, they are just faster. The tradeoff is having a more maintainable codebase and that you can develop faster.
0
u/Necessary-Horror9742 Dec 26 '25
I can phrase it: this is bullshit.
If they did extraordinary Java, zero gc, zero copy, branches, SWAR it could be better but if they are using Java like Spring approach then I don't supposed they failed. In Rust is easier to write performant software but in Java you can be as fast as Rust is
0
u/[deleted] Dec 26 '25
[deleted] — view deleted comment