JDK-8136500 : Integer/Long getChars and stringSize should be more idiomatic
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 9
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2015-09-14
  • Updated: 2022-08-04
  • Resolved: 2015-11-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b96Fixed
Related Reports
Relates :  
Relates :  
Description
Current (Integer|Long).getChars are using the non-idiomatic constructs, possibly targeted to improve performance, see e.g.:

        for (;;) {
            q2 = (i2 * 52429) >>> (16+3);   // q2 = q2 / 10
            r = i2 - ((q2 << 3) + (q2 << 1));  // r = i2-(q2*10) ...
            buf[--charPos] = Integer.digits[r];
            i2 = q2;
            if (i2 == 0) break;
        }

This was apparently done before 1994 (estimate given by Josh Bloch), way before Java optimizing compilers could do strength-reduction on their own, and on some hardware manually replacing multiplications and divisions helped. It does not appear true anymore. Therefore, we need to see if we want a more idiomatic code there.

Sample benchmark:
 http://cr.openjdk.java.net/~shade/8136500/LongToString.java

Sample result shows improvement with a very dumb patch:
 http://cr.openjdk.java.net/~shade/8136500/notes.txt

Other things to do:
 a) Make sure strength-reductions happen at least in C2-optimized code;
 b) Do the same for Integer.
Comments
URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/715972927a17 User: lana Date: 2015-12-10 00:27:01 +0000
10-12-2015

URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/715972927a17 User: shade Date: 2015-11-24 16:04:07 +0000
24-11-2015

RFR: http://mail.openjdk.java.net/pipermail/core-libs-dev/2015-November/036765.html
20-11-2015

Webrev: http://cr.openjdk.java.net/~shade/8136500/webrev.01/ Performance data: http://cr.openjdk.java.net/~shade/8136500/notes.txt
20-11-2015