perf(logical-lines): Various small perf improvements (#4022)

This commit is contained in:
Micha Reiser 2023-04-26 21:10:35 +02:00 committed by GitHub
parent cab65b25da
commit f3e6ddda62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 349 additions and 536 deletions

View file

@ -24,14 +24,14 @@ struct LineIndexInner {
impl LineIndex {
/// Builds the [`LineIndex`] from the source text of a file.
pub fn from_source_text(text: &str) -> Self {
assert!(u32::try_from(text.len()).is_ok());
let mut line_starts: Vec<TextSize> = Vec::with_capacity(text.len() / 88);
line_starts.push(TextSize::default());
let bytes = text.as_bytes();
let mut utf8 = false;
assert!(u32::try_from(bytes.len()).is_ok());
for (i, byte) in bytes.iter().enumerate() {
utf8 |= !byte.is_ascii();
@ -39,7 +39,9 @@ impl LineIndex {
// Only track one line break for `\r\n`.
b'\r' if bytes.get(i + 1) == Some(&b'\n') => continue,
b'\n' | b'\r' => {
line_starts.push(TextSize::try_from(i + 1).unwrap());
// SAFETY: Assertion above guarantees `i <= u32::MAX`
#[allow(clippy::cast_possible_truncation)]
line_starts.push(TextSize::from(i as u32) + TextSize::from(1));
}
_ => {}
}