r/programming • • 4d ago

Java4Shell is a reason to use Java

https://mccue.dev/pages/11-18-26-java4shell
47 Upvotes

50 comments sorted by

View all comments

Show parent comment

4

u/davidalayachew 4d ago

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.

0

u/BenchEmbarrassed7316 2d ago

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".

3

u/davidalayachew 2d ago

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.

1

u/BenchEmbarrassed7316 1d ago

Again, you are thinking way too small scale.

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.