RFR [7192942] Optimizing calculation of power of 2 in HashMap

Ivan Gerasimov ivan.gerasimov at oracle.com
Tue Jul 30 15:35:10 UTC 2013


Hello everybody!

HashTable needs to calculate the power of two that is not less that the 
given number.
This calculation used to be implemented as a relatively inefficient loop 
(that's why the issue 7192942 was created).
The implementation was improved with 
http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/2e3cc7f599ca

However it can be improved even more. I reused 7192942 number for this 
propose.

Would you please help with reviewing?
Here's the webrev: 
http://cr.openjdk.java.net/~igerasim/7192942/0/webrev/ 
<http://cr.openjdk.java.net/%7Eigerasim/7192942/0/webrev/>

I also created a benchmark test to make sure the improvement is observable.
Here's the jmh benchmark I used:
http://cr.openjdk.java.net/~igerasim/bench/powoftwo/src/main/java/org/benches/PowerOfTwoBench.java 
<http://cr.openjdk.java.net/%7Eigerasim/bench/powoftwo/src/main/java/org/benches/PowerOfTwoBench.java>

Benchmark                                Mode Thr    Cnt Sec         
Mean   Mean error    Units
o.b.PowerOfTwoBench.benchPowerOfTwoA     avgt   1     20 5       
60.697        0.850  msec/op
o.b.PowerOfTwoBench.benchPowerOfTwoB     avgt   1     20 5       
53.013        0.940  msec/op

The first line is for the current implementation and the second one is 
for the proposed change.
Thus, the time gets reduced by 12%.

Sincerely yours,
Ivan



More information about the core-libs-dev mailing list