From af243d4d91400e071a5b8fe5041f55f07fd8a928 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 21 Mar 2020 18:01:50 -0400 Subject: [PATCH] Avoid relying on const parameters to function LLVM seems to at least sometimes optimize better when the length comes directly from the `len()` of the array vs. an equivalent integer. Also, this allows easier copy/pasting of the function into compiler explorer for experimentation. --- src/libcore/unicode/unicode_data.rs | 8 ++++---- src/tools/unicode-table-generator/src/range_search.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libcore/unicode/unicode_data.rs b/src/libcore/unicode/unicode_data.rs index aea923e90757..660b91b6025d 100644 --- a/src/libcore/unicode/unicode_data.rs +++ b/src/libcore/unicode/unicode_data.rs @@ -27,11 +27,11 @@ fn range_search< } else { return false; }; - let idx = bitset_chunk_idx[(chunk_idx as usize)][chunk_piece] as usize; - let word = if idx < CANONICAL { - bitset_canonical[idx] + let idx = bitset_chunk_idx[chunk_idx as usize][chunk_piece] as usize; + let word = if let Some(word) = bitset_canonical.get(idx) { + *word } else { - let (real_idx, mapping) = bitset_canonicalized[idx - CANONICAL]; + let (real_idx, mapping) = bitset_canonicalized[idx - bitset_canonical.len()]; let mut word = bitset_canonical[real_idx as usize]; let should_invert = mapping & (1 << 6) != 0; if should_invert { diff --git a/src/tools/unicode-table-generator/src/range_search.rs b/src/tools/unicode-table-generator/src/range_search.rs index 12efa5a9f83b..b57fd2c1d862 100644 --- a/src/tools/unicode-table-generator/src/range_search.rs +++ b/src/tools/unicode-table-generator/src/range_search.rs @@ -25,11 +25,11 @@ fn range_search< } else { return false; }; - let idx = bitset_chunk_idx[(chunk_idx as usize)][chunk_piece] as usize; - let word = if idx < CANONICAL { - bitset_canonical[idx] + let idx = bitset_chunk_idx[chunk_idx as usize][chunk_piece] as usize; + let word = if let Some(word) = bitset_canonical.get(idx) { + *word } else { - let (real_idx, mapping) = bitset_canonicalized[idx - CANONICAL]; + let (real_idx, mapping) = bitset_canonicalized[idx - bitset_canonical.len()]; let mut word = bitset_canonical[real_idx as usize]; let should_invert = mapping & (1 << 6) != 0; if should_invert {