From: River Riddle Date: Wed, 28 Oct 2020 23:46:31 +0000 (-0700) Subject: [llvm][StringExtras] Use a lookup table for `hexDigitValue` X-Git-Tag: llvmorg-13-init~7795 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f6a6f27edb3991154393976e9e8f7b88542d406c;p=platform%2Fupstream%2Fllvm.git [llvm][StringExtras] Use a lookup table for `hexDigitValue` This method is at the core of the conversion from hex to binary, and using a lookup table great improves the compile time of hex conversions. Context: In MLIR we use hex strings to represent very large constants in the textual format of the IR. These changes lead to a large decrease in compile time when parsing these constants (>1 second -> 350 miliseconds). Differential Revision: https://reviews.llvm.org/D90320 --- diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h index 6084a56..77288f8 100644 --- a/llvm/include/llvm/ADT/StringExtras.h +++ b/llvm/include/llvm/ADT/StringExtras.h @@ -66,10 +66,22 @@ inline ArrayRef arrayRefFromStringRef(StringRef Input) { /// /// If \p C is not a valid hex digit, -1U is returned. inline unsigned hexDigitValue(char C) { - if (C >= '0' && C <= '9') return C-'0'; - if (C >= 'a' && C <= 'f') return C-'a'+10U; - if (C >= 'A' && C <= 'F') return C-'A'+10U; - return -1U; + struct HexTable { + unsigned LUT[255] = {}; + constexpr HexTable() { + // Default initialize everything to invalid. + for (int i = 0; i < 255; ++i) + LUT[i] = -1U; + // Initialize `0`-`9`. + for (int i = 0; i < 10; ++i) + LUT['0' + i] = i; + // Initialize `A`-`F` and `a`-`f`. + for (int i = 0; i < 6; ++i) + LUT['A' + i] = LUT['a' + i] = 10 + i; + } + }; + constexpr HexTable Table; + return Table.LUT[static_cast(C)]; } /// Checks if character \p C is one of the 10 decimal digits.