Hello jdk devs!

I noticed that HashMap's impl of computeIfAbsent() is never inlined
which creates the unfortunate situation that clean code is less
efficient than the hand written "get(), if null, put()" chain. (Which is
also the default implementation of the Map interface)

Interestingly, the method used to be just under the limit, but an
additional guard was added which caused the regression (probably
https://bugs.openjdk.org/browse/JDK-8071667, can't access the hg patch
links atm).

Test branch which naively removes the guard as quick proof that it used
to inline with just a few bytes less weight:

https://github.com/mbien/jdk/tree/poc-cifa-noguard

Bumping via -XX:FreqInlineSize=335 (default: 325) works too, although
that value didn't change for a long time (if ever?).

gist of the JMH benchmark contains the full results in a comment since I
worry the mail will break formatting:

https://gist.github.com/mbien/df58d588747e540d3cae5b17f8414a5a

(btw CHM is way above the limit, HashMap slightly above, but all other
Map impls of that method seem to be below 325 B)

I am pretty sure the method could be split, but I thought to ask first
before opening a PR.

best regards,

michael

- - -

JDK 28 ~b26

Benchmark                                    Mode  Cnt   Score   Error 
Units
HashMapCIABenchmark.hitComputeIfAbsent       avgt    5  11.677 ± 0.405 
ns/op
HashMapCIABenchmark.hitHandWrittenGetFirst   avgt    5   4.384 ± 0.030 
ns/op

JDK 28 ~b26 -XX:FreqInlineSize=335
    
Benchmark                                    Mode  Cnt   Score   Error 
Units
HashMapCIABenchmark.hitComputeIfAbsent       avgt    5   4.642 ± 0.090 
ns/op
HashMapCIABenchmark.hitHandWrittenGetFirst   avgt    5   4.373 ± 0.068 
ns/op

Reply via email to