From 22a3a75eb5b20f66651a9040ec9bbc6d1771dfcd Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 9 Oct 2021 14:15:45 +0200 Subject: [PATCH] Optimization: replace HashSet uses with AHashSet --- Cargo.lock | 1 + egui/src/memory.rs | 10 +++++----- egui/src/widgets/plot/legend.rs | 11 +++++------ egui/src/widgets/plot/mod.rs | 7 +++---- epaint/Cargo.toml | 8 ++++---- epaint/src/mutex.rs | 2 +- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 92fa74b54..adbfcf98a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,6 +47,7 @@ checksum = "43bb833f0bf979d8475d38fbf09ed3b8a55e1885fe93ad3f93239fc6a4f17b98" dependencies = [ "getrandom", "once_cell", + "serde", "version_check", ] diff --git a/egui/src/memory.rs b/egui/src/memory.rs index a646e631c..df7228886 100644 --- a/egui/src/memory.rs +++ b/egui/src/memory.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use epaint::ahash::AHashSet; use crate::{any, area, window, Id, IdMap, InputState, LayerId, Pos2, Rect, Style}; @@ -464,15 +464,15 @@ pub struct Areas { areas: IdMap, /// Back-to-front. Top is last. order: Vec, - visible_last_frame: HashSet, - visible_current_frame: HashSet, + visible_last_frame: AHashSet, + visible_current_frame: AHashSet, /// When an area want to be on top, it is put in here. /// At the end of the frame, this is used to reorder the layers. /// This means if several layers want to be on top, they will keep their relative order. /// So if you close three windows and then reopen them all in one frame, /// they will all be sent to the top, but keep their previous internal order. - wants_to_be_on_top: HashSet, + wants_to_be_on_top: AHashSet, } impl Areas { @@ -524,7 +524,7 @@ impl Areas { self.visible_last_frame.contains(layer_id) || self.visible_current_frame.contains(layer_id) } - pub fn visible_layer_ids(&self) -> HashSet { + pub fn visible_layer_ids(&self) -> AHashSet { self.visible_last_frame .iter() .cloned() diff --git a/egui/src/widgets/plot/legend.rs b/egui/src/widgets/plot/legend.rs index f461f6225..676480494 100644 --- a/egui/src/widgets/plot/legend.rs +++ b/egui/src/widgets/plot/legend.rs @@ -1,7 +1,6 @@ -use std::{ - collections::{BTreeMap, HashSet}, - string::String, -}; +use std::{collections::BTreeMap, string::String}; + +use epaint::ahash::AHashSet; use crate::*; @@ -167,7 +166,7 @@ impl LegendWidget { rect: Rect, config: Legend, items: &[Box], - hidden_items: &HashSet, + hidden_items: &AHashSet, ) -> Option { // Collect the legend entries. If multiple items have the same name, they share a // checkbox. If their colors don't match, we pick a neutral color for the checkbox. @@ -198,7 +197,7 @@ impl LegendWidget { } // Get the names of the hidden items. - pub fn get_hidden_items(&self) -> HashSet { + pub fn get_hidden_items(&self) -> AHashSet { self.entries .iter() .filter(|(_, entry)| !entry.checked) diff --git a/egui/src/widgets/plot/mod.rs b/egui/src/widgets/plot/mod.rs index 74b873e08..1576093b1 100644 --- a/egui/src/widgets/plot/mod.rs +++ b/egui/src/widgets/plot/mod.rs @@ -4,8 +4,6 @@ mod items; mod legend; mod transform; -use std::collections::HashSet; - use items::PlotItem; pub use items::{ Arrows, HLine, Line, LineStyle, MarkerShape, PlotImage, Points, Polygon, Text, VLine, Value, @@ -17,6 +15,7 @@ use transform::{Bounds, ScreenTransform}; use crate::*; use color::Hsva; +use epaint::ahash::AHashSet; // ---------------------------------------------------------------------------- @@ -27,7 +26,7 @@ struct PlotMemory { bounds: Bounds, auto_bounds: bool, hovered_entry: Option, - hidden_items: HashSet, + hidden_items: AHashSet, min_auto_bounds: Bounds, } @@ -350,7 +349,7 @@ impl Widget for Plot { bounds: min_auto_bounds, auto_bounds: !min_auto_bounds.is_valid(), hovered_entry: None, - hidden_items: HashSet::new(), + hidden_items: Default::default(), min_auto_bounds, }) .clone(); diff --git a/epaint/Cargo.toml b/epaint/Cargo.toml index 7b0a4bb33..60cc7a8f4 100644 --- a/epaint/Cargo.toml +++ b/epaint/Cargo.toml @@ -39,6 +39,9 @@ bytemuck = { version = "1.7.2", features = ["derive"], optional = true } [features] default = ["default_fonts", "multi_threaded"] +# implement bytemuck on most types. +convert_bytemuck = ["bytemuck", "emath/bytemuck"] + # If set, epaint will use `include_bytes!` to bundle some fonts. # If you plan on specifying your own fonts you may disable this feature. default_fonts = [] @@ -52,10 +55,7 @@ extra_asserts = ["emath/extra_asserts"] mint = ["emath/mint"] # implement serde on most types. -serialize = ["serde", "emath/serde"] - -# implement bytemuck on most types. -convert_bytemuck = ["bytemuck", "emath/bytemuck"] +serialize = ["serde", "ahash/serde", "emath/serde"] single_threaded = ["atomic_refcell"] diff --git a/epaint/src/mutex.rs b/epaint/src/mutex.rs index dec4d86b8..e0163ddb2 100644 --- a/epaint/src/mutex.rs +++ b/epaint/src/mutex.rs @@ -24,7 +24,7 @@ pub struct Mutex(parking_lot::Mutex); #[cfg(debug_assertions)] thread_local! { - static HELD_LOCKS_TLS: std::cell::RefCell> = std::cell::RefCell::new(std::collections::HashSet::new()); + static HELD_LOCKS_TLS: std::cell::RefCell> = std::cell::RefCell::new(ahash::AHashSet::new()); } #[cfg(feature = "multi_threaded")]