on master help sign in
Optimize Changeset::is_empty()

Checked the ASM output for these three options: pub enum Operation { /// Move cursor by n characters. Retain(usize), /// Delete n characters. Delete(usize), /// Insert text at position. Insert(String), } pub struct A { changes: Vec<Operation>, len: usize, } impl A { pub fn is_empty1(&self) -> bool { match self.changes.as_slice() { [] => true, [Operation::Retain(_)] => true, _ => false, } } /// `true` when the set is empty. pub fn is_empty2(&self) -> bool { let len = self.changes.len(); len == 0 || ( len == 1 && self.changes[0] == Operation::Retain(self.len) ) } pub fn is_empty3(&self) -> bool { match self.changes.as_slice() { [] | [Operation::Retain(_)] => true, _ => false } } }

commit9821c4dd3b41d4e1ad07208e3c65b5cb96b7ecec authorBlaž Hrastnik <blaz@mxxn.io> dateTue 16 Feb 2021, 13:37 (6 years ago) parentb0b5451
1 file changed+4−2
helix-core/src/transaction.rs+4−2
helix-core/src/transaction.rs+4−2
@@ -314,8 +314,10 @@ impl ChangeSet {
314314 /// `true` when the set is empty.
315315 #[inline]
316316 pub fn is_empty(&self) -> bool {
317− let len = self.changes.len();
318− len == 0 || (len == 1 && self.changes[0] == Operation::Retain(self.len))
317+ match self.changes.as_slice() {
318+ [] | [Operation::Retain(_)] => true,
319+ _ => false,
320+ }
319321 }
320322
321323 /// Map a position through the changes.