Skip to content

Commit

Permalink
tiny_skia: Add a capacity limit to GlyphCache
Browse files Browse the repository at this point in the history
 * 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>
  • Loading branch information
MoSal committed Jan 22, 2024
1 parent 545cc90 commit 37e1ac1
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tiny_skia/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ struct GlyphCache {

Check failure on line 258 in tiny_skia/src/text.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/tiny_skia/src/text.rs
impl GlyphCache {
const TRIM_INTERVAL: usize = 300;
const CAPACITY_LIMIT:usize = 16*1024;

fn new() -> Self {
GlyphCache::default()
Expand Down Expand Up @@ -344,12 +345,15 @@ impl GlyphCache {
}

Check failure on line 345 in tiny_skia/src/text.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/tiny_skia/src/text.rs

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;
Expand Down

0 comments on commit 37e1ac1

Please sign in to comment.