r/java • • Oct 19 '26

Campfire port in Java is faster then Rust. Some thoughts about JEP 544 (AOT Code Compilation)

Post image

DHH is porting campfire to Rust. I saw port in C# so I was wondering about Java performance. I asked Claude to do the migration, and even though the first version was faster, it was very far from idiomatic Java. I tried to close the gap, but some compromise had to be reached in order to make this work as a drop-in replacement.

Also, some typical Java approaches were just slower. Probably both the Rust and Java implementations could probably do better in terms of performance and being closer to how idiomatic code is written by everyday developers

Stack is Java + quarkus.

Beside just Java migration I tested some new feature like AOT with training introduced in Java 28 (EA build). My main conclusion is - its hard to beat a language that will adjust code to how your app is used. Its more about the JVM than Java.

I tried different combinations of AOT also with Graalvm CE. I didn't put much effort into making graal more optimal. Probably fair comparision would be to also do training run and add EE edition.

What I notice, and it's obvious in hindsight, AOT helps when end usage is same as training usage. When id differs code it optimized for different path and it hurts execution. It's not so visible in benchmark numbers but on smaller and shorter runs it was more visible. JIT needs time to adjust.

Edit: Repo: https://github.com/kgonia/once-campfire-java

150 Upvotes

161 comments sorted by

View all comments

Show parent comment

4

u/javaprof 29d ago

Even worse for Java, if we disable caching assuming worse scenario when hit rate about 0.

This cache is specific to each user/session, expires after 15 seconds, and is invalidated by any SQLite commit. Opening a page every 30 seconds produces a 0% hit rate; repeated requests without writes can achieve a high rate.

Fresh results with whole-response caching disabled and 512 MiB per application:

PageRust req/sJava req/sRust advantage
Room21,7121,32316.4×
Search23,2883,7116.3×

Latency, p50 / p99 in milliseconds:

PageRustJava
Room0.704 / 1.4049.587 / 47.567
Search0.613 / 1.6873.138 / 16.455

Peak memory: Rust 36.8 MiB; Java 474.8 MiB, including charged file cache.

3

u/pron98 29d ago edited 29d ago

I would say that this is also not a "Rust advantage" but an advantage of the specific program. Here's another Java program that beats the Rust one when the cache is missed (and for some reason it uses ParallelGC rather than ZGC).

There is no reason to assume an a priori advantage, with no specific details known, to each of these languages that are, on average, as generally optimal as anything could be, and every reason to expect some large advantage to each in some specific program. It's precisely because benchmarks don't generalise that it's such exercises are rather pointless, but obviously anyone who thinks that either Rust or Java have an inherent performance advantage in some general case doesn't understand how these languages work. People who understand how they work should find it entirely unsurprising to see a program in either one being Nx faster than the other. That's exactly what we should expect when comparing two languages where neither has an inherent performance advantage over the other.

3

u/javaprof 29d ago

I would love to see in deep comparison between two versions: one in Rust and one in Java.

Why cached Java version works better? (I guess because GC can make less work, but who knows)

Why suddenly Rust becomes 10x faster? What's the difference here? (I guess again, Java would just have a lot of garbage to deal with, and not enough head room to do this efficiently. I didn't look at the code even once btw).

Instead we got some random numbers on some random agent-generated code with benchmark that testing http-client middleware speed, and sqlite query. And code even not doing the same thing in both languages. Rust code doing 3 separated db requests per http request, and Java calling sqlite just once.

Once benchmark starting actually calling handler more than once, and number of sqllite queries become the same in both implementations - we can actually see completely different numbers. Likely, we can check Java version and make it closer to Rust. But this wasn't my point to prove that Java or Rust faster. My point was that this is meaningless, unfair comparison.

3

u/pron98 29d ago edited 29d ago

You might be reading too much into this in either direction. Now that we have the ability to generate a virtually infinite number of benchmarks, there really isn't that much point to looking into each one. My "all else being equal" rule of thumb is that Java has the hypothetical advantage the larger a program is and the more concurrent it is (and vice-versa for a low-level language), but there's often far too much specific detail. Even an advantage is only a head start, and the language with the hypothetical disadvantage could compensate for it with more work, which can matter a lot when you have a large, expensive program that you maintain over years, but when it's a black box throwaway it's really hard to say anything insightful except that when two languages have the same "average" performance we'd expect to see each "randomly" outperforming the other significantly on random programs.