From ea50ec8df1431c9c6aa8077cd1578c4698dc0314 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Mar 2023 19:58:42 +0100 Subject: Trim text `Buffer` cache every frame in `iced_wgpu` and `iced_tiny_skia` --- tiny_skia/src/text.rs | 20 ++++++-------------- wgpu/src/text.rs | 16 +++------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs index f2935efa..714695b9 100644 --- a/tiny_skia/src/text.rs +++ b/tiny_skia/src/text.rs @@ -246,6 +246,8 @@ struct GlyphCache { } impl GlyphCache { + const TRIM_INTERVAL: usize = 300; + fn new() -> Self { GlyphCache::default() } @@ -328,7 +330,7 @@ impl GlyphCache { } pub fn trim(&mut self) { - if self.trim_count > 300 { + if self.trim_count > Self::TRIM_INTERVAL { self.entries .retain(|key, _| self.recently_used.contains(key)); @@ -345,7 +347,6 @@ struct Cache<'a> { entries: FxHashMap>, recently_used: FxHashSet, hasher: HashBuilder, - trim_count: usize, } #[cfg(not(target_arch = "wasm32"))] @@ -355,14 +356,11 @@ type HashBuilder = twox_hash::RandomXxHashBuilder64; type HashBuilder = std::hash::BuildHasherDefault; impl<'a> Cache<'a> { - const TRIM_INTERVAL: usize = 300; - fn new() -> Self { Self { entries: FxHashMap::default(), recently_used: FxHashSet::default(), hasher: HashBuilder::default(), - trim_count: 0, } } @@ -407,16 +405,10 @@ impl<'a> Cache<'a> { } fn trim(&mut self) { - if self.trim_count >= Self::TRIM_INTERVAL { - self.entries - .retain(|key, _| self.recently_used.contains(key)); + self.entries + .retain(|key, _| self.recently_used.contains(key)); - self.recently_used.clear(); - - self.trim_count = 0; - } else { - self.trim_count += 1; - } + self.recently_used.clear(); } } diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index e99844e6..35f24cd9 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -321,7 +321,6 @@ struct Cache<'a> { entries: FxHashMap>, recently_used: FxHashSet, hasher: HashBuilder, - trim_count: usize, } #[cfg(not(target_arch = "wasm32"))] @@ -331,14 +330,11 @@ type HashBuilder = twox_hash::RandomXxHashBuilder64; type HashBuilder = std::hash::BuildHasherDefault; impl<'a> Cache<'a> { - const TRIM_INTERVAL: usize = 300; - fn new() -> Self { Self { entries: FxHashMap::default(), recently_used: FxHashSet::default(), hasher: HashBuilder::default(), - trim_count: 0, } } @@ -387,16 +383,10 @@ impl<'a> Cache<'a> { } fn trim(&mut self) { - if self.trim_count >= Self::TRIM_INTERVAL { - self.entries - .retain(|key, _| self.recently_used.contains(key)); - - self.recently_used.clear(); + self.entries + .retain(|key, _| self.recently_used.contains(key)); - self.trim_count = 0; - } else { - self.trim_count += 1; - } + self.recently_used.clear(); } } -- cgit