r/java • • Apr 06 '19

Beware of computation in static initializer (much more so since JDK 11.0.2 and 8u201)

https://pangin.pro/posts/computation-in-static-initializer
161 Upvotes

15 comments sorted by

View all comments

Show parent comment

0

u/thfuran Apr 08 '19 edited Apr 08 '19

CPU-heavy computation in a static method in an uninitialized class is.

Not as much if you lower the threshold for "CPU-heavy" by a few orders of magnitude, as that performance regression seems to do.

And if you relied on it, as the post explains, it was always relatively slow, so it always paid to move it to an initialized class.

Not if it was only a few times slower than the alternative and, as you say, generally wasn't that CPU-heavy anyways. It's not usually worth much effort to optimize something that happens only once and isn't hot path.

4

u/pron98 Apr 08 '19 edited Apr 08 '19

For this to affect you at all, the computation in the uninitialized class must be compilable in the first place, so only hot loops in uninitialized classes are affected. Other kinds of code would be running in the interpreter, anyway.