From 37e1ac18371886465b0597246606125188d8bd20 Mon Sep 17 00:00:00 2001 From: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com> Date: Mon, 22 Jan 2024 11:27:29 +0300 Subject: [PATCH] tiny_skia: Add a capacity limit to `GlyphCache` * Trim the cache if `recently_used` size reaches the limit, even if a trim interval hasn't passed. * Shrink `entries` and `recently_used` to the limit when trimming. Signed-off-by: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com> --- tiny_skia/src/text.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs index 9413e31170..69eeb7c519 100644 --- a/tiny_skia/src/text.rs +++ b/tiny_skia/src/text.rs @@ -258,6 +258,7 @@ struct GlyphCache { impl GlyphCache { const TRIM_INTERVAL: usize = 300; + const CAPACITY_LIMIT:usize = 16*1024; fn new() -> Self { GlyphCache::default() @@ -344,12 +345,15 @@ impl GlyphCache { } pub fn trim(&mut self) { - if self.trim_count > Self::TRIM_INTERVAL { + if self.trim_count > Self::TRIM_INTERVAL || self.recently_used.len() >= Self::CAPACITY_LIMIT { self.entries .retain(|key, _| self.recently_used.contains(key)); self.recently_used.clear(); + self.entries.shrink_to(Self::CAPACITY_LIMIT); + self.recently_used.shrink_to(Self::CAPACITY_LIMIT); + self.trim_count = 0; } else { self.trim_count += 1;