Trim Cache every 300 frames in text::Pipeline

This commit is contained in:
Héctor Ramón Jiménez 2023-02-10 18:52:35 +01:00
parent 17a4d817c4
commit 51844c5d0c
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -316,6 +316,7 @@ struct Cache<'a> {
entries: FxHashMap<KeyHash, glyphon::Buffer<'a>>, entries: FxHashMap<KeyHash, glyphon::Buffer<'a>>,
recently_used: FxHashSet<KeyHash>, recently_used: FxHashSet<KeyHash>,
hasher: HashBuilder, hasher: HashBuilder,
trim_count: usize,
} }
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
@ -325,11 +326,14 @@ type HashBuilder = twox_hash::RandomXxHashBuilder64;
type HashBuilder = std::hash::BuildHasherDefault<twox_hash::XxHash64>; type HashBuilder = std::hash::BuildHasherDefault<twox_hash::XxHash64>;
impl<'a> Cache<'a> { impl<'a> Cache<'a> {
const TRIM_INTERVAL: usize = 300;
fn new() -> Self { fn new() -> Self {
Self { Self {
entries: FxHashMap::default(), entries: FxHashMap::default(),
recently_used: FxHashSet::default(), recently_used: FxHashSet::default(),
hasher: HashBuilder::default(), hasher: HashBuilder::default(),
trim_count: 0,
} }
} }
@ -389,10 +393,16 @@ impl<'a> Cache<'a> {
} }
fn trim(&mut self) { fn trim(&mut self) {
self.entries if self.trim_count >= Self::TRIM_INTERVAL {
.retain(|key, _| self.recently_used.contains(key)); self.entries
.retain(|key, _| self.recently_used.contains(key));
self.recently_used.clear(); self.recently_used.clear();
self.trim_count = 0;
} else {
self.trim_count += 1;
}
} }
} }