summaryrefslogtreecommitdiffstats
path: root/tiny_skia
diff options
context:
space:
mode:
authorLibravatar Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>2024-01-22 11:27:29 +0300
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-02-07 14:27:35 +0100
commit5bd93181f349f7160b694d7b1a594bfe7263d063 (patch)
tree80eb6833814544aab453258f400bbb94e92dfdc7 /tiny_skia
parentbdd1891f434066dcb878520358c2e582f231683a (diff)
downloadiced-5bd93181f349f7160b694d7b1a594bfe7263d063.tar.gz
iced-5bd93181f349f7160b694d7b1a594bfe7263d063.tar.bz2
iced-5bd93181f349f7160b694d7b1a594bfe7263d063.zip
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>
Diffstat (limited to 'tiny_skia')
-rw-r--r--tiny_skia/src/text.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs
index c16037cf..8f36f955 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;