Take a `&clock::Global` instead of cloning in `edits_since`

Antonio Scandurra created

Change summary

crates/buffer/src/lib.rs                  | 20 ++++++++++++--------
crates/buffer/src/tests.rs                |  2 +-
crates/editor/src/display_map/fold_map.rs |  4 ++--
crates/language/src/lib.rs                | 16 ++++++++--------
4 files changed, 23 insertions(+), 19 deletions(-)

Detailed changes

crates/buffer/src/lib.rs 🔗

@@ -317,7 +317,7 @@ struct Edits<'a, D: TextDimension<'a>, F: FnMut(&FragmentSummary) -> bool> {
     deleted_cursor: rope::Cursor<'a>,
     fragments_cursor: Option<FilterCursor<'a, F, Fragment, FragmentTextSummary>>,
     undos: &'a UndoMap,
-    since: clock::Global,
+    since: &'a clock::Global,
     old_end: D,
     new_end: D,
 }
@@ -1365,7 +1365,10 @@ impl Buffer {
         })
     }
 
-    pub fn edits_since<'a, D>(&'a self, since: clock::Global) -> impl 'a + Iterator<Item = Edit<D>>
+    pub fn edits_since<'a, D>(
+        &'a self,
+        since: &'a clock::Global,
+    ) -> impl 'a + Iterator<Item = Edit<D>>
     where
         D: 'a + TextDimension<'a> + Ord,
     {
@@ -1603,7 +1606,10 @@ impl Snapshot {
         self.content().anchor_at(position, Bias::Right)
     }
 
-    pub fn edits_since<'a, D>(&'a self, since: clock::Global) -> impl 'a + Iterator<Item = Edit<D>>
+    pub fn edits_since<'a, D>(
+        &'a self,
+        since: &'a clock::Global,
+    ) -> impl 'a + Iterator<Item = Edit<D>>
     where
         D: 'a + TextDimension<'a> + Ord,
     {
@@ -1935,17 +1941,15 @@ impl<'a> Content<'a> {
         }
     }
 
-    // TODO: take a reference to clock::Global.
-    pub fn edits_since<D>(&self, since: clock::Global) -> impl 'a + Iterator<Item = Edit<D>>
+    pub fn edits_since<D>(&self, since: &'a clock::Global) -> impl 'a + Iterator<Item = Edit<D>>
     where
         D: 'a + TextDimension<'a> + Ord,
     {
-        let since_2 = since.clone();
-        let fragments_cursor = if since == *self.version {
+        let fragments_cursor = if since == self.version {
             None
         } else {
             Some(self.fragments.filter(
-                move |summary| summary.max_version.changed_since(&since_2),
+                move |summary| summary.max_version.changed_since(since),
                 &None,
             ))
         };

crates/buffer/src/tests.rs 🔗

@@ -78,7 +78,7 @@ fn test_random_edits(mut rng: StdRng) {
 
     for mut old_buffer in buffer_versions {
         let edits = buffer
-            .edits_since::<usize>(old_buffer.version.clone())
+            .edits_since::<usize>(&old_buffer.version)
             .collect::<Vec<_>>();
 
         log::info!(

crates/editor/src/display_map/fold_map.rs 🔗

@@ -261,7 +261,7 @@ impl FoldMap {
             },
         );
         let edits = buffer
-            .edits_since(last_sync.version)
+            .edits_since(&last_sync.version)
             .map(Into::into)
             .collect::<Vec<_>>();
         if edits.is_empty() {
@@ -1344,7 +1344,7 @@ mod tests {
                         let edit_count = rng.gen_range(1..=5);
                         buffer.randomly_edit(&mut rng, edit_count);
                         buffer
-                            .edits_since::<Point>(start_version)
+                            .edits_since::<Point>(&start_version)
                             .collect::<Vec<_>>()
                     });
                     log::info!("editing {:?}", edits);

crates/language/src/lib.rs 🔗

@@ -360,7 +360,7 @@ impl Buffer {
                                         content_changes: snapshot
                                             .buffer_snapshot
                                             .edits_since::<(PointUtf16, usize)>(
-                                                prev_snapshot.buffer_snapshot.version().clone(),
+                                                prev_snapshot.buffer_snapshot.version(),
                                             )
                                             .map(|edit| {
                                                 let edit_start = edit.new.start.0;
@@ -616,7 +616,7 @@ impl Buffer {
     }
 
     fn interpolate_tree(&self, tree: &mut SyntaxTree) {
-        for edit in self.edits_since::<(usize, Point)>(tree.version.clone()) {
+        for edit in self.edits_since::<(usize, Point)>(&tree.version) {
             let (bytes, lines) = edit.flatten();
             tree.tree.edit(&InputEdit {
                 start_byte: bytes.new.start,
@@ -672,7 +672,7 @@ impl Buffer {
         diagnostics.sort_unstable_by_key(|d| (d.range.start, d.range.end));
         self.diagnostics = {
             let mut edits_since_save = content
-                .edits_since::<PointUtf16>(self.saved_version.clone())
+                .edits_since::<PointUtf16>(&self.saved_version)
                 .peekable();
             let mut last_edit_old_end = PointUtf16::zero();
             let mut last_edit_new_end = PointUtf16::zero();
@@ -1081,7 +1081,7 @@ impl Buffer {
     ) -> Result<()> {
         if let Some(start_version) = self.text.end_transaction_at(selection_set_ids, now) {
             let was_dirty = start_version != self.saved_version;
-            self.did_edit(start_version, was_dirty, cx);
+            self.did_edit(&start_version, was_dirty, cx);
         }
         Ok(())
     }
@@ -1222,7 +1222,7 @@ impl Buffer {
 
     fn did_edit(
         &mut self,
-        old_version: clock::Global,
+        old_version: &clock::Global,
         was_dirty: bool,
         cx: &mut ModelContext<Self>,
     ) {
@@ -1298,7 +1298,7 @@ impl Buffer {
         let was_dirty = self.is_dirty();
         let old_version = self.version.clone();
         self.text.apply_ops(ops)?;
-        self.did_edit(old_version, was_dirty, cx);
+        self.did_edit(&old_version, was_dirty, cx);
         // Notify independently of whether the buffer was edited as the operations could include a
         // selection update.
         cx.notify();
@@ -1330,7 +1330,7 @@ impl Buffer {
             self.send_operation(operation, cx);
         }
 
-        self.did_edit(old_version, was_dirty, cx);
+        self.did_edit(&old_version, was_dirty, cx);
     }
 
     pub fn redo(&mut self, cx: &mut ModelContext<Self>) {
@@ -1341,7 +1341,7 @@ impl Buffer {
             self.send_operation(operation, cx);
         }
 
-        self.did_edit(old_version, was_dirty, cx);
+        self.did_edit(&old_version, was_dirty, cx);
     }
 }