Skip to content

Commit f1cc68e

Browse files
No alloc in lookup_alias/lookup_named_sequence (#9819)
The original code allocated for case insensitive comparison. Instead, I iterate through both strings' bytes and convert the bytes to uppercase for the comparison. This has the added benefit of only iterating once in total. The old code iterated at least three times (once per each string pair and then a third time to compare).
1 parent dba7a05 commit f1cc68e

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

‎crates/unicode/src/data.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,22 @@ pub enum LookupResult {
152152
Sequence(&'static str),
153153
}
154154

155-
fn fold_lookup_name(name: &str) -> String {
156-
name.chars().map(|c| c.to_ascii_uppercase()).collect()
155+
fn cmp_caseless(a: &str, b: &str) -> Ordering {
156+
a.bytes()
157+
.map(|b| b.to_ascii_uppercase())
158+
.cmp(b.bytes().map(|b| b.to_ascii_uppercase()))
157159
}
158160

159161
fn lookup_alias(name: &str) -> Option<char> {
160-
let key = fold_lookup_name(name);
161162
NAME_ALIASES
162-
.binary_search_by_key(&key.as_str(), |(n, _)| *n)
163+
.binary_search_by(|(a, _)| cmp_caseless(a, name))
163164
.ok()
164165
.map(|i| NAME_ALIASES[i].1)
165166
}
166167

167168
fn lookup_named_sequence(name: &str) -> Option<&'static str> {
168-
let key = fold_lookup_name(name);
169169
NAMED_SEQUENCES
170-
.binary_search_by_key(&key.as_str(), |(n, _)| *n)
170+
.binary_search_by(|(a, _)| cmp_caseless(a, name))
171171
.ok()
172172
.map(|i| NAMED_SEQUENCES[i].1)
173173
}

0 commit comments

Comments
 (0)