1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 12:50:04 -04:00

[optimize] switch to ahash for ids and most maps and sets

This commit is contained in:
Emil Ernerfeldt
2020-05-24 11:36:24 +02:00
parent 6577de9036
commit 96e6f1f3fc
7 changed files with 72 additions and 16 deletions

View File

@@ -7,13 +7,14 @@ edition = "2018"
[lib]
[dependencies]
ahash = "0.3"
parking_lot = "0.10"
rusttype = "0.9"
serde = "1"
serde_derive = "1"
[dev-dependencies]
criterion = "0.3"
criterion = { version = "0.3", default-features = false }
[[bench]]
name = "benchmark"

View File

@@ -1,6 +1,6 @@
use std::{collections::HashMap, sync::Arc};
use std::sync::Arc;
use parking_lot::Mutex;
use {ahash::AHashMap, parking_lot::Mutex};
use crate::{layout::align_rect, paint::*, *};
@@ -31,7 +31,7 @@ pub struct Context {
graphics: Mutex<GraphicLayers>,
output: Mutex<Output>,
/// Used to debug name clashes of e.g. windows
used_ids: Mutex<HashMap<Id, Pos2>>,
used_ids: Mutex<AHashMap<Id, Pos2>>,
paint_stats: Mutex<PaintStats>,
}

View File

@@ -27,7 +27,7 @@
//! So we have two type of Ids: `PositionId` and `UniqueId`.
//! TODO: have separate types for `PositionId` and `UniqueId`.
use std::{collections::hash_map::DefaultHasher, hash::Hash};
use std::hash::Hash;
use crate::math::Pos2;
@@ -47,14 +47,14 @@ impl Id {
pub fn new(source: impl Hash) -> Id {
use std::hash::Hasher;
let mut hasher = DefaultHasher::new();
let mut hasher = ahash::AHasher::default();
source.hash(&mut hasher);
Id(hasher.finish())
}
pub fn with(self, child: impl Hash) -> Id {
use std::hash::Hasher;
let mut hasher = DefaultHasher::new();
let mut hasher = ahash::AHasher::default();
hasher.write_u64(self.0);
child.hash(&mut hasher);
Id(hasher.finish())

View File

@@ -1,5 +1,4 @@
use std::collections::HashMap;
use ahash::AHashMap;
use serde_derive::{Deserialize, Serialize};
use crate::{math::Rect, paint::PaintCmd, Id};
@@ -39,7 +38,7 @@ type PaintList = Vec<(Rect, PaintCmd)>;
/// TODO: improve this
#[derive(Clone, Default)]
pub struct GraphicLayers(HashMap<Layer, PaintList>);
pub struct GraphicLayers(AHashMap<Layer, PaintList>);
impl GraphicLayers {
pub fn layer(&mut self, layer: Layer) -> &mut PaintList {

View File

@@ -1,7 +1,10 @@
use std::{collections::HashMap, sync::Arc};
use std::sync::Arc;
use parking_lot::Mutex;
use rusttype::{point, Scale};
use {
ahash::AHashMap,
parking_lot::Mutex,
rusttype::{point, Scale},
};
use crate::math::{vec2, Vec2};
@@ -179,7 +182,7 @@ pub struct Font {
/// Maximum character height
scale_in_pixels: f32,
pixels_per_point: f32,
glyph_infos: HashMap<char, GlyphInfo>, // TODO: optimize these lookups, e.g. with binary search or a fast hashmap
glyph_infos: AHashMap<char, GlyphInfo>, // TODO: see if we can optimize if we switch to a binary search
atlas: Arc<Mutex<TextureAtlas>>,
}

View File

@@ -1,5 +1,5 @@
use std::{
collections::{hash_map::DefaultHasher, BTreeMap},
collections::BTreeMap,
hash::{Hash, Hasher},
sync::Arc,
};
@@ -107,7 +107,7 @@ impl Fonts {
.collect();
self.texture = atlas.lock().texture().clone();
let mut hasher = DefaultHasher::new();
let mut hasher = ahash::AHasher::default();
self.texture.pixels.hash(&mut hasher);
self.texture.id = hasher.finish();
}