Skip to content

Commit

Permalink
Merge pull request #2210 from MoSal/glyph_cache_capacity_limit
Browse files Browse the repository at this point in the history
tiny_skia: Add a capacity limit to `GlyphCache`
  • Loading branch information
hecrj authored Feb 7, 2024
2 parents bdd1891 + 9c97711 commit b4dcf4e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use `Radians` for angle fields in `Arc` and `arc::Elliptical`. [#2027](https://github.com/iced-rs/iced/pull/2027)
- Assert dimensions of quads are normal in `iced_tiny_skia`. [#2082](https://github.com/iced-rs/iced/pull/2082)
- Remove `position` from `overlay::Element`. [#2226](https://github.com/iced-rs/iced/pull/2226)
- Add a capacity limit to the `GlyphCache` in `iced_tiny_skia`. [#2210](https://github.com/iced-rs/iced/pull/2210)

### Fixed
- Clipping of `TextInput` selection. [#2199](https://github.com/iced-rs/iced/pull/2199)
Expand Down Expand Up @@ -126,6 +127,7 @@ Many thanks to...
- @lufte
- @matze
- @MichalLebeda
- @MoSal
- @MrAntix
- @nicksenger
- @Nisatru
Expand Down
8 changes: 7 additions & 1 deletion tiny_skia/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ struct GlyphCache {

impl GlyphCache {
const TRIM_INTERVAL: usize = 300;
const CAPACITY_LIMIT: usize = 16 * 1024;

fn new() -> Self {
GlyphCache::default()
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b4dcf4e

Please sign in to comment.