Skip to content

Commit b12a276

Browse files
authored
syntax/utf8: avoid a spurious vector reallocation
This reworks `Utf8Sequences` logic in order to avoid allocating a 0-sized vector and immediately reallocating it for the initial element. Directly create the populated vector instead. I was looking at the memory usage patterns of [rolldown] through heaptrack, and this spot showed up as a potentially-spurious temporary allocation. The consumer side is [here][consumer side]. I do not have a specific benchmark for this. [rolldown]: https://github.com/rolldown/rolldown [consumer side]: https://github.com/rolldown/rolldown/blob/ce36a195ed4e9ce7c446557cefff4750a2268e01/crates/rolldown/src/utils/extract_hash_pattern.rs#L12
1 parent 4c565c8 commit b12a276

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

regex-syntax/src/utf8.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ impl Utf8Sequences {
302302
/// Create a new iterator over UTF-8 byte ranges for the scalar value range
303303
/// given.
304304
pub fn new(start: char, end: char) -> Self {
305-
let mut it = Utf8Sequences { range_stack: vec![] };
306-
it.push(u32::from(start), u32::from(end));
307-
it
305+
let range =
306+
ScalarRange { start: u32::from(start), end: u32::from(end) };
307+
Utf8Sequences { range_stack: vec![range] }
308308
}
309309

310310
/// reset resets the scalar value range.

0 commit comments

Comments
 (0)