1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-01 07:46:05 +02:00

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
        }
    }

}
This commit is contained in:
Blaž Hrastnik 2021-02-16 13:37:58 +09:00
parent b0b5451c38
commit 9821c4dd3b

View File

@ -314,8 +314,10 @@ pub fn apply(&self, text: &mut Rope) -> bool {
/// `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.