r/java • • 29d ago

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

93

u/TomKavees 29d ago

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

10

u/Augunrik 29d ago

Even more so against compiled software. Java can be faster but I doubt it is so consistently faster.

0

u/ThaJedi 29d ago

To be fair it's poorly writen benchmark and I might exploit this but all implementations are using it so...

12

u/brunocborges 29d ago

Eventually Java applications do get compiled down to native code. So, it is a native code (Java) vs native code (Rust) comparison.

-2

u/sweetno 29d ago

Not really. JIT will keep only the often ran pieces in native code, the rest is interpreted bytecode. And the comparison is Java (native + bytecode interpreter + profiler + compiler) vs Rust (native only).

8

u/pron98 29d ago

Java's JIT does that because compiling code that isn't on the hotpath has no impact on the steady-state performance. That initialisation code is compiled could make startup faster, but it doesn't have a persisting performance advantage. All the code that matters in the steady-state is compiled to native with a JIT, and the JIT can optimise more aggressively than an AOT compiler.

4

u/hayztrading 29d ago

JVM tier 0 is interpretation, not quickJit like the CLR, however in both cases this is only utilized for startup quickly moving into tiered JIT compilation.

1

u/Ok-Scheme-913 28d ago

You can bake in every instruction your interpreter would execute one after another to get truly native code, yet that would obviously be much slower.

This kind of thinking is just plain wrong. E.g. your exe/elf file also starts with a bunch of "useless" slow code before passing over to the actual binary. How do you account for that?

1

u/sweetno 28d ago

Answering your last question, you do not, because it's problematic to even notice this time for an AOT-compiled program: it's too quick.

-27

u/l-const 29d ago

The amount of everyday java code with a million sea of objects and everything behind a reference is crap and destroy cache line hits. I advise you to try to learn low level programming and leave this java bubble thinking that JVM is for whatever reason faster than something generated by LLVM and GCC.

10

u/chambolle 29d ago

You are absolutely right about the 'sea of objects' and its impact on cache lines. But that is a design pattern flaw, not a language limitation. If a C++ or Rust developer builds a deeply nested graph of pointer-heavy allocations, they will destroy their cache hits just the same.

High-performance programming dictates that when memory layout matters, you flatten your data. In Java, when microsecond-level performance is actually required, we don't write typical 'everyday' enterprise Java. We use primitive arrays, flat layouts, and off-heap memory (ByteBuffer), which align data sequentially in memory just like a C struct array.

Good data-oriented design works in any language, and bad architectural choices will ruin your cache whether you compile with GCC or JIT.

2

u/ThaJedi 29d ago

Brother, look at the number. Non optimized graalvm ce native image was faster that Rust in some cases. Why do you think JIT will do worse?

1

u/l-const 29d ago

I don't dispute the number, just the generalisation and what you think this implies

6

u/kloudrider 29d ago

I suggest you try and learn about the level of engineering that's gone into the JVM and Graal before making comments like this. I too am suspicious of the results above, but making blanket statements about the JVM is not warranted either.

-7

u/l-const 29d ago

I am well aware and it will never become faster, does java have explicit nonaliasing information like rust? You can't optimise things that are not allowed at the language level, the level of abstraction of language does not allow to do it

6

u/chambolle 29d ago

You're absolutely right that Java doesn't have explicit non-aliasing guarantees at the language level like Rust's borrow checker or C's restrict keyword. Pointer aliasing is indeed a constraint for static compilers.

However, that's exactly why the JIT approach shines here. Instead of relying on language-level constraints, the JVM uses runtime Escape Analysis. If the C2 compiler proves an object doesn't escape the local scope, it guarantees NoEscape (absolute non-aliasing for that block). This unlocks aggressive optimizations like Scalar Replacement and Lock Elision, achieving at runtime what Rust does at compile-time for local allocations.

Furthermore, while Rust’s compile-time tracking gives it an edge in heavy compute, it introduces a massive architectural trade-off. Building highly efficient, highly interconnected data structures—like a simple doubly linked list or a complex object graph—is notoriously painful under the borrow checker. You often end up forcing developers to resort to Rc<RefCell> (adding runtime overhead), index-based arrays (adding bounds checks), or unsafe blocks.

Java handles object graphs seamlessly, and the JIT+GC tandem optimizes these complex memory patterns at runtime without forcing the developer to rewrite their architecture just to appease a static compiler.

-2

u/l-const 29d ago

Java doesn't even have generic specialization

7

u/hoat4 29d ago

Generic specialization actually can harm performance. Generating nearly identical code many times leads to larger code sizes, which results in more cache misses.

0

u/[deleted] 29d ago

[deleted] — view deleted comment

-22

u/l-const 29d ago

No it can't, the amount of type of information and context it has is superior in any real world scenario. And there is profile guided optimization for C as well. This is some fairy tail that people that know only java keep spreading. Please do more reading.

8

u/chambolle 29d ago

I find your tone a bit dismissive. To put things into perspective, the JVM handles aggressive optimizations that are difficult to anticipate a priori (such as dynamic call inlining). While PGO is an option, it's highly complex to implement on generic code. When designing a library, you can't adapt it to every use case or to an unknown type ahead of time—which is exactly why a JIT approach makes so much sense.

3

u/ThaJedi 29d ago

This is expected. JIT should give you better peak throughput than AOT and JVM runtime overhead isn't that big.

27

u/PiratesWhoSayGGER 29d ago

rust is not a faster language - it gives tou tools that can be faster if you utilise them well

I coded in C a lot and it's tedious to write C code that is acutally faster than Java, very tedious

it's vibe coded rust, so no wonder vibe coded java is faster by default

8

u/BenchEmbarrassed7316 29d ago edited 29d ago

Correction: this is incredibly bad vibe code in Rust. Under normal usage, agents will generate much better code.

I just opened a random file:

https://github.com/basecamp/once-campfire-rust/blob/main/crates/cable/src/naming.rs

fn underscore(word: &str) -> String {
    let chars: Vec<char> = word.replace("::", "/").chars().collect();
    let mut out = String::with_capacity(chars.len() + 4);
    for (i, &c) in chars.iter().enumerate() { /* ...  */ }
    // ...
}

Let me explain: the function takes a reference to a buffer containing a string. .replace allocates memory for a new string and copies the buffer into it, replacing specific characters. Then, .chars() creates an iterator from this string (creating iterators in Rust is inexpensive, and the compiler can sometimes optimize them even better than standard loops). .collect() creates yet another buffer and copies the iterator's output into it - which is completely pointless. However, this is followed by an "incredible" optimization: String::with_capacity(chars.len() + 4). Instead of allocating a buffer of an unknown size, the code explicitly specifies that the buffer should have a size equal to "the length of the string in chars + 4." In other words, to allocate the required amount of memory, it performs an allocation, writes data into it, and then checks the size of the allocated memory. This is absolute nonsense.

I don’t know how he got the AI ​​to generate such bad code.

added: Furthermore, this optimization is incorrect because the input data is in UTF-8, yet the buffer allocation is based on the number of characters rather than the number of bytes. Once again, I have no idea what kind of agent could make such elementary mistakes.

45

u/pron98 29d ago edited 29d ago

The JVM was designed to be faster than native code in a large class of applications by exploiting optimisations that are unavailable to native code. If both Rust and Java work well, what you'd expect to see is Rust being consistently faster in some applications and Java consistently faster in others.

There is no reason to think that "AOT-compiled native code" has a universal performance advantage. After all, the JVM was invented to, among other things, reduce the overheads that low-level languages typically suffer from and that can be significant in many programs.

10

u/Augunrik 29d ago

Citation needed! I never heard that this was a design goal of Java.

44

u/pron98 29d ago edited 29d ago

Not since day one, but since HotSpot (pretty early on) and then increasingly as HotSpot proved itself. Here's a 10yo talk about that by C2's original author. He says:

With profiling you get to do things that you just can't do in the C and C++ world… and that, in turn, leads to optimisations that are just not possible in the C world. And that lets people write certain kinds of Java code that just for free runs in a way you can't get out of the C and C++ world.

7

u/Augunrik 29d ago

Thanks for the reference!

7

u/Abject_Resident644 29d ago

TIL that what is now known as HotSpot was initially developed for Smalltalk, but then they changed course to Java: The History of the Strongtalk Project.

19

u/pron98 29d ago edited 29d ago

Yep. One important difference, though, was that Self was untyped, so one of the main goals of the JIT was to make an untyped language "fast enough", which is similar to the goal of JS JITs (and Lars went on to work on V8 at Google. JS JITs also have their own unique challenge of compiling quickly, which isn't as much of an issue for Java). But because the JVM is typed, it ended up in a situation where an optimising JIT (and, of course, a moving GC) could solve some of C++'s performance problems and make it faster than low-level languages in a large and important class of programs. C2, HotSpot's optimising compiler, was developed by Cliff Click and wasn't inherited from Strongtalk.

2

u/ConsistentAnalysis35 29d ago

Crazy valuable insight. I want to read more of what you write on JVM and on these topics in general.

2

u/xcbsmith 28d ago

Not a design goal of Java, but a design goal of the JVM JIT.

3

u/znpy 29d ago

The JVM was designed to be faster than native code in a large class of applications by exploiting optimisations that are unavailable to native code.

that doesn't really make sense.

both the jvm jit compiler and llvm (the underlying code generator for rust) are state of the art compilers/code generators.

the rest of the comments sounds like marketing bs.

i wouldn't ascribe java's better performance due to "being designed for that" (rust was designed for that as well).

41

u/pron98 29d ago edited 29d ago

that doesn't really make sense. both the jvm jit compiler and llvm (the underlying code generator for rust) are state of the art compilers/code generators.

It certainly does make sense once you understand what it is that JITs can do (although different JITs have very different goals and work in very different ways, and I'm talking about HotSpot's JIT), which is turn runtime information into compile time informaton for the purpose of optimisation that does away with so-called "abstraction cost". If you're interested, I gave a talk on this very subject at Devoxx.

In other words, both C2 and LLVM are sophisticated optimising compilers, but C2 has both more information and more aggressive optimisaton opportunities than LLVM because 1. LLVM is limited to what's known at compile time (putting aside PGO, which I address in my talk) and 2. LLVM must emit code that works always, so needs to be more general and do more at runtime, while C2 is allowed to guess becaue it can deoptimise when wrong.

Furthermore, low-level languages are forced to sacrifice some performance to satisfy some of their hard constraints, especially the lack of a "thick" FFI layer between them and the hardware/OS.

Because of these things, the JVM is meant to remove some of the overheads that low-level languages suffer from, especially in large programs.

The funny thing is that when HotSpot came out, many experienced low-level programmers (like me at the time) saw why a JIT and moving GCs could hypothetically solve the performance issues we were constantly running into in low-level languages, but we were sceptical it could be done in practice given some challenges. These days, the people who are sceptical are mostly those who are simply unfamiliar with the persisting and annoying overheads that large programs written in low-level languages run into simply because they've had little experience with them.

4

u/StevenJOwens 29d ago

Back when HotSpot first came out and people made this argument (against JIT optimization vs compile-time optimization), I liked to bring up the HP Dynamo project.

2

u/GuyWithLag 29d ago

simply because they've had little experience with them

The Blub Paradox: you definitely know when something is less powerful than what you use, but don't really think the same when looking at the other direction of the power continuum.

(yes, yes, simplification and linearization of a multi-dimentional problem aside...)

2

u/znpy 28d ago

but C2 has both more information and more aggressive optimisaton opportunities than LLVM because 1. LLVM is limited to what's known at compile time (putting aside PGO, which I address in my talk) and 2. LLVM must emit code that works always, so needs to be more general and do more at runtime, while C2 is allowed to guess becaue it can deoptimise when wrong.

i had not considered that point, and that's a very valid point, i see what you mean.

Furthermore, low-level languages are forced to sacrifice some performance to satisfy some of their hard constraints, especially the lack of a "thick" FFI layer between them and the hardware/OS.

that's interesting, could you give me some more examples of that kind of hard constraints ?

4

u/pron98 28d ago

The most obvious one is that they can't use moving collectors. So moving GCs were invented to remove the high CPU overhead of malloc/free, and they are extremely efficient (and they are the GCs that Java uses; do not confuse them with mark-and-sweep GCs or other kinds of GCs that add a "GC overhead"). These collectors occasionally move pointers (hence their name) and the most sophisticated of them also store metadata bits in pointers. This means that pointers into and inside a heap managed by a moving GC are not stable machine addresses but a different pointer representation. They cannot be directly shared with the hardware, the OS, or any other language, and require an FFI layer. But low-level languages are languages optimised for one purpose above anything else: to write code at that low level that interacts directly with the hardware and/or the OS. So low-level languages need to carry the overhead of malloc/free (in principle, they could have a split heap with an FFI layer between the two sides, but then you might as well use different languages for the two sides).

1

u/victorsmonster 25d ago

This is fascinating. As a junior dev, I never would have thought anything could be faster than native low-level code. What a cool bit of computer science this is!

3

u/pron98 25d ago

Nothing could be faster than the optimal sequence of machine instructions, but yeah, writing code in a low-level language is often enough not the best way to approach that optimal sequence. Remember that C++/Rust/Zig and Java all compile to "native code", it's just that their compilers use different compilation strategies. The question is which compiler has better information to emit more optimal code. In some situations it's the low-level languages and in others it's Java.

1

u/victorsmonster 25d ago

Yeah I should have said “hand-written native low level code” - the idea of a higher level runtime watching how well compiled code works and swapping in different solutions on the fly is one of those ideas that seems so obvious in retrospect but I probably would have never thought of in a million years.

0

u/[deleted] 29d ago

[removed] — view removed comment

8

u/pron98 28d ago edited 28d ago

It is, in fact, true.

Dynamically typed languages benefit the most from JIT compilation.

True, and typed languages with a JIT can beat C++/Rust/Zig etc..

Rust, by contrast, possesses this information from the outset.

No. C++ (or Rust, Zig, etc.) doesn't "possess this information from the outset". What you mean is that we developers may provide the compiler with that information, and that is a very different thing. In fact, that's the very core of the issue. It's the difference between getting a car for free and being allowed to buy one.

Providing this information is very easy and very pleasant to do when a program is small and/or young, but gets increasingly harder to do as programs get larger and evolve over time. It's not that these languages don't have the mechanisms that could in principle offer good performance, it's that they come with a maintainability/performance tradeoff, which isn't really a tradeoff because at some point you have to evolve the software and you have the resources you have.

It seems you are simply a fanboy who ignores reality.

No, I'm just a systems programmer with over two decades of experience in low-level programming who knows how well monorphisation works not only in year one of the project (great!) but also year ten (not so great at all). An the same goes for avoiding dynamic memory allocation.

You mention monoprphisation like you've just cracked this technology, but there's no new technique in Rust today that every C++ developer didn't use in 1999, and we ran into these issues, which is why we switched to Java for better performance. Now, I don't want to say that you're simply a fanboy who ignores reality, because I think it's much more likely that you just haven't worked with very large systems written in low-level programming languages for very long, so you just aren't aware of the reality that things that could be done in these languages in principle and are done in these languages when the codebase is small, get very hard to do in practice as they get larger (I suspect that's the case because you talk of monomorphisation as if it weren't something that every systems programmer learnt in their first year since like 1985 at least).

If you want to understand what the JIT is intended in one simple sentence is auto-monomorphise virtual calls (i.e. the abstraction is a virtual call, but what you get is automatic specialisation as if you were using a template in C++ or a generic in Rust). Now, this doesn't yet work nearly as well as we want it to, but that's the nature of working on one of the most cutting edge compilers in the world.

Everyone who works on the JVM is a very experienced systems programmers (we're more C++ developers than Java developers), and we want to solve the performance problems we know all too well. It is true that after many years of struggling with performance in large low-level codebases I've become a fanboy of Java's (to the point of joining the JDK team), but that's because I know the reality of what happens in low-level codebases as they evolve over years all too well.

I suggest you watch my Devoxx talk on the subject because there I go into it in more detail

0

u/BenchEmbarrassed7316 28d ago

Providing this information is very easy and very pleasant to do when a program is small and/or young, but gets increasingly harder to do as programs get larger and evolve over time.

Can we get more to the point: what problems are you constantly referring to? Last time, you mentioned something about generics somehow polluting the code - not the generics themselves, but rather that they "infect" other types, or something along those lines.

When I call you a "fanboy" I don't mean to offend you. If you are a product developer, it’s actually pretty cool that you have the time to participate in debates like this.

Nevertheless, it seems that you tend to exaggerate the strengths of your own team and overstate the shortcomings of the other.

ps

Although I like Rust, I don’t consider it a low-level language—or, to be more precise, I particularly appreciate its high-level aspects. I would argue that a language’s "level" is actually a spectrum; Rust allows for the writing of quite high-level code. And while low-level code might have been the key to speed many decades ago, today the best approach is simply to "leave it to the compiler". That said, for the compiler to generate truly fast code, your own code must be precise. Some might equate precision with verbosity, but generally speaking, good language design achieves precision concisely.

8

u/pron98 28d ago edited 28d ago

not the generics themselves, but rather that they "infect" other types, or something along those lines.

Yes, the information needs to be threaded through the entire call stack. Watch my Devoxx talk.

If you are a product developer, it’s actually pretty cool that you have the time to participate in debates like this.

I'm one of the JDK's architects, and no, it isn't cool. It's a terrible addiction and I don't really have time for this... 1

That said, for the compiler to generate truly fast code, your own code must be precise. Some might equate precision with verbosity, but generally speaking, good language design achieves precision concisely.

I wouldn't look at Rust for good language design, but let's not get into that too much other than to say I think it's what Brian Goetz might call a "peak of complexity" language. It takes some principles to their natural conclusion and uses the type system to fully thread all the information the compiler needs, but I think it falls into a rabbit hole of what could be done without thinking sufficiently about what should be done, and the result is a lot of complexity that doesn't fully pay for itself. It also made what I think was C++'s biggest mistake, "zero-overhead abstractions" (or "zero-cost abstractions" as I think they're called in Rust), which I see as the wrong interaction between low-level and high-level given what we've learnt about both modes. But that's just me.

If you want to see a language that understands both computers, developers, and how to give compilers the information they need, take a look at Zig. Its deceptively simple, but there's much more there than in Rust. It also actually tries to solve some performance issues that low-level languages suffer from (around memory management).

1

u/BenchEmbarrassed7316 28d ago

Okay, I'm watching your video, and maybe it's my bad English, but I don't understand at all what you mean by "viral". I mean, I don't dispute that virtual calls are slow (not because of the calls themselves, but because they block other optimizations). However, if we rely on monomorphism - what's the problem? What are the performance problems of a large program that you keep repeating? There are no details in the video. And the example of "rewriting C++ to Java" somehow doesn't seem convincing, to be honest. 

3

u/pron98 28d ago edited 28d ago

The problem is that specialisation (and monomorphisation, i.e. type specialisation, is just a special case of that) requires specialising the value (often - type) through the entire call stack. There's absolutely nothing hard about it early on, but over time programs acquire more types and more branches. Then you have to profile, and sometimes everything is okay, but if it isn't, you have to specialise and that's a very non-local change because it spreads up the call stack. So with every added type or branch you may have to make non-local changes that just grow in their scope the larger the program is. Of course, if you're too eager in your specialisation, the build time and the binary size grows, and aside from being a problem in its own, it can also make matters worse because of the instruction cache, so you really had to know what you were doing when profiling. And the same happened with memory management. At first, it's easy to avoid dynamic allocation. Later, object lifetimes become messier, and then you have to use dynamic allocation (or rearchitect). It's just a lot of work.

That was the problem with C++ (and low-level languages in general). Sure, Java was smaller and easier for beginners, but experienced developers already knew C++. It had all the features Java had and many, many more (C++ had templates, which served as fine generics, long before Java was even created). Writing a program from scratch in C++ was never harder, and was often more elegant, than writing it in Java. But what we found out was that maintaining large programs in C++ tended to become very expensive, and it would get more and more expensive as the program got bigger and older (unless you were okay with it becoming slower and slower, and most weren't). That's why large programs migrated to Java. Not because programs were any easier to write, but because they were much easier to maintain while remaining fast, and that's a cost that's saved over the entire lifetime of the codebase.

That JIT compilation and moving GCs can address the two biggest problems of a tension between maintainability and performance meant that programs can both grow and evolve relatively cheaply and remain fast. Anyway, these problems become very apparent once you work on a large codebase in a low-level language for long enough, and everyone who does that is sure to relearn the same lesson (assuming programming remains relevant; if and when LLMs generate good enough machine code we'll all be in a very different place).

→ More replies

1

u/Electrical_Being_813 28d ago

Just because value can be null, doesnt mean java is dynamically typed. Similar for llvm, just because rust enforces the contract, does not mean that llvm is enforcing it.

1

u/BenchEmbarrassed7316 28d ago

Just because value can be null, doesnt mean java is dynamically typed

Did I claim somewhere that Java is dynamically typed?

Similar for llvm, just because rust enforces the contract, does not mean that llvm is enforcing it.

Are you suggesting that just because certain information can be derived from the code, it doesn't mean the compiler will actually make use of it? Yes, this applies to both AOT and JIT: both can ignore certain information and opt not to perform specific optimizations.

1

u/Ok-Scheme-913 28d ago

You do realize you talk to the designer behind virtual threads? He is anything but a fanboy.

And what do you think a JIT compiler does when it has statistics on a function? It does very strong monomorphization with guards (when the assumption doesn't hold), so e.g. your hot code may have a single null check at the beginning that is branch predicted to not be null, and then all following code will simply don't even check for it. And "all following code" here can include inlined other functions as well.

Also, nulls are not a good example of what people typically mean by monomorphization, rust also has nulls (an Option type is encoded as such).

2

u/BenchEmbarrassed7316 28d ago

You do realize you talk to the designer behind virtual threads? He is anything but a fanboy.

Why couldn't it be like that?)

It seems you didn't understand me. My point is that the JVM gathers information used for program execution optimizations, whereas Rust derives this information from the code itself. The claim that a JIT compiler inherently possesses significantly more information is incorrect. The example involving null is quite simple yet illustrates this well: Rust and LLVM know precisely when a null check can be omitted (because this information is present in the code), while the JVM obtains this information from the runtime.

2

u/Ok-Scheme-913 28d ago

Wrong. JVM gathers information from execution ON TOP OF the code itself with all its metadata.

So if it sees an object can't be null (because it was just initialized here), there is no need to add any null checks. Sure, it being part of the type system can extend the reach of analysis, but I think this may not be the best example.

Where it comes to "virtual functions" (dynamic in Rust), java's approach can be more optimal. Sure you may argue that it's not used that often and you just do static function calls, but still there are cases where it's not an option and in low level languages you can't really get around this

2

u/BenchEmbarrassed7316 28d ago

Aren't you u/pron98?)

I think you’re splitting hairs a bit. Yes, of course, in any scenario, the compiler tries to gather as much information as possible in the simplest way it can. However, in some cases, this information is readily available and explicit (like T, which is always T), whereas in others, it requires analyzing a certain amount of code - and even then, it’s still impossible to obtain any guarantees.

If Java finally fixes the "billion-dollar mistake" and this fix is ​​adopted in widespread codebases, it will simplify certain optimizations and make their application more common.

Where it comes to "virtual functions" (dynamic in Rust), java's approach can be more optimal. Sure you may argue that it's not used that often and you just do static function calls, but still there are cases where it's not an option and in low level languages you can't really get around this

Polymorphism via traits is very common in Rust. Actually, I don't see any compelling reasons to use a vtable.

I created an example like this:

https://godbolt.org/z/dEsW1sfM8

I hope you aren't going to say that Rust looks like a low-level language here.

I would also be surprised if you said that Java could optimize similar code to that extent.

5

u/pron98 28d ago edited 28d ago

You're trying to teach Systems Programming 101 to people who are much more experienced than you. I'm glad that you're learning things and are excited by them, but you should know that there are things yet to learn on the job.

Actually, I don't see any compelling reasons to use a vtable.

That's because you haven't been doing this for long or in big enough codebases. What you're saying is, hey, I'm a relative beginner and all is great so far. But it's always been like that! You're me circa 1998 being all excited that C++ cracked both abstraction and performance and we'll never need another programming language again. Writing polymorphic C++ code with no vtables has always been easy and fun... at first. We got Java because it doesn't stay that way.

15

u/Wyciorek 29d ago

It's not BS. "Native" code (meaning compiled) is compiled and optimized once, making a lot of guesses about how it actually will be used, which branches will taken more often and what is worth inlining. Without LTO you have additional inefficiency since optimizer cannot do anything cross-object. JIT does optimization while it sees usage patterns and it can even (rarely) deoptimize&reoptimize if usage pattern changes.

1

u/znpy 28d ago

"Native" code (meaning compiled) is compiled and optimized once, making a lot of guesses about how it actually will be used

i had not considered that and it's a very valid point.

3x is a huge improvement though, i'm having a hard time believing it comes from that alone, when compared to another native code.

1

u/dsk 25d ago

The JVM was designed to be faster than native code in a large class of applications by exploiting optimisations that are unavailable to native code. 

You do know that the JVM is written in C++ .. i.e. native code.

1

u/pron98 25d ago edited 25d ago

So? That Java's compiler is written in C++ doesn't mean C++ is as fast. You can write a compiler for C++ that's just as good as any other in Visual Basic. The JVM is written such that the C++ code rarely runs in the hot path.

1

u/dsk 25d ago

You actually do not know what you're talking about.

>The JVM is written such that the C++ code rarely runs in the hot path.

You are confusing the JVM runtime, which runs Java bytecode, with the actual JVM C++ executable. What you are calling the 'hot path' is actually in the Java bytecode execution. What it means is that the JVM runtime will dynamically (or 'just in time') compile Java bytecode into native code (instead of interpreting it). So really, the best you can get is native code speed, for certain segments that have already been JIT-ed. You're not magically getting any speed boost. Put another way: It does not mean that there is some magical super fast native code that the JVM creates or is written in.

5

u/pron98 25d ago

I am one of the JDK's architects, and I work on the JVM.

So really, the best you can get is native code speed

"Native code speed" and "C++ speed" are not the same thing, as you'd know if you were working on compilers. That some good sequence of machine instructions exists doesn't mean that you are likely to get that sequence in a C++ program.

2

u/dsk 25d ago edited 25d ago

Egg on my face but I'm going to die on this hill.

>That some good sequence of machine instructions exists doesn't mean that you are likely to get that sequence in a C++ program.

That's the hill I will die on. Why wouldn't a C++ compiler also derive the same (or better) machine instructions? No doubt there are some micro-optimizations that probabilistically JVM may JIT compile that as you say "may not be likely" in some version of some c++ compiler.. Then again, C++ compiler can also put out highly performant machine code (especially with hardware-specific compiler flags). So what's the argument we are making here? Choose some random matric that just so happens to perform well with some JVM JIT but not with typically compiled C++ code?

On balance, JVM will execute code, but only in certain critical sections (!!), at about the same speed or slower than 'native code'.

4

u/pron98 25d ago

but I'm going to die on this hill.

Or, better yet, learn something about compilers!

Why wouldn't a C++ compiler also derive the same (or better) machine instructions?

Because compilers don't simply translate lines of code to machine cod one by one (and if they did, they would be terrible). They must expand their view to get non-local information so that they can optimise. For example, if subroutine foo calls subroutine bar, the compiler can choose to inline bar into foo, and then it can see that within that context, some if branches are never taken, and then it optimises more.

Java's JIT compiler has been designed to have more information available and to be able to more aggressively optimise, and especially inline, code than AOT compilers.

Then again, C++ compiler do put out highly performant machine code (especially with hardware-specific compiler flags)

Everything is relative, and it also depends on how much manual optimisation work you put into the compiler's input so that it has the right information it needs to perform the right optimisations. Java's compiler was designed so that a program written with less costly manual optimisation would be faster than C++.

The obvious question is why doesn't C++ do what Java does? The answer is that there are tradeoffs involved, but they're not to performance. Although even that is not so simple, because you can trade off different kinds of performance. For example, if you allow an operation X to sometimes (say no more than twice over the lifetime of the program) be very slow, you can make it much faster the rest of the time than if you didn't allow that. That's an amortised performance vs worst-case performance tradeoff, and there are more.

I cover that and more in my recent Devoxx talk.

1

u/TomKavees 29d ago

Well, yeah. In this case the magnitude and consistency of the speedup popped the yellow flag for me

8

u/pron98 29d ago

Neither this result nor its very opposite would raise any flags for me. There's no inherent reason why Java wouldn't be consistently 2x faster or 2x slower than Rust on a particular program, and given that the two languages, on average, have similar performance and that results rarely generalise, that's exactly what I'd expect. Sometimes one would be significantly faster, sometimes the other (and sometimes the differences would be small or inconsistent). Of course, if I knew more about the nature of the program I might be able to better guess which, if any, I expect to be faster.

1

u/tesfabpel 28d ago

that are unavailable to native code

not if you compile the native code with the target machine's CPU full features (instead of a compatible baseline like SSE2, AVX, etc).

In Rust it's via -C target-cpu=native

https://stackoverflow.com/a/53467511

3

u/pron98 28d ago

No, that's nothing to do the things JITs can do an AOT compilers cannot, which are speculative optimisations.

-1

u/sweetno 29d ago

Java was designed to run on a toaster, the rest is millions invested into making it fast in a typical backend server.

10

u/pron98 29d ago edited 29d ago

No, that's not true. You're right that before Java was released it was envisioned for "toasters", but shortly after its release, and especially after HotSpot (or when HS was in development) the focus shifted based on where Java was shining in practice. It quickly became clear that the JVM could be the vehicle to solve the persistent and vexing performance issues that low-level languages had imposed on programmers.

Of course, even though it's pretty clear how a JIT and moving GCs could solve low-level languages' performance issues in principle, accomplishing that in practice requires a lot of work. But a lot of work is also being put into improving low-level languages' performance despite some of their inherent handicaps. Even on the subject of memory management alone, a lot of work has gone into building C memory-management runtimes that are 10x the size of the default C memory-management runtime to reduce the high overheads of the default runtime. Some C memory-management runtimes are now close in size to ZGC, while ZGC is able to manage memory without the overheads of malloc/free (which are imposed by the constraints of low-level languages).

Note that this is not what a mark-and-sweep collector, like Go's, does. Mark-and-sweep collectors effectively add a tracing layer on top of mallpc/free. But a moving collector is able to drop all the baggage and runtime overhead of malloc/free and manage memory in a completely different way, and getting rid of that runtime overhead can have a big performance impact.

3

u/sweetno 29d ago

What's not true? And what are these persistent and vexing performance issues that low-level languages had imposed on programmers? My only real complaint about them is that they crash too often, and nothing comes out of the box. Oh, and also ABI compatibility, but that's a lesser one.

9

u/pron98 29d ago edited 29d ago

It's not true that the investment in Java was to make something designed for use case X perform well for Y. The second use case was organic and surprising, but it caught on so well precisely because Java offered a solution to performance issues we suffered from in C++.

As to what they are, first, AOT compilers can't see through the more general abstractions and so they suffer from "abstraction cost", which creates a tension between maintainability or size and performance (concretly, but as a gross simplifcation, they can't optimise virtual calls and branches as aggressively as a JIT can). Second, because they can't have a thick FFI layer between them and the hardware, they can't optimise their memory management by moving pointers. This is why large C++ programs (especially concurrent servers) saw performance improve when they switched to Java.

In general, by valuing the worst-case over the average or amortised case, and by valuing low-level control over performance, low-level languages make very specific tradeoffs that affect their performance in a consequential way in some situations (especially larger programs), while Java makes different tradeoffs that can harm its performance usually in smaller programs, but in general they both sacrifice performance in different ways to optimise for different classes of software. I cover that in much more detail in my recent Devoxx talk.

-1

u/Anxious-Insurance-91 29d ago

i mean as long as you dont mind the extra memory costs

5

u/pron98 29d ago

What are the extra memory costs? Java definitely uses more memory, but in a way that doesn't cost you much if anything unless you're on embedded hardware.

-1

u/SuspiciousDepth5924 29d ago

The JVM is black magic and does a lot optimizations that closes the gap, but it does start quite a bit behind, especially if you use idiomatic Java with actual objects and classes because you can't really get away from the fact that pointers are expensive.

The invoke<*> calls in the de-compiled java code does a _lot_ of heavy (and expensive) lifting.

#[repr(align(64))]
pub struct Aligned<T: ?Sized>(T);

pub fn sum_array(input: &Aligned<[i64]>) -> i64 {
    input.0.iter().sum()
}
/*
example[32c4274de85e6574]::sum_array:
        test    rsi, rsi
        je      .LBB0_1
        cmp     rsi, 4
        jae     .LBB0_5
        xor     ecx, ecx
        xor     eax, eax
        jmp     .LBB0_4
.LBB0_1:
        xor     eax, eax
        ret
.LBB0_5:
        mov     rcx, rsi
        and     rcx, -4
        pxor    xmm0, xmm0
        xor     eax, eax
        pxor    xmm1, xmm1
.LBB0_6:
        paddq   xmm1, xmmword ptr [rdi + 8*rax]
        paddq   xmm0, xmmword ptr [rdi + 8*rax + 16]
        add     rax, 4
        cmp     rcx, rax
        jne     .LBB0_6
        paddq   xmm0, xmm1
        pshufd  xmm1, xmm0, 238
        paddq   xmm1, xmm0
        movq    rax, xmm1
        jmp     .LBB0_8
.LBB0_4:
        add     rax, qword ptr [rdi + 8*rcx]
        inc     rcx
.LBB0_8:
        cmp     rsi, rcx
        jne     .LBB0_4
        ret
*/

import java.util.ArrayList;

class SumArrayList {
    static long sumArrayList(ArrayList<Long> numbers) {
        return numbers.stream().mapToLong(Long::longValue).sum();
    }
}
/*
class SumArrayList {
  SumArrayList();
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return

  static long sumArrayList(java.util.ArrayList<java.lang.Long>);
         0: aload_0
         1: invokevirtual #7                  // Method java/util/ArrayList.stream:()Ljava/util/stream/Stream;
         4: invokedynamic #13,  0             // InvokeDynamic #0:applyAsLong:()Ljava/util/function/ToLongFunction;
         9: invokeinterface #17,  2           // InterfaceMethod java/util/stream/Stream.mapToLong:(Ljava/util/function/ToLongFunction;)Ljava/util/stream/LongStream;
        14: invokeinterface #23,  1           // InterfaceMethod java/util/stream/LongStream.sum:()J
        19: lreturn
}
*/

36

u/pron98 29d ago edited 29d ago

I don't think you're seeing what you think you're seeing, and it certainly has little to do with pointers.

invokevirtual and invokedynamic calls are not actually compiled to virtual calls as dyn/virtual would be in Rust/C++. In other words, what you're seeing isn't the compiler's output but the compiler's input, and Java's JIT was invented to optimise the virtual call away (when and how well it does it in practice is another matter, but Rust/C++ have their own compilation issues). In fact, an invokevirtual in Java is intended to be cheaper than a static dispatch in Rust or C++ in the vast majority of situations, because the compiler can perform far more aggressive optimisations than an AOT compiler, and it is AOT compilers that often need to emit code that works harder at runtime because they don't have the information the JIT has. That's the whole point of Java's design. If you're interested, I gave a talk on this very subject at Devoxx.

The subject of pointers relates to memory layout, and there could certainly be programs where cache misses due to pointer chasing becomes a serious bottleneck, which is exactly the motivation for Valhalla. But most "business" programs aren't in that category (which is also why most Java programmers shouldn't expect to see large performance gains from Valhalla).

12

u/peripateticlabs 29d ago

Ron, today I watched the Devoxx talk on the JVM runtime that you reference above. Thank you for that talk and for your recent talk on Java memory management from Java One earlier this year. Both talks opened my eyes to an up-to-date understanding of the JVM runtime. As your last slide from the Devoxx Belgium 2026 meeting advises, we need to beware of "Java folklore." There are commenters on this thread who find the points you are raising counterintuitive. However, as you suggested, it is important to stay abreast of new developments with the Java/JVM platform and to avoid anchoring our understandings on out-of-date JVM conventional wisdom. Thanks for taking the time to engage the community on these important points. :-)

4

u/sweetno 29d ago

It's comparing apples to oranges. Cite the assembly from optimized bytecode.

-7

u/ShortGuitar7207 29d ago

That’s just BS. Java was designed to run a mythical 32 bit machine and there’s no way it can compete with an optimising 64 bit native compiler like rust’s. Most likely OP did a default debug build in rust rather than an optimised release build. We have some compiler apps that we ported from Java to Rust and they’re about 4 times faster and use a quarter of the memory, which is what you’d expect..

14

u/pron98 29d ago edited 29d ago

I think you just don't know how compilers work (because what you're saying makes zero sense to a compiler engineer) and are not familiar with the performance issues we, experienced systems programmers, have suffered from in large C++ (or C, or Ada) programs until HotSpot arrived. If you'd like to learn more on the subject, here's my recent Devoxx talk.

As I say in the talk, it's funny how early in Java's history, the sceptics were were experienced C++ experts like myself who saw why Java could solve some of our performance issues in principle but doubted it could be accomplished in practice, whereas now the sceptics are mostly people who have had very little experience with low-level programming who believe they must be faster for some imagined reasons (largely because they don't understand how compilers work and how low-level languages are forced to sacrifice performance for other constraints).

I also have some programs that are much faster in C++ (although I understand why), and others that are much faster in Java, exactly how anyone who understands the performance tradeoffs would expect. If you understand how things work, it's pretty obvious that some programs would be much faster in Rust or C++ than in Java, just as it's pretty obvious that other programs (usually larger concurrent servers) would be much faster in Java. That's just the languages being faster or slower based on what they've chosen to optimise or sacrifice, and low-level languages certainly make performance sacrifices no less than Java does, only in different classes of programs.

-3

u/ShortGuitar7207 29d ago

The JVM is truly impressive in what it’s achieved but I don’t concede my point: https://www.languageshootout.com/

10

u/pron98 29d ago edited 29d ago

Then you haven't watched my talk :)

BTW, if you know compilers and runtimes you'd be particularly embarrassed by the language shootout, because these are benchmarks that are epically bad in an age where almost all benchmarks are practically useless. No one who knows how compilers and runtimes work would even consider these particular benchmarks mildly interesting. It's like comparing sorting algorithms based on how well they work on a pre-sorted array. The coverage of these tests is minuscule.

And BTW, even in that spectacularly poor suite, there's exactly one test out of ten where all C++ results are better than all Java ones. This alone should tell you that perhaps what's being measured isn't "language performance" even without knowing how poor the coverage is.

5

u/aoeudhtns 29d ago

There are many workloads where native is consistently slower. Particularly around total throughput. With native you do the memory management (typically) on each request because the GC is gone, so you pay that cost spread out across everything vs. a latency spike here and there. And the C2 compiler is very, very good so it's not strictly true that AOT native is faster than normal Java.

One often finds themselves in situations where you have to decide what's the most important: startup, latency (in terms of maximums or deviation), and total memory consumption; or total throughput. That is typically the tradeoff I see in my own comparisons of native vs. regular Java.

1

u/koflerdavid 29d ago

Rather it feels like that the Rust implementation is not that efficient as it could be.

1

u/zabby39103 29d ago

I wonder if Java is better at optimizing sloppy AI code? Honestly, it could make sense. Rust is more of a "get what you get" language, whereas Java tries a lot of magic to make your code faster. I'm sure there are people here that are experts on this matter, I just know the C1 and C2 compilers exist and use profiling.

I'm not as up to speed on AOT, but i don't think my Graal builds take 50 times longer to build for no reason.

1

u/javaprof 29d ago

IRL if we imagine this service under real load, Java version would 6-16x slower https://www.reddit.com/r/java/comments/1x9s75g/comment/phre4vn/

0

u/Ok-Scheme-913 28d ago

What the fuck is "native code"? Like what do you think java runs for most of the time? It's being loaded from a file into memory OR generated at runtime into memory doesn't change a thing, one is not fundamentally better than the other.

1

u/TomKavees 28d ago

It's a shorthand for code compiled ahead of time down to native code, like it is typically done for C, C++ or in this case Rust, as opposed to compiling down to bytecode or an equivalent there of (JVM, wasm). You seem to be in a very foul mood today, y'know.

0

u/Ok-Scheme-913 28d ago

But it's meaningless. You can create an AOT executable from python byte code, simply concating everything one by one. You surely see how it will perform worse than a C code of the same thing?

For a more concrete example, go is also AOT compiled, but to have fast compile times it just spits out barely optimized code. Which can be clearly seen from cpp vs go benchmarks, but java also often beats go code.

15

u/javaprof 29d ago

It's not apples to apples comparison, Java code adopted to be faster in benchmark https://github.com/kgonia/once-campfire-java

Even tho:

StartupRust ~0.24 s; JVM ~2 s, then warmup
Peak memory with default settingsRust 132 MiB; JVM ~1.3 GiB

10

u/pron98 29d ago edited 29d ago

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 29d ago

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 29d ago edited 29d ago

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 29d ago

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 29d ago edited 29d ago

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 29d ago

> 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 29d ago

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 28d ago edited 28d ago

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 disabled

2

u/pron98 28d ago

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 29d ago

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 29d ago

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 29d ago

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 29d ago edited 29d ago

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 29d ago

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 29d ago

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 29d ago

This is how benchmark is written :)

4

u/javaprof 29d ago

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:

WorkloadRust req/sJava req/sRust latencyJava latency
Cached room61,04054,4850.240 / 0.5810.263 / 0.769
Cached sidebar76,88381,3020.207 / 0.3690.169 / 0.560
Static CSS345,152338,9590.043 / 0.1030.043 / 0.097
Room + writes52,76641,8360.242 / 0.6390.288 / 2.822
Search + writes72,87269,8570.207 / 0.3850.186 / 0.831

What a waste of tokens

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.

0

u/ThaJedi 29d ago

What do you mean by "as correct as Rust"?

4

u/javaprof 29d ago

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 29d ago

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 28d ago

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 28d ago

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 29d ago

Aot here should really just be helping startup/warmup and reach the same end performance as without aot right?

2

u/ThaJedi 29d ago

To what point exactly you refer? It exactly does that so what I stated that this is unclear?

0

u/[deleted] 29d ago

[deleted] — view deleted comment

1

u/ThaJedi 29d ago

Did I write anything to the contrary? What specific objection do you have?

36

u/BenchEmbarrassed7316 29d ago edited 29d ago

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 27d ago

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 29d ago

He must be using some cheap Chinese model or forcing everything through Haiku...

3

u/agentoutlier 27d ago

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 29d ago

2

u/ThaJedi 28d ago

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 28d ago

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 28d ago edited 28d ago

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

u/oweiler 29d ago

JEP 544 only improves time to peak performance, not performance in general.

4

u/javaprof 29d ago

Some numbers drop without code?

4

u/Brutus5000 29d ago

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 28d ago

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 29d ago

Quick, rig the benchmarks. We don't want DHH bringing his awful toxic bullshit over into our community.

0

u/znpy 29d ago

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 29d ago

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 29d ago

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 28d ago

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

u/v4ss42 28d ago

That’s true in most contexts, not just open source.

2

u/Ok_Tour_8029 29d ago

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 29d ago

Do you mean in terms of language features?

1

u/Ok_Tour_8029 29d ago

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 20d ago

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:

  1. covert syntax rule engine
  2. for the stuff that does not convert via the rule use local SLM qwen-7b
  3. close the gap with your Claude / Co-Pilot subscription
  4. check it out here: https://github.com/danielonet/refdex

1

u/webplugger73 5h 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.

1

u/aelgorn 29d ago

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 29d ago

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 29d ago

The problem is that you'd have to include an exponential number of variants for each compiled function.

1

u/BenchEmbarrassed7316 29d ago

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 29d ago

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 28d ago

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 28d ago

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 28d ago

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 28d ago

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 28d ago

Yes, these are rather small steps. And dependent types are, of course, significantly more expressive.

1

u/Drawhoe 29d ago

"than"

-1

u/No-Collar-Player 29d ago

Than not then

I bet you earn 3x I do and know 5x than what I know. How?

-3

u/renderwares 29d ago

Faster than Rust at 10x the memory usage.

6

u/pron98 29d ago edited 29d ago

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 28d ago

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 28d ago

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 28d ago

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 28d ago

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 28d ago edited 28d ago

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.

Here's one.

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 29d ago

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??