From 9821c4dd3b41d4e1ad07208e3c65b5cb96b7ecec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Tue, 16 Feb 2021 13:37:58 +0900 Subject: [PATCH] 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, 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 } } } --- helix-core/src/transaction.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index 9cb51718..000782dd 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -314,8 +314,10 @@ impl ChangeSet { /// `true` when the set is empty. #[inline] pub fn is_empty(&self) -> bool { - let len = self.changes.len(); - len == 0 || (len == 1 && self.changes[0] == Operation::Retain(self.len)) + match self.changes.as_slice() { + [] | [Operation::Retain(_)] => true, + _ => false, + } } /// Map a position through the changes.