RFR: 8316426: Optimization for HexFormat.formatHex [v7]

温绍锦 duke at openjdk.org
Wed Sep 27 09:08:07 UTC 2023


On Wed, 27 Sep 2023 04:16:11 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

> ok, but perhaps you can shrink it further, if it does not hurt performance, by subtracting 0x20 before indexing and cut the table to 64 bytes.

If you use DIGITS of size 54, performance will be 10% slower,  The code is written like this: 


public final class HexFormat {
    private static final byte[] DIGITS = {
             0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
            -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, 10, 11, 12, 13, 14, 15
           // remove 128 - 255
    };

    public static boolean isHexDigit(int ch) {
        // unsigned right shift 8 change to 7
        return (ch >= '0' && ch <= 'f' && DIGITS[ch - '0'] >= 0);
    }

    public static int fromHexDigit(int ch) {
        int value;
        // unsigned right shift 8 change to 7
        if (ch >= '0' && ch <= 'f' && (value = DIGITS[ch - '0']) >= 0) 
        ...
    }
}

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

PR Comment: https://git.openjdk.org/jdk/pull/15768#issuecomment-1737001849


More information about the core-libs-dev mailing list