[llvm] f6a6f27 - [llvm][StringExtras] Use a lookup table for `hexDigitValue`
River Riddle via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 28 17:03:58 PDT 2020
Author: River Riddle
Date: 2020-10-28T16:58:06-07:00
New Revision: f6a6f27edb3991154393976e9e8f7b88542d406c
URL: https://github.com/llvm/llvm-project/commit/f6a6f27edb3991154393976e9e8f7b88542d406c
DIFF: https://github.com/llvm/llvm-project/commit/f6a6f27edb3991154393976e9e8f7b88542d406c.diff
LOG: [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
Added:
Modified:
llvm/include/llvm/ADT/StringExtras.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 6084a564b95d..77288f8514dc 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -66,10 +66,22 @@ inline ArrayRef<uint8_t> 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<unsigned char>(C)];
}
/// Checks if character \p C is one of the 10 decimal digits.
More information about the llvm-commits
mailing list