Skip to content

Commit

Permalink
Add a size limit to font_matches_cache and empty it when it's reached
Browse files Browse the repository at this point in the history
 In `FontSystem`, `font_matches_cache` is an ever growing cache.

 It can also be a fast growing one in stress tests like running this
 in `cosmic-term`:

     mpv -speed 3 -vo tct <some_video>

 So this commit adds a size limit to that cache, and simply empties
 it when that limit is reached, which shouldn't be a common occurrence
 in normal usage.

Signed-off-by: Mohammad AlSaleh <[email protected]>
  • Loading branch information
MoSal committed Jan 22, 2024
1 parent db1530c commit 8544f00
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/font/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl fmt::Debug for FontSystem {
}

impl FontSystem {
const FONT_MATCHES_CACHE_SIZE_LIMIT: usize = 256;
/// Create a new [`FontSystem`], that allows access to any installed system fonts
///
/// # Timing
Expand Down Expand Up @@ -132,6 +133,12 @@ impl FontSystem {
}

pub fn get_font_matches(&mut self, attrs: Attrs<'_>) -> Arc<Vec<FontMatchKey>> {
// Empty the cache first if it reached the size limit
if self.font_matches_cache.len() >= Self::FONT_MATCHES_CACHE_SIZE_LIMIT {
log::trace!("empty font mache cache");
self.font_matches_cache = Default::default();
}

self.font_matches_cache
//TODO: do not create AttrsOwned unless entry does not already exist
.entry(AttrsOwned::new(attrs))
Expand Down

0 comments on commit 8544f00

Please sign in to comment.