Perf: Skip string normalization when possible#10116
Conversation
CodSpeed Performance ReportMerging #10116 will improve performances by 18.79%Comparing Summary
Benchmarks breakdown
|
1c7e244 to
b87fbfb
Compare
|
| let raw_content = locator.slice(string.content_range()); | ||
| let first_quote_or_normalized_char_offset = raw_content | ||
| .bytes() | ||
| .position(|b| matches!(b, b'\\' | b'"' | b'\'' | b'\r' | b'{')); |
There was a problem hiding this comment.
If it's not an f-string, you can omit {, I think? Similarly, if it's a raw string, you can omit \\... (In that case, you could actually use memchr3, but perhaps not worth it.)
There was a problem hiding this comment.
I considered special casing but decided against it because I want to avoid the extra complexity and it doesn't have to be perfect, for as long as it avoids the "expensive" normalization for most strings.
Using memchr would be nice... but normalize is already now almost gone from the benchmark profiles. That's why I consider this as "good enough".
b87fbfb to
f7a3f92
Compare
Summary
Most strings don't contain any quotes or other characters that need to be normalized. This PR makes use of this fact and short circuits
choose_quotesandnormalizefor strings that contain no such characters. This is done by having one simple loop that searches for the occurrences of any character that needs normalization. This is much faster than the complicated normalization loop.This removes the calls to
StringNormalizeralmost entirely from the flamegraphs (except for users that use\ror\r\nnewlines that always need normalisation ;( )Test Plan
cargo test