Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tiny_skia: Add a capacity limit to GlyphCache #2210

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading