about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArpad Borsos <arpad.borsos@sentry.io>2024-02-15 17:36:21 +0100
committerArpad Borsos <swatinem@swatinem.de>2024-05-20 10:04:45 +0200
commit3fda931afe98ccfbc2da7307731b268a38b153e9 (patch)
treeda8e4a59a25109b43a47a0604e3942b35227abeb
parent0334c45bb525f7eb9078f3c5b5074320a9ac7a3d (diff)
downloadrust-3fda931afe98ccfbc2da7307731b268a38b153e9.tar.gz
rust-3fda931afe98ccfbc2da7307731b268a38b153e9.zip
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.
-rw-r--r--library/core/src/fmt/mod.rs5
1 files changed, 5 insertions, 0 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs
index 10e1d27c88a..4cc2a9cf96d 100644
--- a/library/core/src/fmt/mod.rs
+++ b/library/core/src/fmt/mod.rs
@@ -2401,6 +2401,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, '\\' | '\"') {
+                continue;
+            }
+
             let esc = c.escape_debug_ext(EscapeDebugExtArgs {
                 escape_grapheme_extended: true,
                 escape_single_quote: false,