Repository: alacritty/alacritty
File: alacritty/src/renderer/mod.rs (Rust)

Code before:
        Ok((regular, bold, italic, bold_italic))
    }

    fn load_regular_font(
        rasterizer: &mut Rasterizer,
        description: &FontDesc,
        size: Size,
    ) -> Result<FontKey, crossfont::Error> {
        match rasterizer.load_font(description, size) {
            Ok(font) => Ok(font),
            Err(err) => {
                error!("{}", err);

                let fallback_desc =
                    Self::make_desc(&Font::default().normal(), Slant::Normal, Weight::Normal);
                rasterizer.load_font(&fallback_desc, size)
            },
        }
    }

    fn make_desc(desc: &FontDescription, slant: Slant, weight: Weight) -> FontDesc {
        let style = if let Some(ref spec) = desc.style {
            Style::Specific(spec.to_owned())
        } else {
            Style::Description { slant, weight }
        };
        FontDesc::new(desc.family.clone(), style)
    }

    pub fn get<L>(&mut self, glyph_key: GlyphKey, loader: &mut L) -> &Glyph
    where
        L: LoadGlyph,
    {
        let glyph_offset = self.glyph_offset;
        let rasterizer = &mut self.rasterizer;
        let metrics = &self.metrics;
        self.cache.entry(glyph_key).or_insert_with(|| {
            let mut is_missing = false;
            let mut rasterized = rasterizer.get_glyph(glyph_key).unwrap_or_else(|err| match err {
                RasterizerError::MissingGlyph(glyph) => {
                    is_missing = true;
                    glyph
                },
                _ => Default::default(),
            });

            rasterized.left += i32::from(glyph_offset.x);
            rasterized.top += i32::from(glyph_offset.y);
            rasterized.top -= metrics.descent as i32;

            let mut glyph = loader.load_glyph(&rasterized);

Review comment (performance):
I don't really have an idea how to do it nicer than that. The whole purpose is to avoid going into crossfont and performing the entire fallback logic on each redraw when zerowidth char is missing in the font, since it could only work if we add missing glyph for that char into the glyph cache, but not drawn on screen.

If you have any idea how to do that better, I'd like to hear, however everything I can think of will require rework of the loading logic entirely, and not sure if it'll be any better than what we have now...

Code after:
    ///
    /// If the glyph has never been loaded before, it will be rasterized and inserted into the
    /// cache.
    ///
    /// # Errors
    ///
    /// This will fail when the glyph could not be rasterized. Usually this is due to the glyph
    /// not being present in any font.
    fn get<L>(&mut self, glyph_key: GlyphKey, loader: &mut L) -> Result<&Glyph, RasterizerError>
    where
        L: LoadGlyph,
    {
        match self.cache.entry(glyph_key) {
            // Glyph was loaded from cache.
            Entry::Occupied(entry) => Ok(entry.into_mut()),
            // Try to rasterize the glyph if it's uncached.
            Entry::Vacant(entry) => match self.rasterizer.get_glyph(glyph_key) {
                // Add rasterized glyph to the cache.
                Ok(mut rasterized) => {
                    rasterized.left += i32::from(self.glyph_offset.x);
                    rasterized.top += i32::from(self.glyph_offset.y);
                    rasterized.top -= self.metrics.descent as i32;

                    // The metrics of zero-width characters are based on rendering
                    // the character after the current cell, with the anchor at the
                    // right side of the preceding character. Since we render the
                    // zero-width characters inside the preceding character, the
                    // anchor has been moved to the right by one cell.
                    if glyph_key.character.width() == Some(0) {
                        rasterized.left += self.metrics.average_advance as i32;
                    }

                    Ok(entry.insert(loader.load_glyph(&rasterized)))
                },
                // Propagate rasterization failure.
                Err(err) => Err(err),
            },
        }
    }

    /// Load a glyph into the cache.
    ///
    /// This will always insert a new glyph in the cache, even if the rasterization returned a
    /// "missing" glyph or failed completely.
    fn cache_glyph<L>(&mut self, glyph_key: GlyphKey, loader: &mut L)
    where
        L: LoadGlyph,
    {
        let rasterized = match self.rasterizer.get_glyph(glyph_key) {
            Ok(mut rasterized) | Err(RasterizerError::MissingGlyph(mut rasterized)) => {
                rasterized.left += i32::from(self.glyph_offset.x);