From 96e6f1f3fc0df02130fbf161e2abc93aee50fdb1 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 24 May 2020 11:36:24 +0200 Subject: [PATCH] [optimize] switch to ahash for ids and most maps and sets --- Cargo.lock | 53 +++++++++++++++++++++++++++++++++++++++ emigui/Cargo.toml | 3 ++- emigui/src/context.rs | 6 ++--- emigui/src/id.rs | 6 ++--- emigui/src/layers.rs | 5 ++-- emigui/src/paint/font.rs | 11 +++++--- emigui/src/paint/fonts.rs | 4 +-- 7 files changed, 72 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 745649f8f..d3672bdc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,14 @@ dependencies = [ "gimli 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ahash" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "const-random 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "andrew" version = "0.2.1" @@ -186,6 +194,24 @@ dependencies = [ "objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "const-random" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "const-random-macro 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.5.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "const-random-macro" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.5.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "core-foundation" version = "0.6.4" @@ -329,6 +355,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "emigui" version = "0.1.0" dependencies = [ + "ahash 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "criterion 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", "rusttype 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -402,6 +429,16 @@ name = "foreign-types-shared" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "getrandom" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.70 (registry+https://github.com/rust-lang/crates.io-index)", + "wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gimli" version = "0.21.0" @@ -785,6 +822,11 @@ dependencies = [ "web-sys 0.3.39 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proc-macro-hack" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "0.4.30" @@ -1089,6 +1131,11 @@ dependencies = [ "winapi-util 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wasm-bindgen" version = "0.2.62" @@ -1307,6 +1354,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum ab_glyph_rasterizer 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2b7e4e8cf778db814365e46839949ca74df4efb10e87ba4913e6ec5967ef0285" "checksum addr2line 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a49806b9dadc843c61e7c97e72490ad7f7220ae249012fbda9ad0609457c0543" +"checksum ahash 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2f3e0bf23f51883cce372d5d5892211236856e4bb37fb942e1eb135ee0f146e3" "checksum andrew 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9b7f09f89872c2b6b29e319377b1fbe91c6f5947df19a25596e121cf19a7b35e" "checksum android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "000444226fcff248f2bc4c7625be32c63caccfecc2723a2b9f78a7487a49c407" "checksum approx 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0e60b75072ecd4168020818c0107f2857bb6c4e64252d8d3983f6263b40a5c3" @@ -1328,6 +1376,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum clipboard-win 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e3a093d6fed558e5fe24c3dfc85a68bb68f1c824f440d3ba5aca189e2998786b" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum cocoa 0.18.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1706996401131526e36b3b49f0c4d912639ce110996f3ca144d78946727bce54" +"checksum const-random 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "2f1af9ac737b2dd2d577701e59fd09ba34822f6f2ebdb30a7647405d9e55e16a" +"checksum const-random-macro 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "25e4c606eb459dd29f7c57b2e0879f2b6f14ee130918c2b78ccb58a9624e6c7a" "checksum core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "25b9e03f145fd4f2bf705e07b900cd41fc636598fe5dc452fd0db1441c3f496d" "checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b" "checksum core-graphics 0.17.3 (registry+https://github.com/rust-lang/crates.io-index)" = "56790968ab1c8a1202a102e6de05fc6e1ec87da99e4e93e9a7d13efbfc1e95a9" @@ -1345,6 +1395,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fnv 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +"checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" "checksum gimli 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bcc8e0c9bce37868955864dbecd2b1ab2bdf967e6f28066d65aaac620444b65c" "checksum gl_generator 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "39a23d5e872a275135d66895d954269cf5e8661d234eb1c2480f4ce0d586acbd" "checksum gl_generator 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ca98bbde17256e02d17336a6bdb5a50f7d0ccacee502e191d3e3d0ec2f96f84a" @@ -1390,6 +1441,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" "checksum plotters 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "f9b1d9ca091d370ea3a78d5619145d1b59426ab0c9eedbad2514a4cee08bf389" +"checksum proc-macro-hack 0.5.15 (registry+https://github.com/rust-lang/crates.io-index)" = "0d659fe7c6d27f25e9d80a1a094c223f5246f6a6596453e09d7229bf42750b63" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum proc-macro2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "53f5ffe53a6b28e37c9c1ce74893477864d64f74778a93a4beb43c8fa167f639" "checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" @@ -1429,6 +1481,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" +"checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum wasm-bindgen 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "e3c7d40d09cdbf0f4895ae58cf57d92e1e57a9dd8ed2e8390514b54a47cc5551" "checksum wasm-bindgen-backend 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "c3972e137ebf830900db522d6c8fd74d1900dcfc733462e9a12e942b00b4ac94" "checksum wasm-bindgen-macro 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "2cd85aa2c579e8892442954685f0d801f9129de24fa2136b2c6a539c76b65776" diff --git a/emigui/Cargo.toml b/emigui/Cargo.toml index 24f04240e..9a013319d 100644 --- a/emigui/Cargo.toml +++ b/emigui/Cargo.toml @@ -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" diff --git a/emigui/src/context.rs b/emigui/src/context.rs index 93551eb56..0b77d67e5 100644 --- a/emigui/src/context.rs +++ b/emigui/src/context.rs @@ -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, output: Mutex, /// Used to debug name clashes of e.g. windows - used_ids: Mutex>, + used_ids: Mutex>, paint_stats: Mutex, } diff --git a/emigui/src/id.rs b/emigui/src/id.rs index 9d2df3b6a..7a46b80c8 100644 --- a/emigui/src/id.rs +++ b/emigui/src/id.rs @@ -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()) diff --git a/emigui/src/layers.rs b/emigui/src/layers.rs index fc7f3d9f4..1324f1b7b 100644 --- a/emigui/src/layers.rs +++ b/emigui/src/layers.rs @@ -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); +pub struct GraphicLayers(AHashMap); impl GraphicLayers { pub fn layer(&mut self, layer: Layer) -> &mut PaintList { diff --git a/emigui/src/paint/font.rs b/emigui/src/paint/font.rs index 56bdd608b..b2700b594 100644 --- a/emigui/src/paint/font.rs +++ b/emigui/src/paint/font.rs @@ -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, // TODO: optimize these lookups, e.g. with binary search or a fast hashmap + glyph_infos: AHashMap, // TODO: see if we can optimize if we switch to a binary search atlas: Arc>, } diff --git a/emigui/src/paint/fonts.rs b/emigui/src/paint/fonts.rs index 107719828..f6e0e705d 100644 --- a/emigui/src/paint/fonts.rs +++ b/emigui/src/paint/fonts.rs @@ -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(); }