about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArpad Borsos <swatinem@swatinem.de>2024-05-20 11:31:02 +0200
committerArpad Borsos <swatinem@swatinem.de>2024-05-20 11:31:02 +0200
commitaaba972e06a35ca6988f41f31ca56d747eac4dc9 (patch)
treed9d875911a3f59078eb956b7dc3fffffd4dad6fd
parent42d870ec8815b860b31f03f938ad911a49a5cff3 (diff)
downloadrust-aaba972e06a35ca6988f41f31ca56d747eac4dc9.tar.gz
rust-aaba972e06a35ca6988f41f31ca56d747eac4dc9.zip
Switch to primarily using `&str`
Surprisingly, benchmarks have shown that using `&str`
instead of `&[u8]` with some `unsafe` code is actually faster.
-rw-r--r--library/core/src/fmt/mod.rs13
1 files changed, 6 insertions, 7 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs
index 7fbbbb67f82..b9f6b2d35c9 100644
--- a/library/core/src/fmt/mod.rs
+++ b/library/core/src/fmt/mod.rs
@@ -2409,9 +2409,10 @@ impl Debug for str {
 
         // the outer loop here splits the string into chunks of printable ASCII, which is just skipped over,
         // and chunks of other chars (unicode, or ASCII that needs escaping), which is handler per-`char`.
-        let mut rest = self.as_bytes();
+        let mut rest = self;
         while rest.len() > 0 {
-            let Some(non_printable_start) = rest.iter().position(|&b| needs_escape(b)) else {
+            let Some(non_printable_start) = rest.as_bytes().iter().position(|&b| needs_escape(b))
+            else {
                 printable_range.end += rest.len();
                 break;
             };
@@ -2420,12 +2421,10 @@ impl Debug for str {
             // SAFETY: the position was derived from an iterator, so is known to be within bounds, and at a char boundary
             rest = unsafe { rest.get_unchecked(non_printable_start..) };
 
-            let printable_start = rest.iter().position(|&b| !needs_escape(b)).unwrap_or(rest.len());
+            let printable_start =
+                rest.as_bytes().iter().position(|&b| !needs_escape(b)).unwrap_or(rest.len());
             let prefix;
-            // SAFETY: the position was derived from an iterator, so is known to be within bounds, and at a char boundary
-            (prefix, rest) = unsafe { rest.split_at_unchecked(printable_start) };
-            // SAFETY: prefix is a valid utf8 sequence, and at a char boundary
-            let prefix = unsafe { crate::str::from_utf8_unchecked(prefix) };
+            (prefix, rest) = rest.split_at(printable_start);
 
             for c in prefix.chars() {
                 let esc = c.escape_debug_ext(EscapeDebugExtArgs {