summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--wgpu/src/text.rs16
1 files changed, 13 insertions, 3 deletions
diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs
index c0f49417..dea6ab18 100644
--- a/wgpu/src/text.rs
+++ b/wgpu/src/text.rs
@@ -316,6 +316,7 @@ struct Cache<'a> {
entries: FxHashMap<KeyHash, glyphon::Buffer<'a>>,
recently_used: FxHashSet<KeyHash>,
hasher: HashBuilder,
+ trim_count: usize,
}
#[cfg(not(target_arch = "wasm32"))]
@@ -325,11 +326,14 @@ type HashBuilder = twox_hash::RandomXxHashBuilder64;
type HashBuilder = std::hash::BuildHasherDefault<twox_hash::XxHash64>;
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,
}
}
@@ -389,10 +393,16 @@ impl<'a> Cache<'a> {
}
fn trim(&mut self) {
- self.entries
- .retain(|key, _| self.recently_used.contains(key));
+ if self.trim_count >= Self::TRIM_INTERVAL {
+ 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;
+ }
}
}