From 04602a24a38033c6ef20fe0ecb53316b808542ce Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Thu, 15 Feb 2024 17:36:21 +0100 Subject: [PATCH] Add a fast-path to `Debug` ASCII `&str` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of going through the `EscapeDebug` machinery, we can just skip over ASCII chars that don’t need any escaping. --- library/core/src/fmt/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index e1b7b46a1ed2f..f625c9f565d7f 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -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, '\\' | '\"') { + continue; + } + let esc = c.escape_debug_ext(EscapeDebugExtArgs { escape_grapheme_extended: true, escape_single_quote: false,