From 72f2ff850f69e74764bd8219283c1f44c69fc34d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 20 Mar 2021 22:49:27 -0700 Subject: [PATCH] fonts: avoid making copies of Memory font handles refs: https://github.com/wez/wezterm/issues/559 --- wezterm-font/src/db.rs | 10 +++++----- wezterm-font/src/ftwrap.rs | 6 +++++- wezterm-font/src/locator/core_text.rs | 3 ++- wezterm-font/src/locator/gdi.rs | 5 +++-- wezterm-font/src/locator/mod.rs | 3 +-- wezterm-font/src/parser.rs | 3 ++- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/wezterm-font/src/db.rs b/wezterm-font/src/db.rs index 783684180..a26487c0f 100644 --- a/wezterm-font/src/db.rs +++ b/wezterm-font/src/db.rs @@ -5,6 +5,7 @@ use crate::FontDataHandle; use anyhow::{anyhow, Context}; use config::{Config, FontAttributes}; use rangeset::RangeSet; +use std::borrow::Cow; use std::cell::RefCell; use std::cmp::Ordering; use std::collections::{HashMap, HashSet}; @@ -22,17 +23,16 @@ impl Entry { /// the set of codepoints for which the font has coverage. fn compute_coverage(&self) -> anyhow::Result> { use ttf_parser::Face; - let on_disk_data; let (data, index) = match &self.handle { - FontDataHandle::Memory { data, index, .. } => (data, *index), + FontDataHandle::Memory { data, index, .. } => (data.clone(), *index), FontDataHandle::OnDisk { path, index } => { - on_disk_data = std::fs::read(path) + let data = std::fs::read(path) .with_context(|| anyhow!("reading font data from {}", path.display()))?; - (&on_disk_data, *index) + (Cow::Owned(data), *index) } }; - let face = Face::from_slice(data, index)?; + let face = Face::from_slice(&data, index)?; let mut coverage = RangeSet::new(); for table in face.character_mapping_subtables() { diff --git a/wezterm-font/src/ftwrap.rs b/wezterm-font/src/ftwrap.rs index 9488aebfb..d956c57d9 100644 --- a/wezterm-font/src/ftwrap.rs +++ b/wezterm-font/src/ftwrap.rs @@ -329,7 +329,11 @@ impl Library { // face because freetype doesn't use O_CLOEXEC and keeps the fd // floating around for a long time! let data = std::fs::read(path)?; - log::trace!("Loading {} for freetype!", path.display()); + log::trace!( + "Loading {} ({} bytes) for freetype!", + path.display(), + data.len() + ); let res = unsafe { FT_New_Memory_Face( diff --git a/wezterm-font/src/locator/core_text.rs b/wezterm-font/src/locator/core_text.rs index 530c452f6..52e6530dc 100644 --- a/wezterm-font/src/locator/core_text.rs +++ b/wezterm-font/src/locator/core_text.rs @@ -9,6 +9,7 @@ use core_foundation::number::CFNumber; use core_foundation::string::CFString; use core_text::font::*; use core_text::font_descriptor::*; +use std::borrow::Cow; use std::collections::HashSet; use ttf_parser::fonts_in_collection; @@ -58,7 +59,7 @@ fn handle_from_descriptor(descriptor: &CTFontDescriptor) -> Option anyhow::Result anyhow::Result, + data: std::borrow::Cow<'static, [u8]>, index: u32, }, } diff --git a/wezterm-font/src/parser.rs b/wezterm-font/src/parser.rs index 921f68688..a7902e7d3 100644 --- a/wezterm-font/src/parser.rs +++ b/wezterm-font/src/parser.rs @@ -2,6 +2,7 @@ use crate::locator::FontDataHandle; use crate::shaper::GlyphInfo; use anyhow::anyhow; use config::FontAttributes; +use std::borrow::Cow; use std::path::{Path, PathBuf}; use ttf_parser::{fonts_in_collection, Face, Name, PlatformId}; @@ -338,7 +339,7 @@ pub(crate) fn load_built_in_fonts( names, PathBuf::from(name), FontDataHandle::Memory { - data: data.to_vec(), + data: Cow::Borrowed(data), index: 0, name: name.to_string(), },