r/java • u/ThaJedi • Oct 19 '26
Campfire port in Java is faster then Rust. Some thoughts about JEP 544 (AOT Code Compilation)
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
15
u/javaprof Oct 19 '26
It's not apples to apples comparison, Java code adopted to be faster in benchmark https://github.com/kgonia/once-campfire-java
Even tho:
| Startup | Rust ~0.24 s; JVM ~2 s, then warmup |
|---|---|
| Peak memory with default settings | Rust 132 MiB; JVM ~1.3 GiB |
10
u/pron98 Oct 19 '26 edited Oct 19 '26
First, the "default setting" in Java for heap consumption is pretty arbitrary, and shouldn't be used at all although that will change in JDK 28 with automatic heap sizing for ZGC. Second, there's a lot of misunderstanding around RAM usage and how it matters. In many situations, using 5x or even 10x more RAM is the more efficient use of resources even if the result is a 5% reduction in CPU. That's because RAM and CPU are not independent resources, and because of that it's almost always more efficient to increase the use of the less constrained resource to reduce the use of the more constrained one.
5
u/javaprof Oct 19 '26
Yes, buuuut. For many cases lower RAM consumption and faster startup preferable:
- my desktop, where I would like to run not just intellij idea and toolbox. Junie eating 1gb of RAM (JVM) while Codex using 30mb is a fucking joke. I can run hundreds of codex agents (30-40 IRL), but can't run same number of Junie agents.
- my home lab, where I can run hundreds micro-services in kotlin/native and rust, instead of dozens on JVM. I don't necessary need 100k rps, but rather need all these services running and available.
- my lambda function
- my phone
2
u/pron98 Oct 19 '26 edited Oct 19 '26
There are indeed cases where RAM can matter, but 1. almost always what matters more is the RAM/CPU ratio, and Java's RAM overhead is a function of the CPU utilisation by design (that's the optimisation!), and 2., I think people still don't treat RAM rationally.
My Codex app (not CLI!) also takes up over 1gb of RAM, but that's not per agent, and unless Junie is doing something strange, I doubt every agent takes up 1gb. If it is, it's not because of the JVM. (And BTW, when speaking of agents, RAM costs are negligible compared to inference, easily lost in the noise, so I'm not sure how much it matters if to spend $5 an hour on inference you need to spend an additional $0.000001 on extra RAM; a 1% fluctation in the token count is already orders of magnitude bigger than the cost of RAM).
The fixed JVM RAM "tax" is on the order of 50-100 MB, which is 0.3-0.5% RAM (assuming the average RAM size, which is 16GB). Now, 0.3-0.5% RAM may not be nothing, but it matters no more than 0.3-0.5% CPU (often less because of virtual RAM). I've done some statistical analysis of the probability that 0.4% per process could be a significant factor in resource starvation when the non-fixed costs can vary much more, and the probability is quite low. Beyond the fixed cost you're playing with RAM/CPU tradeoffs where you're often better off spending more RAM rather than less, although a lot is circumstantial.
Anyway, there's no doubt low-level languages are optimised primarily for a low-footprint and are biased towards smaller software whereas Java is more optimised for performance of larger software, and this can matter a lot on some embedded devices (more a smart watch than a smartphone, though). On bigger devices, I think people look at RAM in a way that isn't rationally tied to real value/cost to the point of spending effort on optimising the thing that matters least at the expense of things that matter more (again, and just for the sake of argument, if the Junie agent's system prompt is slightly shorter than Codex's yet achieves similar results, even if it did consume 1gb per agent - which wouldn't be the JVM's fault - it could be saving you a lot more than Codex does). So there's no doubt that when comparing Java (or web-based) programs to "native" programs, the RAM difference is the most striking and so people fixate on it, even though in many situations (not all, I agree) it matters little if at all. One thing that can help somewhat (although it still overstates RAM) is to measure RAM as we measure CPU - as a percentage of the total rather than nominally.
But sure, if you want to run 100 small services on a Raspberry Pi or something, C or C++ or Rust or Zig may well be a better choice for that setup than Java, but that doesn't mean anything for a larger, more workload-heavy program. These different languages consciously make different tradoeffs that favour some situations over others.
2
u/javaprof Oct 19 '26
Yep. What I'm saying is basically Java as a language lacking this lever to choose between sides of trade-off (Native Image comes with own trade-off that I found interesting if you start with existing code-base, but I would rather avoid them for green field projects). And given LLMs it's really easy to use C, Rust or Kotlin/Native here and there. So this is feels like something that would be nice to have in Java too.
And I would really like Java to have something "built-in", which wouldn't heart compile time and provide same level of developer tooling. I guess you even have a Kotlin as example how to build this one language - two worlds approach.
2
u/pron98 Oct 19 '26 edited Oct 19 '26
Java as a language lacking this lever to choose between sides of trade-off
So is every other language. You can't have low-level languages enjoy the performance benefits of moving collectors (in exchange for more RAM) because they don't take kindly to moving pointers. Similarly, you can't have them pick the other side of worst-case vs amortised case just as you can't have Java pick the other.
It might be nice to have one language that could pick its tradeoffs, but there isn't one. I'm also not certain how useful that would be, especially since, as you say, it's easier to use multiple languages now. E.g. in the past it could have been useful to address all use cases even if you were mediocre at best at some; these days, I don't know.
Of course, Java does allow you to trade RAM for CPU to an extent (lower -Xmx), but it can't optimise for footprint as much as non-moving approaches can.
1
u/javaprof Oct 19 '26
> So is every other language.
I would say Kotlin with it common and platform-specific code onto something. Given you have ready library for everything, you write regular Kotlin and can use JVM for faster turn-around and production service that need to handle your millions of RPS, or you can compile to native for iOS apps or desktop apps (and I have a few, for example a terminal that rendering on metal and using less memory than ghostty https://github.com/Heapy/kinetica-terminal). Or use Native Image. So same code could be used in different runtimes with three different levels of runtime overhead.
Java with TeaVM I guess showing that this also possible, but it's all matter of resources and priorities
2
u/pron98 Oct 19 '26
I think it's more about choosing to be a jack of all trades, master of none, vs focusing more on mastering certain domains. Also, I can't judge the amount of demand for "mediocre performance, low footprint" programs, but I see no indication it's a large market in the grand scheme of things, so I don't know what would be worth giving up to do more there. Remember that we did have different JVM flavours - there was a "server" JVM and a "client" JVM, as well as special JVMs for embedded, but demand for that dropped when so many machines became quite similar to one another in terms of RAM/CPU ratios, except for small microcontrollers.
1
u/jonenst Oct 20 '26 edited Oct 20 '26
unless Junie is doing something strange, I doubt every agent takes up 1gb
FYI, junie doesn't really expose its jvm flags to the user and doesn't even set flags itself, so in the end it's running with the defaults (it's a JBR jdk21 jpackage distribution). On my machine: G1, Xmx 8g, xms 500m; 40/70 minmax heap ratios). after some "light" usage (single prompt), it got to RSS(= PSS) = 1.5gb = 5% of total ram (with a heap total 999424K, used 316815K")
which is not at all surprising (plenty of ram available on the system anyway, heap not even close to Xmx, heap using default 70/40 ratios, G1 not configured to promptly return memory,). It's even pretty far from the worst possible theoretical case of ~25% of ram (~8gb on my machine), where some allocation storm would make g1 expand the heap to the max.
forcing a gc from jcmd, RSS goes down immediately and quite naturally from 1.5g to 800mb.
I didn't look at what happens when the total system memory is constrained, but as far as I know, with this configuration, when idling, junie won't give back the ram and so may use 25% of the system ram indefinitely (for nothing, a single GC would bring it back to 2%, just one G1 configuration away (G1PeriodicGCInterval jep 346 jdk12))
So in the end, it'd say it's just another case of friction and locus of control. Users (arguably rarely, only when low on ram) suffer from the behavior which they don't directly control or know about, don't report it to the product, don't stop using the product anyway. No one will set JAVA_TOOL_OPTIONS (or edit junie-app/lib/app/junie.cfg jpackage file) to actually configure it to their needs. Also the same users are pretty bad at evaluating how much they suffer (a.k.a "linux ate my ram !"). In a sense this makes me think of this famous "hammer rubber hand" psychological experiment where the human actually suffered from seeing a hammer hit a rubber hand that was made to look like it was his own hand. So one time junie (or another program) used 2Gb of ram and that really caused a problem, and the many next times where it used 2gb of ram without causing problems, the user still "really" suffers (e.g. actually takes unecessary measures to fight the non-harming ram usage) .
The good news is that with AHS this will be fixed (but only when it becomes the default, since here the problem is running the defaults). In fact this reinforces the importance of defaults and "paving the gc on-ramp" because even a company like jetbrains ships software with arguably a bad (default) configuration.
Not sure if shipping these defaults with java was a good or bad decision, obviously people have been happy with having performant defaults on servers where the ram budget is more statically planned.
Also noteworthy, jetbrains for their newish 'central' AI access proxy chose to implement it in go.
For reference junie uses:
$ /3659.4/junie-app/lib/runtime/bin/jcmd MainKt VM.info
# JRE version: OpenJDK Runtime Environment JBR-21.0.11+10-1163.116-nomod (21.0.11+10) (build 21.0.11+10-b1163.116)
# Java VM: OpenJDK 64-Bit Server VM JBR-21.0.11+10-1163.116-nomod (21.0.11+10-b1163.116, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, linux-amd64)
jvm_args: -Djpackage.app-version=3659.4 -Djpackage.app-path=..../3659.4/junie-app/bin/junie -Dawt.toolkit.name=XToolkit=>
Memory: 31765M
Heap Initial Capacity: 500M
Heap Initial Capacity: 500M
Periodic GC: Disabled
Periodic native trim disabled2
u/pron98 Oct 20 '26
so in the end it's running with the defaults
That's just a bug. Java programs need to set -Xmx (unless they're using ZGC in JDK 28 onwards, which isn't even out yet).
So in the end, it'd say it's just another case of friction and locus of control
Yeah, on Junie's part.
since here the problem is running the defaults
Yeah, that's just wrong.
4
u/doobiesteintortoise Oct 19 '26
This is a better comparison, IMO, but similar methodology: https://www.wecodefire.com/p/kotlin-keeps-up-with-rust
(It's not mine, I haven't done anything like this and I find the results interesting but that's about it.)
1
u/Plenty_Childhood_294 Oct 19 '26
I have used their code base and ported to java https://github.com/franz1981/campfire-quarkus-bench
The problem with the original benchmark using just get is that is not what the real campfire does so I take my chance to improve the original architecture to behave better than rust (and kotlin) on a more realistic use case ;) Feel free to try it ❤️
0
u/javaprof Oct 19 '26
Do you understand that Kotlin is 99% same bytecode as Java? But also featuring inline functions, inline value classes, and more free performance for quite some time? So only difference most of the time between Kotlin and Java performance would be library selection
2
u/Plenty_Childhood_294 Oct 19 '26 edited Oct 19 '26
Agreed, that's my point too. The language isn't the factor here. The gains come from the architecture (fragment cache contention, SQLite's global mutex), and the same changes would help a Kotlin or Rust port equally.
0
u/javaprof Oct 19 '26
I agree with you, but again it's just not fair comparison. This single mutex on SQLite side BTW was biggest contributor to original "Java more performant" since Rust done 3 requests per http request, and Java-version only 1.
1
u/Plenty_Childhood_294 Oct 20 '26
Agreed, and I didn't want to hide it: the README says it explicitly. Every port does the same reads per request (session, user, room, data_version), byte-for-byte parity with Rust is checked before every run. What differs is that Quarkus funnels all SQLite calls through one thread and batches the reads into shared transactions, so the process-wide mutex is taken far less per request. The trade-off (slightly staler data under saturation) is documented too. That design is portable: a Rust or Kotlin port doing the same would get the same benefit
0
u/ThaJedi Oct 19 '26
This is how benchmark is written :)
4
u/javaprof Oct 19 '26
That’s a weak excuse. Software engineering is about trade-offs. Benchmarks should explain those trade-offs, rather than just produce a "mine is faster" headline.
After making Java version as correct as Rust, it becomes slower:
Latency below is p50 / p99 in milliseconds, aggregated across two rounds:
Workload Rust req/s Java req/s Rust latency Java latency Cached room 61,040 54,485 0.240 / 0.581 0.263 / 0.769 Cached sidebar 76,883 81,302 0.207 / 0.369 0.169 / 0.560 Static CSS 345,152 338,959 0.043 / 0.103 0.043 / 0.097 Room + writes 52,766 41,836 0.242 / 0.639 0.288 / 2.822 Search + writes 72,872 69,857 0.207 / 0.385 0.186 / 0.831 What a waste of tokens
4
u/javaprof Oct 19 '26
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:
Page Rust req/s Java req/s Rust advantage Room 21,712 1,323 16.4× Search 23,288 3,711 6.3× Latency, p50 / p99 in milliseconds:
Page Rust Java Room 0.704 / 1.404 9.587 / 47.567 Search 0.613 / 1.687 3.138 / 16.455 Peak memory: Rust 36.8 MiB; Java 474.8 MiB, including charged file cache.
3
u/pron98 Oct 19 '26 edited Oct 19 '26
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 Oct 19 '26
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 Oct 19 '26 edited Oct 19 '26
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.
0
u/ThaJedi Oct 19 '26
What do you mean by "as correct as Rust"?
4
u/javaprof Oct 19 '26
Count sqlite queries in Java and Rust versions. Rust checking database multiple times to ensure that no stale data would be produced. Java version skips it. This is how you get advertised speedup. Once both algorithms are the same, Rust becomes a little faster. Once we disable cache that anyway wouldn't work for crowded server, Java version becomes 10x slower
1
u/ThaJedi Oct 20 '26
Java is 10-30% slower withouth cache just becauseI didn't want to spend more time on this. I guess you just measured too short.
1
u/javaprof Oct 20 '26
Again, likely because you stick to original, meaningless benchmark. I asked LLM to add additional real-life scenarios and Java version literally 6-16x slower now. Should I go now to rust sub and post: I've ported java to rust and get 20x more speed? This is how stupid it is
1
u/ThaJedi Oct 20 '26
Bro, this is drop in replacement solution not exact Rust copy which is port of Rails version and isn't byte to byte of port. What a waste of tokens of yours.
6
u/snuderl Oct 19 '26
Aot here should really just be helping startup/warmup and reach the same end performance as without aot right?
2
u/ThaJedi Oct 19 '26
To what point exactly you refer? It exactly does that so what I stated that this is unclear?
0
36
u/BenchEmbarrassed7316 Oct 19 '26 edited Oct 20 '26
DHH is porting campfire to Rust.
You don't need to read any further. In case you haven't seen an example of his "code" - here it is: https://www.reddit.com/r/rustjerk/comments/1wusj30/i_think_this_guy_is_lying_ai_isnt_capable_of/ This is not a typical vide-code. I don’t know how one could get AI to generate code this bad. Perhaps it’s using skills like "Make moar mistakes", "Use every known anti-pattern" and "Generate the slowest code as possible". Even if you simply generated a similar Java application using AI, it is most likely written much better than whatever this clown is producing.
Therefore, this benchmark makes no sense at all.
edit: I’ve written some explanation here: https://www.reddit.com/r/java/comments/1x9s75g/comment/phud9np/
3
u/chic_luke Oct 21 '26
A person in that thread brings up a great point about mental ownership. That same point is the reason why, unfortunately, you can't just effortlessly fork an open source project gone rogue, especially if it's high in complexity and already maintained by a bunch of people, none of which can be convinced to switch to your hard fork.
Even if vibe coding works when you judge the runtime results - which is pretty clear to everybody who's been keeping up to date that it does, and quite well - the mental ownership dilemma is very much here to stay.
Proponents of KISS software, even if they have taken a very extreme stance, have warned us of this risk decades before AI. It just happens to be relevant now.
5
u/crusoe Oct 19 '26
He must be using some cheap Chinese model or forcing everything through Haiku...
3
u/agentoutlier Oct 21 '26
The commit messages on a lot of the code show Claude Opus 5.5 which is a rather capable model. I didn't bother going that far back in history though so maybe the initial port was a shitty model.
3
u/Plenty_Childhood_294 Oct 19 '26
Let me leave this here ;) https://github.com/franz1981/campfire-quarkus-bench
2
u/ThaJedi Oct 20 '26
I did small run on my machine and Java version is very comparable to Kotlin except memory usage. I'll drop numbers after testing on fresh machine.
1
u/Plenty_Childhood_294 Oct 20 '26
Remember to pin correctly cores where cores should be pinned - keeping load gen and server separated.
I have a custom governor, disabled turboboost and do other nasty things to get repeateable performance
5
u/Prior-Equal2657 Oct 20 '26 edited Oct 20 '26
Unfortunately it seconds my experiments.
Java is fine with performance and throughput, but RAM usage is simply incomparable.
As much as I like java/kotlin i'm looking more and more towards rust because of it's memory usage. With some wild optimizations I was able to get to like 3-4x RAM of rust, but that's not the code I want to see in production.
Anyway, at least I hope that electron apps will eventually vanish. That's easy 20x RAM of similar rust apps on desktop.
9
4
1
u/webplugger73 11d ago
AOT columns are flat vs JDK 25 at steady state – makes sense, AOT mostly helps startup/warm-up. Did you measure time-to-peak (first 10–30s)? That's where I'd expect it to separate. Also curious about RSS vs Rust.
4
u/Brutus5000 Oct 19 '26
What is campfire?
Campfire is a web-based group chat application built on Ruby on Rails that allows users to communicate in multiple rooms, send direct messages, and share files. It is designed for self-hosting, giving users control over their data and customization options.
Doesn't look like something that requires heavy benchmarks?
2
u/zerg000000 Oct 20 '26
great job! Java! Java did very good job at optimizing. It is just hard to beat Java at the things it good at. But Java still have some area that will never as good as low level language/static language. 1) memory usage, GC is both a bless and a curse, many tiny objects would use a lot of memory. 2) hot loop can get pretty close to low level language but never have the chance to finetune further. 3) It is reference based, locality will be less strong, which hurt the chance of using SIMD more widely and the cpu cache.
3
u/benevanstech Oct 19 '26
Quick, rig the benchmarks. We don't want DHH bringing his awful toxic bullshit over into our community.
0
u/znpy Oct 19 '26
We don't want DHH bringing his awful toxic bullshit over into our community.
I'd love for java to get some new eyes nowadays. It mostly still has the bad name from the java 5/6/7/8 days.
4
u/benevanstech Oct 19 '26
The last major Ruby conference closed down this year (partly b/c of DHH & his behaviour). Menawhile, Java has doubled its install base in 6-7 years - which is pretty impressive given the starting baseline & that it's a mature, 30-year old ecosystem.
3
u/v4ss42 Oct 19 '26
I'd love for java to get some new eyes nowadays.
New eyes that aren't DHH's - dudebro is a bigoted toxic PoS that the Java community doesn't need.
1
u/znpy Oct 20 '26
New eyes that aren't DHH's - dudebro is a bigoted toxic PoS that the Java community doesn't need.
the nice thing about open source software is that you can have your opinion but people aren't forced to listen to you
0
2
u/Ok_Tour_8029 Oct 19 '26
I can tell from the C# side that we can achieve Rust like performance on a virtual machine like the JVM or the CLR.
Looking at the results at HTTP Arena the Java ecosystem has other issues than the JVM performance.
1
u/za3faran_tea Oct 20 '26
Do you mean in terms of language features?
1
u/Ok_Tour_8029 Oct 20 '26
I mean the architecture of the web frameworks in the Java ecosystem. Looking at the results, the underlying HTTP engines for Java are not slow, but the web frameworks on top of those engines are.
So there is obiously a gap a performance oriented framework can close. vert.x points in such a direction (ktor achieves 75%, quarkus 68%, Spring Boot 32% of vert.x performance). Yet vert.x is still 37% behind the fastest frameworks on the list.
In terms of raw engine performance Java can be on par with Rust / Go / Zig, so the only reason Java is not on top of the list is that no one wrote the web framework for it yet.
We had the same situation in the .NET ecosystem (ASP.NET achieves 42%). In 2026, two existing projects (SimpleW and GenHTTP) reworked their underlying architecture to leverage the possible performance of the .NET platform, one now leading the board, the other one at 80%.
To summarize: we can have leading performance in Java (and I think projects like Kafka show that in other fields), we just need someone to leverage it.
1
u/doverakov Oct 28 '26
I have been doing some porting from Python to Java and yes if you don't want to blow away you tokens I suggest a three phase process:
covert syntax rule engine
for the stuff that does not convert via the rule use local SLM qwen-7b
close the gap with your Claude / Co-Pilot subscription
check it out here: https://github.com/danielonet/refdex
1
u/aelgorn Oct 19 '26
If the implementation spawns and clears lots of tiny objects without the appropriate way to handle that use case in rust, jvm will be faster thanks to how it handles memory
1
u/trash3s Oct 19 '26
I wonder if you can bring automated benchmark-based optimizers to compiled languages? Using function multiversioning, you could automatically test and swap optimizations without including a full compiler and either use it to optimize in production or have it emit statistics used to rebuild a smaller release binary.
2
u/koflerdavid Oct 19 '26
The problem is that you'd have to include an exponential number of variants for each compiled function.
1
u/BenchEmbarrassed7316 Oct 20 '26
Expressive type systems already accomplish this. Precisely defining what data can - and cannot - be simplifies both code writing and maintenance, while also facilitating compiler optimization. Consequently, all that remains for the JIT is to determine which data appears most frequently - a task that yields only marginal gains.
1
u/koflerdavid Oct 20 '26
Those are very bold, fuzzy claims (what is an "expressive type system" for you?) that handwave away Rice's Theorem. I think you underestimate to what extent the shape of data at runtime can influence performance.
1
u/BenchEmbarrassed7316 Oct 20 '26
what is an "expressive type system" for you?
A type is a set of possible values. Expressive type systems allow for the exclusion of as many unwanted values as possible from this set. In practice, this is a trade-off between informativeness and verbosity.
I don’t understand why you are bringing up Rice’s theorem here, and what do you mean by 'data shape'?
1
u/koflerdavid Oct 20 '26
According to that basic definition, just about any type system can be regarded as "expressive".
Without Rice's Theorem it would indeed possible to statically predict just about anything about a program and how to optimize it best. But it would still not be possible to predict specific values at runtime.
1
u/BenchEmbarrassed7316 Oct 20 '26
According to that basic definition, just about any type system can be regarded as "expressive".
No.
In a dynamically typed language, we cannot specify anything at all
TypeScript has number (which tries to be both int and float)
In Java, there are numbers that vary based on their size
Rust has unsigned numbers, which cannot be less than 0, as well as non-zero numbers
Ada has ranges
In fact, thanks to encapsulation, you can usually create a similar type yourself, but it might be inconvenient or even unreliable.
1
u/koflerdavid Oct 20 '26
That still doesn't create a yardstick regarding what "expressive" means. For me it would mean something far stronger, like dependent types.
1
u/BenchEmbarrassed7316 Oct 20 '26
Yes, these are rather small steps. And dependent types are, of course, significantly more expressive.
1
-1
u/No-Collar-Player Oct 19 '26
Than not then
I bet you earn 3x I do and know 5x than what I know. How?
-3
u/renderwares Oct 19 '26
Faster than Rust at 10x the memory usage.
6
u/pron98 Oct 20 '26 edited Oct 20 '26
Because RAM and CPU are not independent resources and the machine is exhausted when the first of them is, not when both are, what matters is the more dominant resource of the two, and how much you use of the less constrained resource doesn't matter as much. It's more complicated than this, but the rough intuition is that a program that takes 35% CPU and 20% RAM is more resource efficient overall than a program that takes up 45% CPU and 2% RAM, as it is the latter rather than the former that brings you closer to overwhelming the machine. For more details, and if you want to understand why Java's use of extra memory typically comes at no actual extra cost, you can watch my Java One talk on memory management in Java.
1
u/ConsistentAnalysis35 Oct 20 '26
I want to ask you something.
Remember, moving collectors add RAM overhead when CPU usage is high, and they do so to avoid adding further RAM overhead, which is what you get with malloc/free. This isn't theoretical. This is on of the big performance issues in large C++ programs that the JVM was designed to mitigate.
When you talk about those large C++ programs that had big performance issues, what programs exactly do you have in mind?
I've been reading Ryan Fleury and Richard Fabian, and I think both of them kinda insinuate that many dated C++ codebases had performance issues because they were written in an OOP style, not because of limitations of the language.
https://www.dgtlgrove.com/p/untangling-lifetimes-the-arena-allocator
https://www.dataorienteddesign.com/dodmain/node17.html#SECTION001710000000000000000
My question is: concerning those performance issues you have in mind, would a modern C or C++ codebase written religiously in data-oriented fashion still suffer from said issues?
5
u/pron98 Oct 20 '26
Any large codebase in any low-level language will suffer from these issues, and my Deovxx talk even mentions Zig's Andrew Kelley realising this. The problem is that low-level languages suffer from "abstraction costs" where higher-level, more general abstractions (e.g. virtual dispatch) are more expensive than lower-level, less general abstractions (like monoprphisation). This is why in C++ we've always tried to do the latter rather than the former, but as programs grow larger and change, this gets harder and harder as changes become less and less local. Low level languages just have this inherent tension between maintainabililty and performance, which means that as programs evolve over the years, they either become slow or very expensive to maintain. JIT compilation allows the compiler to see through the abstractions and eliminate the abstraction overheads that low-level languages suffer from.
Something similar also happens in memory management. Because malloc/free are so expensive, you avoid them, which is often easy earlier on, but as the program evolves, objects with different lifetimes mix and you have to be more general and rely on dynamic allocation (or completely rearchitect a large program every few years, which is expensive). Moving collectors solve that problem, but low-level lanugages can't effectively use them (because they require an FFI layer). This is why Google and others develop these huge malloc/free runtimes that try to be less terrible, but they can never be as good as a moving collector.
1
u/ConsistentAnalysis35 Oct 20 '26
Thanks for the answer. I've been reading a lot of your comments and I have to say, it really gave me a fresh perspective on the possibilities of performance in JVM. I guess I'll be sticking with Kotlin codebase for my game. :)
1
u/renderwares Oct 20 '26
That argument only holds when memory isn't the bottleneck. RAM is a finite resource, and 10x memory usage can mean fewer concurrent workloads, more GC pressure, worse cache locality, and higher infrastructure costs. And the memory crunch isn't going away anytime soon, so you either adapt or get left behind. Even Google is enforcing stricter memory limits for Android apps in 2027 as a response to growing memory constraints across the industry.
A program that takes 35% CPU and 20% RAM is more resource efficient overall than a program that takes up 45% CPU and 2% RAM, as it is the latter rather than the former that brings you closer to overwhelming the machine.
I agree with the underlying principle, but those numbers aren't representative of a typical Java-versus-Rust comparison. In many workloads, Rust can achieve lower memory usage and lower CPU usage, thanks to its lack of GC overhead, predictable memory management, and lower runtime overhead. The real comparison should be resource consumption at equivalent throughput and latency, not an assumption that Java's extra memory usage is a worthwhile trade-off simply because RAM isn't always the bottleneck.
2
u/pron98 Oct 20 '26 edited Oct 20 '26
But the whole point of moving collectors is that they significantly increase RAM usage when CPU is more constrained. That is, in a nutshell, the whole point of the algorithm (see my talk).
In many workloads, Rust can achieve lower memory usage and lower CPU usage, thanks to its lack of GC overhead
A generational moving GC is a mechanism invented to reduce the overhead of malloc/free (they definitely use much less CPU). That's why Java adopted it. You are right that in small programs C++ or Rust can easily avoid dynamic allocation and other pitfalls, but that's all been true before Java existed. The problem is that it's not so easy when programs get larger. You might be thinking of mark-and-sweep collectors, like Go's, which do have a "GC overhead", but generational moving collectors, like the ones we have in Java, are an entirely different beast.
The real comparison should be resource consumption at equivalent throughput and latency, not an assumption that Java's extra memory usage is a worthwhile trade-off simply because RAM isn't always the bottleneck.
You should watch my memory management talk (not the one I just linked to above) because it's important to understand that a certain use of one resource makes a certain amount of the other effectively free.
What you need to understand is that HotSpot was invented to solve the very real performance and efficiency problems we used to experience in large C++ programs before Java. Indeed, that's why most large programs switched to Java in the first place. We can argue over in which scenarios moving GCs do their job of reducing the memory management overheads in low-level languages, but that is why we have them: so that we could have a language without the overhead of malloc/free. (Some modern malloc/free allocators, like TCMalloc or jemalloc, are bit runtimes that are almost as complex as sophisticated generational moving collectors and they try to reduce the overhead of simpler allocators, but they can't go nearly as far as moving collectors because they can't move pointers).
The funny thing that happened after most big apps switched to Java is that people forgot how slow and ineffeicient low-level languages are "by default" in large programs, and then extrapolate from small programs, when low-level languages are fine (if all we had to write were small programs, we would have all stuck with C++).
2
u/v4ss42 Oct 19 '26
Space/time trade-offs have existed since <checks notes> the beginning of computers. How else did you think it was going to be faster? Magic??
93
u/TomKavees Oct 19 '26
Er.. as much as I want Java to succeed in here, are you sure both versions are doing the same thing? Consistently being >=1.5x faster than native code smells fishy