Skip to content

Commit

Permalink
Switch to primarily using &str
Browse files Browse the repository at this point in the history
Surprisingly, benchmarks have shown that using `&str`
instead of `&[u8]` with some `unsafe` code is actually faster.
  • Loading branch information
Swatinem committed May 20, 2024
1 parent 42d870e commit aaba972
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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 {
Expand Down

0 comments on commit aaba972

Please sign in to comment.