Skip to content

Commit

Permalink
syntax/utf8: avoid a spurious vector reallocation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lucab committed Apr 22, 2024
1 parent 4c565c8 commit b12a276
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions regex-syntax/src/utf8.rs
Expand Up @@ -302,9 +302,9 @@ impl Utf8Sequences {
/// Create a new iterator over UTF-8 byte ranges for the scalar value range
/// given.
pub fn new(start: char, end: char) -> Self {
let mut it = Utf8Sequences { range_stack: vec![] };
it.push(u32::from(start), u32::from(end));
it
let range =
ScalarRange { start: u32::from(start), end: u32::from(end) };
Utf8Sequences { range_stack: vec![range] }
}

/// reset resets the scalar value range.
Expand Down

0 comments on commit b12a276

Please sign in to comment.