Repository: alacritty/alacritty
File: alacritty_terminal/src/term/cell.rs (Rust)

Code before:
    pub(crate) fn push_zerowidth(&mut self, character: char) {
        self.init_extra();

        let extra = Arc::make_mut(self.extra.as_mut().unwrap());
        extra.zerowidth.push(character);
    }

    /// Remove all wide char data from a cell.
    #[inline(never)]
    pub(crate) fn clear_wide(&mut self) {
        self.flags.remove(Flags::WIDE_CHAR);
        self.extra = None;
        self.c = ' ';
    }

    /// Set underline color on the cell.
    pub(crate) fn set_underline_color(&mut self, color: Option<Color>) {
        // If we reset color and we don't have zerowidth we should drop extra storage.
        if color.is_none()
            && self.extra.as_ref().map(|extra| !extra.zerowidth.is_empty()) != Some(false)
        {
            self.extra = None;
            return;
        }

        self.init_extra();

        let extra = Arc::make_mut(self.extra.as_mut().unwrap());
        extra.underline_color = color;
    }

    /// Initializes extra storage if there's none.
    #[inline]
    fn init_extra(&mut self) {
        if self.extra.is_none() {
            self.extra = Some(Arc::new(Default::default()));
        }
    }

    /// Underline color stored in this cell.
    #[inline]
    pub fn undreline_color(&self) -> Option<Color> {
        self.extra.as_ref()?.underline_color
    }
}

impl GridCell for Cell {
    #[inline]
    fn is_empty(&self) -> bool {
        (self.c == ' ' || self.c == '\t')
            && self.bg == Color::Named(NamedColor::Background)

Review comment (performance):
Looks like this function is only ever used when we want to have mutable access to extra. Could we maybe just make this return a `&mut` and use Option's `get_or_insert`? Or is that worse for performance?

Code after:
        let extra = self.extra.get_or_insert(Default::default());
        Arc::make_mut(extra).zerowidth.push(character);
    }

    /// Remove all wide char data from a cell.
    #[inline(never)]
    pub fn clear_wide(&mut self) {
        self.flags.remove(Flags::WIDE_CHAR);
        if let Some(extra) = self.extra.as_mut() {
            Arc::make_mut(extra).zerowidth = Vec::new();
        }
        self.c = ' ';
    }

    /// Set underline color on the cell.
    pub fn set_underline_color(&mut self, color: Option<Color>) {
        // If we reset color and we don't have zerowidth we should drop extra storage.
        if color.is_none() && self.extra.as_ref().map_or(true, |extra| !extra.zerowidth.is_empty())
        {
            self.extra = None;
            return;
        }

        let extra = self.extra.get_or_insert(Default::default());
        Arc::make_mut(extra).underline_color = color;
    }

    /// Underline color stored in this cell.
    #[inline]
    pub fn underline_color(&self) -> Option<Color> {
        self.extra.as_ref()?.underline_color
    }
}

impl GridCell for Cell {
    #[inline]
    fn is_empty(&self) -> bool {
        (self.c == ' ' || self.c == '\t')
            && self.bg == Color::Named(NamedColor::Background)
            && self.fg == Color::Named(NamedColor::Foreground)
            && !self.flags.intersects(
                Flags::INVERSE
                    | Flags::ALL_UNDERLINES
                    | Flags::STRIKEOUT
                    | Flags::WRAPLINE
                    | Flags::WIDE_CHAR_SPACER
                    | Flags::LEADING_WIDE_CHAR_SPACER,
            )
            && self.extra.as_ref().map(|extra| extra.zerowidth.is_empty()) != Some(false)
    }