rope: Minor optimization for tab indices (#20911)

Adam Richardson created

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

Change summary

crates/rope/src/chunk.rs | 23 ++++++-----------------
1 file changed, 6 insertions(+), 17 deletions(-)

Detailed changes

crates/rope/src/chunk.rs 🔗

@@ -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;
+
+        // Since tabs are 1 byte the tab offset is the same as the byte offset
         let position = TabPosition {
-            byte_offset: self.byte_offset,
-            char_offset: self.char_offset,
+            byte_offset: tab_offset,
+            char_offset: 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;
-        }
+        // Remove the tab we've just seen
+        self.tabs ^= 1 << tab_offset;
 
         Some(position)
     }