Long.bitCount micro-optimization

Joseph D. Darcy joe.darcy at oracle.com
Tue May 9 00:07:24 UTC 2017

On 5/8/2017 1:29 PM, Doug Lea wrote:
> On 05/08/2017 02:14 PM, Isaac Levy wrote:
>> Original message below:
>> The JDK impl of bitCount can be improved -- though most users will get
>> the hotspot intrinsic. The best source I could find for the suggestion
>> below is page 195: http://support.amd.com/techdocs/25112.pdf
> The int version differs from current implementation in that it uses
> one multiply instead of two shift+adds. (Similarly for long.)
> I wonder if there is any processor that does not already have a
> bit-count instruction for which this is actually faster?
> Some evidence either way would be helpful.

This is a case of "jmh results or it isn't faster." [1]

It is challenging to evaluate such changes as being universally faster 
without benchmark results, especially for small methods like this. 
Without compelling performance support, my preference would be to leave 
the current Java implementation as-is, especially when there are VM 
intrinsics on many platforms.



[1] http://openjdk.java.net/projects/code-tools/jmh/

More information about the core-libs-dev mailing list