diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs index c16037cf3d..8f36f95586 100644 --- a/tiny_skia/src/text.rs +++ b/tiny_skia/src/text.rs @@ -273,6 +273,7 @@ struct GlyphCache { impl GlyphCache { const TRIM_INTERVAL: usize = 300; + const CAPACITY_LIMIT: usize = 16 * 1024; fn new() -> Self { GlyphCache::default() @@ -359,12 +360,17 @@ 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;