RFR: 8315585: Optimization for decimal to string [v6]

Shaojin Wen duke at openjdk.org
Sun Oct 15 22:35:57 UTC 2023


On Fri, 13 Oct 2023 17:01:11 GMT, Shaojin Wen <duke at openjdk.org> wrote:

>> I submitted PR #15555 before, and there were too many changes. I split it into multiple PRs with small changes. This one is one of them.
>> 
>> this PR removed the duplicate code for getChars in BigDecimal#StringBuilderHelper, i also make performance faster.
>> Please review and don't hesitate to critique my approach and patch.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   use % calculate lowInt

Can I use StringConcatHelper.prepend? as follows:


  static final class ConcatHelper {
        static final MethodHandle STRING_PREPEND =
                JLA.stringConcatHelper("prepend",
                        MethodType.methodType(long.class, long.class, byte[].class, long.class));
        static String scale2(long intCompact) {
            long highInt = intCompact / 100;
            int highIntSize = JLA.stringSize(highInt);
            byte[] buf = new byte[highIntSize + 3];
            try {
                long coder = (long) ConcatHelper.STRING_PREPEND.invokeExact((long) highIntSize, buf, highInt);
                buf[highIntSize] = '.';
                short pair = DecimalDigits.digitPair((int)(Math.abs(intCompact) % 100));
                buf[highIntSize + 1] = (byte)(pair & 0xff);
                buf[highIntSize + 2] = (byte)(pair >> 8);
                return JLA.newStringNoRepl(buf, StandardCharsets.ISO_8859_1);
            } catch (Throwable e) {
                throw new AssertionError(e);
            }
        }
    }

    private String layoutChars(boolean sci) {
        int scale = this.scale;
        long intCompact = this.intCompact;
        if (scale == 0)                      // zero scale is trivial
            return unscaledString();
        if (scale == 2  && intCompact != INFLATED) {
            // currency fast path
            return ConcatHelper.scale2(intCompact);
        }
        // ...
    }

-------------

PR Comment: https://git.openjdk.org/jdk/pull/16006#issuecomment-1763523085


More information about the core-libs-dev mailing list