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
Add a fast-path to Debug
ASCII &str
#121150
base: master
Are you sure you want to change the base?
Conversation
Instead of going through the `EscapeDebug` machinery, we can just skip over ASCII chars that don’t need any escaping.
@@ -2340,6 +2340,11 @@ impl Debug for str { | |||
f.write_char('"')?; | |||
let mut from = 0; | |||
for (i, c) in self.char_indices() { | |||
// a fast path for ASCII chars that do not need escapes: | |||
if matches!(c, ' '..='~') && !matches!(c, '\\' | '\"') { |
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.
Do we want to use https://doc.rust-lang.org/std/primitive.char.html#method.is_ascii_graphic or some combination of other methods rather than hard-coding this?
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.
is_ascii_graphic
does not include space, and I don’t think \
or "
are part of any of the other methods?
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.
There's escape_ascii
but it escapes more than those two.
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.
The loop doesn't look like it would vectorize or even unroll well. For a string of chars that don't need escaping it might be worth processing the utf8 bytes directly like EscapeAscii
While we are still bikeshedding the implementation details, I would appreciate a benchmark run. Not sure if the compiler performance testsuite will show any change, in a micro-benchmark, I was able to get a 10x improvement for a pure-ASCII string, and even a tiny improvement in the "pure"-Unicode case (because even a "pure" unicode sentence still contains ASCII spaces) |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Add a fast-path to `Debug` ASCII `&str` Instead of going through the `EscapeDebug` machinery, we can just skip over ASCII chars that don’t need any escaping. --- This is an alternative / a companion to rust-lang#121138. The other PR is adding the fast path deep within `EscapeDebug`, whereas this skips as early as possible.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (d3c44b1): comparison URL. Overall result: no relevant changes - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 640.535s -> 641.862s (0.21%) |
Not too surprisingly, the compile time benchmarks did not budge, or look bogus. Though if you select "runtime", there is a very significant improvement to So the question remains, what can I do to improve the confidence in this change? I have looked through other similar existing methods, but none of which do exactly what I expect. Another option would be to also perf-test #121138, as in my local benchmarking I found that the slowness came from binary searching the |
Let's see what this now does on top of #121138... @bors try @rust-timer queue And for good measure, let's mention that #122013 is also making changes around this. |
This comment has been minimized.
This comment has been minimized.
Add a fast-path to `Debug` ASCII `&str` Instead of going through the `EscapeDebug` machinery, we can just skip over ASCII chars that don’t need any escaping. --- This is an alternative / a companion to rust-lang#121138. The other PR is adding the fast path deep within `EscapeDebug`, whereas this skips as early as possible.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (d253c69): comparison URL. Overall result: no relevant changes - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 644.65s -> 646.087s (0.22%) |
Another -26.93% on the |
AIUI you're still investigating... |
Yes, I would still love to get to this, just got side tracked a bit with other stuff in the meantime 😅 |
No problem, just managing my review queue. :) |
Instead of going through the
EscapeDebug
machinery, we can just skip over ASCII chars that don’t need any escaping.This is an alternative / a companion to #121138.
The other PR is adding the fast path deep within
EscapeDebug
, whereas this skips as early as possible.