From b12a2761f91320bc8bf8246f88d2884a90034b5a Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Mon, 22 Apr 2024 16:21:41 +0200 Subject: [PATCH] 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 --- regex-syntax/src/utf8.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/regex-syntax/src/utf8.rs b/regex-syntax/src/utf8.rs index e13b55abf..69d749451 100644 --- a/regex-syntax/src/utf8.rs +++ b/regex-syntax/src/utf8.rs @@ -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.