From 235ec02a20a775d37cc5790c87f0b44ca867d8e8 Mon Sep 17 00:00:00 2001 From: Mohammad AlSaleh Date: Mon, 22 Jan 2024 11:12:06 +0300 Subject: [PATCH] Add a size limit to `font_matches_cache` and clear it when it's reached 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 So this commit adds a size limit to that cache, and clears the cache when that limit is reached, which shouldn't be a common occurrence in normal usage. Signed-off-by: Mohammad AlSaleh --- src/font/system.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/font/system.rs b/src/font/system.rs index 5ca2a00..ad85db1 100644 --- a/src/font/system.rs +++ b/src/font/system.rs @@ -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 @@ -132,6 +133,12 @@ impl FontSystem { } pub fn get_font_matches(&mut self, attrs: Attrs<'_>) -> Arc> { + // Clear the cache first if it reached the size limit + if self.font_matches_cache.len() >= Self::FONT_MATCHES_CACHE_SIZE_LIMIT { + log::trace!("clear font mache cache"); + self.font_matches_cache.clear(); + } + self.font_matches_cache //TODO: do not create AttrsOwned unless entry does not already exist .entry(AttrsOwned::new(attrs))