-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_quotes
andnormalize
for 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
StringNormalizer
almost entirely from the flamegraphs (except for users that use\r
or\r\n
newlines that always need normalisation ;( )Test Plan
cargo test