Skip to content

Commit

Permalink
Improve cache of font data to use less memory
Browse files Browse the repository at this point in the history
  • Loading branch information
SamRodri committed Jan 20, 2025
1 parent b0f4dcb commit f11afa4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

* Improve cache of font data to use less memory.

# 0.13.7

Expand Down
1 change: 1 addition & 0 deletions crates/zng-ext-font/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,7 @@ impl<I: SliceIndex<[Font]>> std::ops::Index<I> for FontList {
struct FontFaceLoader {
custom_fonts: HashMap<FontName, Vec<FontFace>>,
unregister_requests: Vec<(FontName, ResponderVar<bool>)>,

system_fonts_cache: HashMap<FontName, Vec<SystemFontFace>>,
list_cache: HashMap<Box<[FontName]>, Vec<FontFaceListQuery>>,
}
Expand Down
22 changes: 21 additions & 1 deletion crates/zng-ext-font/src/query_util.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use std::path::PathBuf;

#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
pub use desktop::*;

use parking_lot::Mutex;
#[cfg(target_arch = "wasm32")]
pub use wasm::*;

#[cfg(target_os = "android")]
pub use android::*;

static DATA_CACHE: Mutex<Vec<(PathBuf, std::sync::Weak<Vec<u8>>)>> = Mutex::new(vec![]);

#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
mod desktop {
use std::{borrow::Cow, path::Path, sync::Arc};

use zng_layout::unit::ByteUnits;
use zng_var::ResponseVar;

use crate::{FontDataRef, FontLoadingError, FontName, FontStretch, FontStyle, FontWeight, GlyphLoadingError};
Expand Down Expand Up @@ -166,9 +172,23 @@ mod desktop {
}
}

for (k, data) in super::DATA_CACHE.lock().iter() {
if *k == *path {
if let Some(data) = data.upgrade() {
return Ok((FontDataRef(data), *font_index));
}
}
}

let bytes = std::fs::read(&*path)?;
tracing::debug!("read font `{}:{}`, using {}", path.display(), font_index, bytes.capacity().bytes());

let data = Arc::new(bytes);
let mut cache = super::DATA_CACHE.lock();
cache.retain(|(_, v)| v.strong_count() > 0);
cache.push((path.to_path_buf(), Arc::downgrade(&data)));

Ok((FontDataRef(Arc::new(bytes)), *font_index))
Ok((FontDataRef(data), *font_index))
}
font_kit::handle::Handle::Memory { bytes, font_index } => Ok((FontDataRef(bytes.clone()), *font_index)),
}
Expand Down

0 comments on commit f11afa4

Please sign in to comment.