RFR (S) 8136500: Integer/Long getChars and stringSize should be more idiomatic

Peter Levart peter.levart at gmail.com
Tue Nov 24 23:12:10 UTC 2015



On 11/24/2015 09:08 PM, Ulf wrote:
> Hi,
>
> Am 24.11.2015 um 18:27 schrieb Peter Levart:
>> On 11/24/2015 06:22 PM, Peter Levart wrote:
>>>
>>> .. for addressing DigitOnes and DigitTens it is ok and might be 
>>> faster, as you say.
>>>
>>> If DigitOnes and DigitTens are extended for 28 ignored zero slots, 
>>> it may save the upper bound check too.
> Another interesting tweak, thanks for the idea.
>
>> I think that the index of code idiomatism has reached the local 
>> maximum and is now falling ... ;-) 
> Good joke, hopefully some day we have unsigned integers in Java, so 
> such twiddling has an end.
>
> -Ulf
>
>

xkcd would probably illustrate it like this:


Quest for Idiomatic Code
(A play in at least N*3 acts)


act 1 - discovery...

This code is 20 years old and not very idiomatic because compilers of 
that time did not know how to optimize things....

int q = i / 100;
int r = i - ((q << 6) + (q << 5) + (q << 2));
buf[--charPos] = DigitOnes[r];
buf[--charPos] = DigitTens[r];


act 2 - let's do it!

That's nicer and runs even a little faster than before...

int q = i / 100;
int r = i - 100 * q;
buf[--charPos] = DigitOnes[r];
buf[--charPos] = DigitTens[r];


act 3 - is that all we can squeeze out?

Byte addressing opcodes are even faster and we can eliminate negative 
bounds check...

int q = i / 100;
byte r = (byte)((i - 100 * q) & 0x7F);
buf[--charPos] = DigitOnes[r];
buf[--charPos] = DigitTens[r];


act 4 - 20 years later - (re)discovery...

This code is 20 years old and not very idiomatic because compilers of 
that time did not know how to optimize things....

int q = i / 100;
byte r = (byte)((i - 100 * q) & 0x7F);
buf[--charPos] = DigitOnes[r];
buf[--charPos] = DigitTens[r];


act 5 - let's do it!

That's nicer and runs even a little faster than before...

int q = i / 100;
int r = i - 100 * q;
buf[--charPos] = DigitOnes[r];
buf[--charPos] = DigitTens[r];


...




More information about the core-libs-dev mailing list