1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Avoid allocations for loader cache lookup (#5584)

[ x ] I have ~~followed~~ _read_ the instructions in the PR template

Unfortunately i had several issues:
- Some snapshot-tests didn't run successfully on osx. diff shows errors
around fonts or missing menu items)
- cargo clippy doesn't run successfully (egui_kittest cannot find `wgpu`
and `image`)
- ./scripts/check.sh had other issues on my system (env: python: No such
file or directory), even if python3 can be called via python in my shell
 
Is there a system independent, standard way to run these tools (e.g. via
Docker?)
I submit the pr anyway, because there changes are very simple and
shouldn't cause issues.
This commit is contained in:
Markus Ineichen
2025-01-06 10:03:33 +01:00
committed by GitHub
parent 9073516e30
commit 0fac8eadfc
2 changed files with 10 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
use std::{mem::size_of, path::Path, sync::Arc};
use std::{borrow::Cow, mem::size_of, path::Path, sync::Arc};
use ahash::HashMap;
@@ -12,7 +12,7 @@ type Entry = Result<Arc<ColorImage>, String>;
#[derive(Default)]
pub struct SvgLoader {
cache: Mutex<HashMap<(String, SizeHint), Entry>>,
cache: Mutex<HashMap<(Cow<'static, str>, SizeHint), Entry>>,
}
impl SvgLoader {
@@ -37,23 +37,21 @@ impl ImageLoader for SvgLoader {
return Err(LoadError::NotSupported);
}
let uri = uri.to_owned();
let mut cache = self.cache.lock();
// We can't avoid the `uri` clone here without unsafe code.
if let Some(entry) = cache.get(&(uri.clone(), size_hint)).cloned() {
if let Some(entry) = cache.get(&(Cow::Borrowed(uri), size_hint)).cloned() {
match entry {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Loading(err)),
}
} else {
match ctx.try_load_bytes(&uri) {
match ctx.try_load_bytes(uri) {
Ok(BytesPoll::Ready { bytes, .. }) => {
log::trace!("started loading {uri:?}");
let result = crate::image::load_svg_bytes_with_size(&bytes, Some(size_hint))
.map(Arc::new);
log::trace!("finished loading {uri:?}");
cache.insert((uri, size_hint), result.clone());
cache.insert((Cow::Owned(uri.to_owned()), size_hint), result.clone());
match result {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Loading(err)),