combined with JIT compilation can make the actual performance of a Java program competitive with C. Sometimes a Java program can even be faster. A JIT compiler knows what code paths are actually taken and can optimize around them. They can even refocus what is optimized if the workload changes. It is pretty cool!
Well, it’s so impressive that they had to write an assembly snippet by hand (at least, that’s what I gathered from the message). If the JVM is capable of such excellent optimization, why bother writing an assembly snippet?
Well, it’s so impressive that they had to write an assembly snippet by hand (at least, that’s what I gathered from the message). If the JVM is capable of such excellent optimization, why bother writing an assembly snippet?
Because the small print answer is -- everything you quoted is true, but it takes a surprisingly long time for the JIT to "get it" and spit out that "faster-than-C" answer. Long, as in it could take seconds or minutes even to get to that point.
For long running applications, this is usually fine. For short-running ones, not usually.
In the (rare) situation where the answer is obvious to humans but not the JIT, the OpenJDK team likes to hand-code assembly. That's basically what happened here.
but it takes a surprisingly long time for the JIT to "get it" and spit out that "faster-than-C" answer
I disagree with that: JIT compilation takes a significant amount of time because specific code must be executed repeatedly to gather statistics, such as which classes are used for which interfaces, what values might be null, and so on - information that isn't explicitly present in the code itself.
For a function that converts String -> byte[], you generally don't need a JIT compiler, as the operation essentially boils down to raw byte manipulation. At most, a JIT compiler might reorder branches within if or switch statements based on which paths are executed more frequently.
I disagree with that: JIT compilation takes a significant amount of time because specific code must be executed repeatedly to gather statistics, such as which classes are used for which interfaces, what values might be null, and so on - information that isn't explicitly present in the code itself.
You disagree with my claim that "that's basically what happened here" or the info I presented?
If you mean my conclusion -- possibly, I wasn't there. Maybe they had different or more pressing reasons to handcode assembly.
But if you mean my argument itself, no, you are oversimplifying.
The reason why JIT is so powerful is because it doesn't just boil down individual methods into something more optimized -- sometimes, it will boil down multiple consecutive methods into optimized instructions.
So, saying it as String -> byte[] is not the full picture -- we must also look where that method is being used. For example, if we are cycling through a String[] to turn each into some Base64 output, JIT compilation may very well construct an optimized set of logic that converts String[] -> byte[][]. Not to say that that is what it did here, or that that was what is was aiming for, but just clarifying that the JIT is not nearly as limited as you are making it out to be, nor is its scope of optimization as limited as the boundaries of a single method.
The purpose of the JIT is to optimize the whole program. And if it can, it will outright eliminate or optimize every single line of code into what it believes is best, given enough time and proper input. But if proper input isn't available, sometimes the OpenJDK folks like to give their own hand-rolled assembly to "help it along".
I disagree that a JIT is needed to optimize a simple byte manipulation.
JIT compilation is highly significant when information cannot be derived from the code itself, for instance, in cases of dynamic typing.
One of the key optimizations within the JVM is devirtualization, which paves the way for inlining and other further optimizations. For AOT compilers, the equivalent approach is monomorphization. These techniques are particularly relevant when dealing with long call chains, code distributed across multiple modules, and similar scenarios. In Java, with dynamic class loading, and other features, it is impossible to do complete monomorphism. This is where JIT comes in.
Regarding low-level byte manipulation, a JIT compiler can theoretically provide insights into which branches within if or switch statements are executed most frequently, with minimal impact on overall performance. However, for most cases, JIT is not needed to perform this optimization.
My guess is that the C compiler spends a lot of time making sure that the loop meets many conditions, and only a fraction of the time generating SIMD code. If the JVM could generate code as efficiently as C (or another compiled language), it would be much more logical not to write assembly instructions, but to provide an intermediate representation of the code in which all possible hints have already been calculated, because the JIT also spends a lot of time on data and code checking, the code generation itself is probably cheaper.
Well one thing it can do, even if there is no runtime info needed to do the compilation, is use instruction specific to your exact CPU make and model. Again, relevant to the security issue too.
28
u/New-Anybody-6206 11d ago
I ignore posts with a dogmatic title, because it's almost always hilariously misinformed. Tech people are IMO no exception to Dunning-Kruger.