1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 05:40:03 -04:00

Faster galley cache (#699)

* Speed up galley cache by only using the hash as key

This hashes the job but doesn't compare them with Eq,
which speeds up demo_with_tessellate__realistic by 5-6%,
winning back all the performance lost in
https://github.com/emilk/egui/pull/682

* Remove custom Eq/PartialEq code for LayoutJob and friends

* Silence clippy

* Unrelated clippy fixes
This commit is contained in:
Emil Ernerfeldt
2021-09-04 10:19:58 +02:00
committed by GitHub
parent 3b75a84d3b
commit 5f88d89f74
8 changed files with 34 additions and 59 deletions

View File

@@ -4,8 +4,6 @@ use std::{
sync::Arc,
};
use ahash::AHashMap;
use crate::{
mutex::Mutex,
text::{
@@ -321,8 +319,8 @@ impl Fonts {
/// [`Self::layout_delayed_color`].
///
/// The implementation uses memoization so repeated calls are cheap.
pub fn layout_job(&self, job: impl Into<Arc<LayoutJob>>) -> Arc<Galley> {
self.galley_cache.lock().layout(self, job.into())
pub fn layout_job(&self, job: LayoutJob) -> Arc<Galley> {
self.galley_cache.lock().layout(self, job)
}
/// Will wrap text at the given width and line break at `\n`.
@@ -400,19 +398,25 @@ struct CachedGalley {
struct GalleyCache {
/// Frame counter used to do garbage collection on the cache
generation: u32,
cache: AHashMap<Arc<LayoutJob>, CachedGalley>,
cache: nohash_hasher::IntMap<u64, CachedGalley>,
}
impl GalleyCache {
fn layout(&mut self, fonts: &Fonts, job: Arc<LayoutJob>) -> Arc<Galley> {
match self.cache.entry(job.clone()) {
fn layout(&mut self, fonts: &Fonts, job: LayoutJob) -> Arc<Galley> {
let hash = {
let mut hasher = ahash::AHasher::new_with_keys(123, 456); // TODO: even faster hasher?
job.hash(&mut hasher);
hasher.finish()
};
match self.cache.entry(hash) {
std::collections::hash_map::Entry::Occupied(entry) => {
let cached = entry.into_mut();
cached.last_used = self.generation;
cached.galley.clone()
}
std::collections::hash_map::Entry::Vacant(entry) => {
let galley = super::layout(fonts, job);
let galley = super::layout(fonts, job.into());
let galley = Arc::new(galley);
entry.insert(CachedGalley {
last_used: self.generation,