Retain text measurements as long as original entries

This commit is contained in:
Héctor Ramón Jiménez 2023-06-28 01:27:09 +02:00
parent 975eebfc62
commit 00859c25f5
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 36 additions and 33 deletions

View file

@ -388,14 +388,10 @@ impl Cache {
) -> (KeyHash, &mut Entry) { ) -> (KeyHash, &mut Entry) {
let hash = key.hash(self.hasher.build_hasher()); let hash = key.hash(self.hasher.build_hasher());
if let Some(measured_hash) = self.measurements.get(&hash) { if let Some(hash) = self.measurements.get(&hash) {
let _ = self.recently_used.insert(hash); let _ = self.recently_used.insert(*hash);
let _ = self.recently_used.insert(*measured_hash);
return ( return (*hash, self.entries.get_mut(hash).unwrap());
*measured_hash,
self.entries.get_mut(measured_hash).unwrap(),
);
} }
if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) { if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) {
@ -421,6 +417,13 @@ impl Cache {
let _ = entry.insert(Entry { buffer, bounds }); let _ = entry.insert(Entry { buffer, bounds });
for bounds in [
bounds,
Size {
width: key.bounds.width,
..bounds
},
] {
if key.bounds != bounds { if key.bounds != bounds {
let _ = self.measurements.insert( let _ = self.measurements.insert(
Key { bounds, ..key }.hash(self.hasher.build_hasher()), Key { bounds, ..key }.hash(self.hasher.build_hasher()),
@ -428,6 +431,7 @@ impl Cache {
); );
} }
} }
}
let _ = self.recently_used.insert(hash); let _ = self.recently_used.insert(hash);
@ -438,10 +442,8 @@ impl Cache {
if self.trim_count > Self::TRIM_INTERVAL { if self.trim_count > Self::TRIM_INTERVAL {
self.entries self.entries
.retain(|key, _| self.recently_used.contains(key)); .retain(|key, _| self.recently_used.contains(key));
self.measurements.retain(|key, value| { self.measurements
self.recently_used.contains(key) .retain(|_, value| self.recently_used.contains(value));
|| self.recently_used.contains(value)
});
self.recently_used.clear(); self.recently_used.clear();

View file

@ -385,14 +385,10 @@ impl Cache {
) -> (KeyHash, &mut Entry) { ) -> (KeyHash, &mut Entry) {
let hash = key.hash(self.hasher.build_hasher()); let hash = key.hash(self.hasher.build_hasher());
if let Some(measured_hash) = self.measurements.get(&hash) { if let Some(hash) = self.measurements.get(&hash) {
let _ = self.recently_used.insert(hash); let _ = self.recently_used.insert(*hash);
let _ = self.recently_used.insert(*measured_hash);
return ( return (*hash, self.entries.get_mut(hash).unwrap());
*measured_hash,
self.entries.get_mut(measured_hash).unwrap(),
);
} }
if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) { if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) {
@ -415,9 +411,15 @@ impl Cache {
); );
let bounds = measure(&buffer); let bounds = measure(&buffer);
let _ = entry.insert(Entry { buffer, bounds }); let _ = entry.insert(Entry { buffer, bounds });
for bounds in [
bounds,
Size {
width: key.bounds.width,
..bounds
},
] {
if key.bounds != bounds { if key.bounds != bounds {
let _ = self.measurements.insert( let _ = self.measurements.insert(
Key { bounds, ..key }.hash(self.hasher.build_hasher()), Key { bounds, ..key }.hash(self.hasher.build_hasher()),
@ -425,6 +427,7 @@ impl Cache {
); );
} }
} }
}
let _ = self.recently_used.insert(hash); let _ = self.recently_used.insert(hash);
@ -434,10 +437,8 @@ impl Cache {
fn trim(&mut self) { fn trim(&mut self) {
self.entries self.entries
.retain(|key, _| self.recently_used.contains(key)); .retain(|key, _| self.recently_used.contains(key));
self.measurements.retain(|key, value| { self.measurements
self.recently_used.contains(key) .retain(|_, value| self.recently_used.contains(value));
|| self.recently_used.contains(value)
});
self.recently_used.clear(); self.recently_used.clear();
} }