Skip to content

Improve efficiency of is_empty #354

@overlookmotel

Description

@overlookmotel

is_empty checks both inline length and heap length:

pub fn is_empty(&self) -> bool {
let len_heap = ensure_read(self.1);
let last_byte = self.last_byte() as usize;
let mut len = last_byte.wrapping_sub(LastUtf8Char::L0 as u8 as usize);
if last_byte >= LastUtf8Char::Heap as u8 as usize {
len = len_heap;
}
len == 0
}

I believe an empty string is always stored inline. Therefore it could be reduced to:

pub fn is_empty(&self) -> bool {
  self.last_byte() == LastUtf8Char::L0 as u8
}

If my assumption is correct, let me know and I'll make a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions