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.
I disagree that a JIT is needed to optimize a simple byte manipulation.
But that's my point -- you are thinking way too small scale.
It's not just the method, but the code calling it -- application code.
If you have one method doing byte manipulation amongst many other operations doing more byte manipulation, the JIT can try and find a specialized operation that would do the both in combination.
That is what JIT Compilation is good at -- taking 2 almost unrelated pieces of code that, when called in-tandem many times in a row, optimizes them into something even faster.
The fact that one of the methods in question is a simple byte manipulation method is irrelevant to whether or not savings could be found.
That is what JIT Compilation is good at -- taking 2 almost unrelated pieces of code that, when called in-tandem many times in a row, optimizes them into something even faster.
Any optimizing compiler works this way, whether it's JIT or AOT. The difference is that in some languages, you have to gather statistics and make assumptions to perform devirtualization, while in other languages, it's known right away.
Here is a simple example:
result = (a + b) / a.foo();
There are many optimizations that can be done here. If the compiler knows which foo will be called, it can check whether it can return 0, 1, or any other number. Also, if the compiler knows that a and b are constants or their values are known in advance, it can perform more optimizations (for example, if this calculation is guaranteed to be zero, there is no point in performing the div operation).
Now, if you are using a JIT compiler, it would be wasteful to check the left-hand side of the expression (a + b) at every optimization and deoptimization. It is much simpler to have some intermediate representation of these instructions, which could be created either in AOT mode when compiling the sources into bytecode, or at program startup, or on first access.
I believe that byte manipulation when decoding base64 is a fairly simple operation, which is confirmed by the fact that they were able to describe it in assembly language at all.
So the fact that they used ASM instead of some intermediate representation makes me doubt the effectiveness of this intermediate representation, to the extent that it allows generating high-quality code that would be "almost like C".
Now, if you are using a JIT compiler, it would be wasteful to check the left-hand side of the expression (a + b) at every optimization and deoptimization.
Again, you are thinking way too small scale.
The reason why it makes sense to check that on a regular basis (maybe not everytime) is that programs change over time, and the needs on start or first access are not necessarily the same for peak times.
Again, I encourage you to think much larger scale. You're looking at this as an isolated problem when it is actually a tiny piece of a much larger program.
Yes, optimizing individual components tends to result in optimizing the larger program, and that's not a wrong approach to take.
But you are wrong to claim things like it is wasteful to repeatedly check when the JIT has been demonstrating optimizations for use cases like this for nearly a decade. Just because it is wasteful in certain contexts, doesn't mean that applies for this one.
Exactly. Because my initial comment concerned code in which the runtime environment authors had literally hardcoded some operations using ASM.
is that programs change over time
Then its are usually rebuilt and restarted. Theoretically, JIT enables hot reloading, but this comes at a cost. If you need to constantly verify everything - including constants or final values - you will lose some performance.
In general, unlike me, you perceive the JIT/JVM as something "magical". Just like a standard compiler, it takes input and produces machine code that needs to be executed. The difference is that some of the information it receives comes not from the code itself, but from a module that monitors the code's execution (which is, in fact, a separate module).
But you are wrong to claim things like it is wasteful to repeatedly check when the JIT has been demonstrating optimizations for use cases like this for nearly a decade.
This is 100% waste: there is no point in checking something that cannot change several times. In fact, this is ordinary caching. This would not be wasteful if checking is cheap or storing the information is expensive. However, for parsing the code, checking is definitely expensive, and storing the optimized version is even cheaper. Or if it can, it is not actually immutable, i.e. there are no constants. The fact that they hardcoded the asm instructions confirms my point: they cached part of the code to simply substitute it.
Exactly. Because my initial comment concerned code in which the runtime environment authors had literally hardcoded some operations using ASM.
The reason why it is in ASM is to communicate more information as well as skip some steps. Aka -- get to the destination faster.
But that doesn't mean that the compiled ASM is the destionation -- just a better starting point.
This is 100% waste: there is no point in checking something that cannot change several times.
Then you are not understanding what is happening here.
The JIT isn't testing to see if the same lines of code has changed. It is testing to see how much payoff it would get from performing a certain optimization, and that must be tested many many times over.
The fact that they hardcoded the asm instructions confirms my point: they cached part of the code to simply substitute it.
Again, not the end goal. The ASM is meant to be a better starting point, because static analysis can prove that it is just faster to run with the given ASM from the start rather than the not-compiled Java bytecode.
Again, I think you are misunderstanding the purpose of the ASM. The ASM is never meant to be a destination (or a blueprint of it). The ASM is meant to be a better starting point, since static analysis (and more) can prove that starting out with that ASM is pretty much always going to be better.
But that ASM and the profile of the program can be ingredients for the JIT to compile all of it down to something even more efficient. Therefore, not wasteful.
The reason why it is in ASM is to communicate more information as well as skip some steps.
Vice versa. Using high-level code provides more useful information that can be leveraged for optimization. Assembly is the final stop: reading bytes, writing bytes. The availability of information indicating that this is a specific loop - which operates on certain data located at various addresses - makes it possible to implement optimizations.
The JIT isn't testing to see if the same lines of code has changed. It is testing to see how much payoff it would get from performing a certain optimization, and that must be tested many many times over.
As far as I understand, JIT does not perform testing/benchmarking itself. It is guided by the general rules that some operations will be faster than others.
Look, any compiler converts code into a certain Abstract Syntax Tree. You say that for the best optimization result, you need to start at the root of this tree, that is, "check everything". I'm saying that certain branches can contain a cached version if the compiler can prove that this cached version will always be up-to-date. And instead of checking sub-branches, you can immediately refer to the cached version. Therefore, I argue that "check everything" makes no sense.
But that ASM and the profile of the program can be ingredients for the JIT to compile all of it down to something even more efficient.
I'll refer you to this article. It describes that even for C, optimization is difficult and you have to do a lot of calculations to convert C code into a more detailed representation and then generate low-level code. So I think ASM is a dead end, code that is just substituted in a certain place.
As far as I understand, JIT does not perform testing/benchmarking itself.
It literally does.
As in, it will create a possible optimization, test it many many times over, and if it fails, it will deoptimize that same code back to a previous stage. This is happening all the time for any Java program lasting longer than a couple of seconds.
And again, it is important to emphasize -- an optimization may prove to be useful in one moment, but no longer useful in another moment. Hence why there is constant testing. As service traffic ebbs and flows, the most optimal solution changes accordingly.
You say that for the best optimization result, you need to start at the root of this tree, that is, "check everything".
Yes, exactly.
I'm saying that certain branches can contain a cached version if the compiler can prove that this cached version will always be up-to-date.
But that's my point -- what is optimal now may not be optimal later.
Imagine a web service that has 2 endpoints -- endpointA and endpointB. Each endpoint maps to a method, and these are compute and memory intensive methods.
If traffic changes such that endpointA is being hit exorbitantly more than endpointB, then the JIT might literally optimize the code such that endpointA is further optimized at the expense of endpointB.
A good example of this is when running a for loop over lines of a file, to do some parsing, let's say. Since it is a for loop, the JIT can figure out pretty quickly that grabbing data ahead of time is to its benefit. And if that for loop is getting kicked off millions of times a minute, the JIT might start trying to load massive chunks of the file in memory at once to get the most performance benefit. Since more memory is being used, that would be at the expense of endpointB.
Again, your description about caching is only partially correct. Yes, caching the results means that, given the same context, that same problem does not need to be recomputed.
But since the context changes very frequently, the cache quickly becomes invalidated. Context being, how often a method is called, how long that method takes, how big the method is, etc. Those values change over time in a single program run (yes, even method size!), and thus, optimizations change over time too.
I'll refer you to this article.
I loosely skimmed it. Looks like a C program did some branch prediction, but the branch predicted was not the branch chosen. So, the result was thrown away, but before it was, some hackers who had access to the machine were able to view the branch misprediction before it got discarded. And thus, had access to the computed result. So it's just a matter of manipulating it to mispredict for the method whose internals you want to expose, let's say an encryption algorithm, then rinse and repeat until you have enough info to see what is going on.
So I think ASM is a dead end, code that is just substituted in a certain place.
Admittedly, I only skimmed, but from what little I read, the purpose of ASM in Java is not the same as in C.
ASM in Java is when we KNOW that the java generated bytecode will always be less efficient than the ASM alternative.
But that's not the same thing as saying that the ASM code will be faster than the JIT generated code. In fact, it usually is not, but that's a moving target, and thus, can't be hard coded in ahead of time.
It's only when we know ahead of time that certain ASM code will 99-100% of the time be more optimal than the default javac generated bytecode that we decide to make ASM hardcoded in.
View all comments
Show parent comment
3
u/BenchEmbarrassed7316 12d ago
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
iforswitchstatements 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.