Skip to content

Eliminate bunch of copies of error codepath from Utf8LossyChunksIter #91244

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

Merged
merged 2 commits into from
Nov 30, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 31 additions & 38 deletions library/core/src/str/lossy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,26 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
}

let mut i = 0;
let mut valid_up_to = 0;
while i < self.source.len() {
let i_ = i;

// SAFETY: `i` starts at `0`, is less than `self.source.len()`, and
// only increases, so `0 <= i < self.source.len()`.
// SAFETY: `i < self.source.len()` per previous line.
// For some reason the following are both significantly slower:
// while let Some(&byte) = self.source.get(i) {
// while let Some(byte) = self.source.get(i).copied() {
let byte = unsafe { *self.source.get_unchecked(i) };
i += 1;

if byte < 128 {
// This could be a `1 => ...` case in the match below, but for
// the common case of all-ASCII inputs, we bypass loading the
// sizeable UTF8_CHAR_WIDTH table into cache.
} else {
let w = utf8_char_width(byte);

macro_rules! error {
() => {{
// SAFETY: We have checked up to `i` that source is valid UTF-8.
unsafe {
let r = Utf8LossyChunk {
valid: from_utf8_unchecked(&self.source[0..i_]),
broken: &self.source[i_..i],
};
self.source = &self.source[i..];
return Some(r);
}
}};
}

match w {
2 => {
if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
error!();
break;
}
i += 1;
}
Expand All @@ -100,13 +90,11 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
(0xE1..=0xEC, 0x80..=0xBF) => (),
(0xED, 0x80..=0x9F) => (),
(0xEE..=0xEF, 0x80..=0xBF) => (),
_ => {
error!();
}
_ => break,
}
i += 1;
if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
error!();
break;
}
i += 1;
}
Expand All @@ -115,34 +103,39 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
(0xF0, 0x90..=0xBF) => (),
(0xF1..=0xF3, 0x80..=0xBF) => (),
(0xF4, 0x80..=0x8F) => (),
_ => {
error!();
}
_ => break,
}
i += 1;
if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
error!();
break;
}
i += 1;
if safe_get(self.source, i) & 192 != TAG_CONT_U8 {
error!();
break;
}
i += 1;
}
_ => {
error!();
}
_ => break,
}
}

valid_up_to = i;
}

let r = Utf8LossyChunk {
// SAFETY: We have checked that the entire source is valid UTF-8.
valid: unsafe { from_utf8_unchecked(self.source) },
broken: &[],
};
self.source = &[];
Some(r)
// SAFETY: `i <= self.source.len()` because it only ever increments by 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i is incremented up to 3 times in some loop iterations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's incremented up to 4 times in some loop iterations, but what I meant is: in between every pair of i += 1, we always compare i vs self.source.len(). As soon as an i += 1 puts i out of bounds, then if safe_get(self.source, i) & 192 != TAG_CONT_U8 { break; } will terminate the loop because 0 & 192 != 128.

// and the loop is terminated as soon as that goes beyond bounds.
let (inspected, remaining) = unsafe { self.source.split_at_unchecked(i) };
self.source = remaining;

// SAFETY: `valid_up_to <= i` because it is only ever assigned via
// `valid_up_to = i` and `i` only increases.
let (valid, broken) = unsafe { inspected.split_at_unchecked(valid_up_to) };

Some(Utf8LossyChunk {
// SAFETY: All bytes up to `valid_up_to` are valid UTF-8.
valid: unsafe { from_utf8_unchecked(valid) },
broken,
})
}
}

Expand Down