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>
This commit is contained in:
Mohammad AlSaleh 2024-01-22 11:27:29 +03:00 committed by Héctor Ramón Jiménez
parent bdd1891f43
commit 5bd93181f3
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -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;