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

温绍锦 duke at openjdk.org
Wed Sep 27 03:54:12 UTC 2023


On Tue, 19 Sep 2023 14:30:18 GMT, 温绍锦 <duke at openjdk.org> wrote:

>> In the improvement of @cl4es PR #15591, the advantages of non-lookup-table were discussed.
>> 
>> But if the input is byte[], using lookup table can improve performance.
>> 
>> For HexFormat#formatHex(Appendable, byte[]) and HexFormat#formatHex(byte[]), If the length of byte[] is larger, the performance of table lookup will be improved more obviously.
>
> 温绍锦 has updated the pull request incrementally with one additional commit since the last revision:
> 
>   fix typo

HexFormat.DIGITS now uses 256-length byte[], which can be changed to 128-length. This is a small change. Should it be changed in the current PR?


public final class HexFormat {
    private static final byte[] DIGITS = {
            -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, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
             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, -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
           // remove 128 - 255
    };

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

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

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

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


More information about the core-libs-dev mailing list