rope: Minor optimization for tab indices (#20911)

This is a follow up on https://github.com/zed-industries/zed/pull/20289
and optimises the tabs by replacing branches with an XOR.

I saw this after watching the latest zed decoded episode so thank you
for those videos!

Release Notes:

- N/A
This commit is contained in:
Adam Richardson 2024-11-21 07:48:13 +00:00 committed by GitHub
parent ebaa270baf
commit 6ab4b46984
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -504,8 +504,6 @@ impl<'a> ChunkSlice<'a> {
#[inline(always)]
pub fn tabs(&self) -> Tabs {
Tabs {
byte_offset: 0,
char_offset: 0,
tabs: self.tabs,
chars: self.chars,
}
@ -513,8 +511,6 @@ impl<'a> ChunkSlice<'a> {
}
pub struct Tabs {
byte_offset: usize,
char_offset: usize,
tabs: u128,
chars: u128,
}
@ -536,21 +532,14 @@ impl Iterator for Tabs {
let tab_offset = self.tabs.trailing_zeros() as usize;
let chars_mask = (1 << tab_offset) - 1;
let char_offset = (self.chars & chars_mask).count_ones() as usize;
self.byte_offset += tab_offset;
self.char_offset += char_offset;
let position = TabPosition {
byte_offset: self.byte_offset,
char_offset: self.char_offset,
};
self.byte_offset += 1;
self.char_offset += 1;
if self.byte_offset == MAX_BASE {
self.tabs = 0;
} else {
self.tabs >>= tab_offset + 1;
self.chars >>= tab_offset + 1;
}
// Since tabs are 1 byte the tab offset is the same as the byte offset
let position = TabPosition {
byte_offset: tab_offset,
char_offset: char_offset,
};
// Remove the tab we've just seen
self.tabs ^= 1 << tab_offset;
Some(position)
}