Skip to content

Commit 109f9fa

Browse files
Merge pull request #972 from Object905/master
Replace default hasher for HashSet and HashMap on fnv.
2 parents 30ebfa1 + 790d35b commit 109f9fa

File tree

11 files changed

+42
-27
lines changed

11 files changed

+42
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ path = "./src/lib.rs"
2020

2121
[dependencies]
2222
daggy = "0.4.0"
23+
fnv = "1.0"
2324
num = "0.1.30"
2425
pistoncore-input = "0.17.0"
2526
rusttype = "0.2.0"

examples/support/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn theme() -> conrod::Theme {
6464
font_size_large: 26,
6565
font_size_medium: 18,
6666
font_size_small: 12,
67-
widget_styling: std::collections::HashMap::new(),
67+
widget_styling: conrod::theme::StyleMap::default(),
6868
mouse_drag_threshold: 0.0,
6969
double_click_threshold: std::time::Duration::from_millis(500),
7070
}

src/graph/algo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use daggy::Walker;
99
use position::{Point, Rect};
10-
use std;
10+
use fnv;
1111
use super::{EdgeIndex, Graph};
1212
use widget;
1313

@@ -192,7 +192,7 @@ fn cropped_area_of_widget_maybe_within_depth(graph: &Graph,
192192
/// depth_children for the total bounding box. This should use a proper `Dfs` type with it's own
193193
/// stack for safer traversal that won't blow the stack on hugely deep GUIs.
194194
pub fn kids_bounding_box(graph: &Graph,
195-
prev_updated: &std::collections::HashSet<widget::Id>,
195+
prev_updated: &fnv::FnvHashSet<widget::Id>,
196196
idx: widget::Id) -> Option<Rect>
197197
{
198198
// When traversing the `depth_kids`, we only want to visit those who:

src/graph/depth_order.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use daggy::Walker;
44
use std;
5+
use fnv;
56
use super::{Graph, Node};
67
use widget;
78

@@ -52,7 +53,7 @@ impl DepthOrder {
5253
pub fn update(&mut self,
5354
graph: &Graph,
5455
root: widget::Id,
55-
updated_widgets: &std::collections::HashSet<widget::Id>)
56+
updated_widgets: &fnv::FnvHashSet<widget::Id>)
5657
{
5758
let DepthOrder { ref mut indices, ref mut floating } = *self;
5859

@@ -90,7 +91,7 @@ impl DepthOrder {
9091
/// Recursive function for visiting all nodes within the dag.
9192
fn visit_by_depth(graph: &Graph,
9293
idx: widget::Id,
93-
updated_widgets: &std::collections::HashSet<widget::Id>,
94+
updated_widgets: &fnv::FnvHashSet<widget::Id>,
9495
depth_order: &mut Vec<widget::Id>,
9596
floating_deque: &mut Vec<widget::Id>)
9697
{

src/image.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! - [Map](./struct.Map.html)
44
55
use std;
6+
use fnv;
67

78
/// Unique image identifier.
89
///
@@ -33,8 +34,8 @@ pub struct Map<Img> {
3334
pub trigger_redraw: std::cell::Cell<bool>,
3435
}
3536

36-
/// The type of `std::collections::HashMap` used within the `image::Map`.
37-
pub type HashMap<Img> = std::collections::HashMap<Id, Img>;
37+
/// The type of `std::collections::HashMap` with `fnv::FnvHasher` used within the `image::Map`.
38+
pub type HashMap<Img> = fnv::FnvHashMap<Id, Img>;
3839

3940
/// An iterator yielding an `Id` for each new `Img` inserted into the `Map` via the `extend`
4041
/// method.
@@ -57,7 +58,7 @@ impl<Img> Map<Img> {
5758
pub fn new() -> Self {
5859
Map {
5960
next_index: 0,
60-
map: std::collections::HashMap::new(),
61+
map: HashMap::<Img>::default(),
6162
trigger_redraw: std::cell::Cell::new(true),
6263
}
6364
}

src/input/state.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
use position::Point;
1111
use self::mouse::Mouse;
12-
use std;
12+
use fnv;
1313
use super::keyboard::{NO_MODIFIER, ModifierKey};
1414
use utils;
1515
use widget;
@@ -26,7 +26,7 @@ pub struct State {
2626
/// Mouse position and button state.
2727
pub mouse: Mouse,
2828
/// All in-progress touch interactions.
29-
pub touch: std::collections::HashMap<super::touch::Id, self::touch::Touch>,
29+
pub touch: fnv::FnvHashMap<super::touch::Id, self::touch::Touch>,
3030
/// Which widget, if any, is currently capturing the keyboard
3131
pub widget_capturing_keyboard: Option<widget::Id>,
3232
/// Which widget, if any, is currently capturing the mouse
@@ -45,7 +45,7 @@ impl State {
4545
/// Returns a fresh new input state
4646
pub fn new() -> State {
4747
State{
48-
touch: std::collections::HashMap::new(),
48+
touch: fnv::FnvHashMap::default(),
4949
mouse: Mouse::new(),
5050
widget_capturing_keyboard: None,
5151
widget_capturing_mouse: None,

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![warn(missing_docs)]
1010

1111
extern crate daggy;
12+
extern crate fnv;
1213
extern crate num;
1314
extern crate input as piston_input;
1415
extern crate rusttype;

src/text.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl<'a, I> Iterator for Lines<'a, I>
9191

9292
/// The `font::Id` and `font::Map` types.
9393
pub mod font {
94+
use fnv;
9495
use std;
9596

9697
/// A type-safe wrapper around the `FontId`.
@@ -105,7 +106,7 @@ pub mod font {
105106
/// A collection of mappings from `font::Id`s to `rusttype::Font`s.
106107
pub struct Map {
107108
next_index: usize,
108-
map: std::collections::HashMap<Id, super::Font>,
109+
map: fnv::FnvHashMap<Id, super::Font>,
109110
}
110111

111112
/// An iterator yielding an `Id` for each new `rusttype::Font` inserted into the `Map` via the
@@ -144,7 +145,7 @@ pub mod font {
144145
pub fn new() -> Self {
145146
Map {
146147
next_index: 0,
147-
map: std::collections::HashMap::new(),
148+
map: fnv::FnvHashMap::default(),
148149
}
149150
}
150151

src/theme.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
use Scalar;
66
use color::{Color, BLACK, WHITE};
77
use position::{Align, Direction, Padding, Position, Relative};
8+
use fnv;
89
use std;
910
use std::any::Any;
1011
use text;
1112
use widget;
1213

14+
/// `std::collections::HashMap` with `fnv::FnvHasher` for unique styling
15+
/// of each widget, index-able by the **Widget::kind**.
16+
pub type StyleMap = fnv::FnvHashMap<std::any::TypeId, WidgetDefault>;
17+
1318

1419
/// A serializable collection of canvas and widget styling defaults.
1520
pub struct Theme {
@@ -39,8 +44,9 @@ pub struct Theme {
3944
pub font_size_medium: u32,
4045
/// A default "small" font size.
4146
pub font_size_small: u32,
42-
/// Unique styling for each widget, index-able by the **Widget::kind**.
43-
pub widget_styling: std::collections::HashMap<std::any::TypeId, WidgetDefault>,
47+
/// `StyleMap` for unique styling
48+
/// of each widget, index-able by the **Widget::kind**.
49+
pub widget_styling: StyleMap,
4450
/// Mouse Drag distance threshold determines the minimum distance from the mouse-down point
4551
/// that the mouse must move before starting a drag operation.
4652
pub mouse_drag_threshold: Scalar,
@@ -94,7 +100,7 @@ impl Theme {
94100
font_size_large: 26,
95101
font_size_medium: 18,
96102
font_size_small: 12,
97-
widget_styling: std::collections::HashMap::new(),
103+
widget_styling: fnv::FnvHashMap::default(),
98104
mouse_drag_threshold: 0.0,
99105
double_click_threshold: std::time::Duration::from_millis(500),
100106
}

src/ui.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use input;
55
use position::{self, Align, Direction, Dimensions, Padding, Point, Position, Range, Rect, Scalar};
66
use render;
77
use std;
8+
use fnv;
89
use text;
910
use theme::Theme;
1011
use utils;
@@ -67,12 +68,12 @@ pub struct Ui {
6768
/// The order in which widgets from the `widget_graph` are drawn.
6869
depth_order: graph::DepthOrder,
6970
/// The set of widgets that have been updated since the beginning of the `set_widgets` stage.
70-
updated_widgets: std::collections::HashSet<widget::Id>,
71+
updated_widgets: fnv::FnvHashSet<widget::Id>,
7172
/// The `updated_widgets` for the previous `set_widgets` stage.
7273
///
7374
/// We use this to compare against the newly generated `updated_widgets` to see whether or not
7475
/// we require re-drawing.
75-
prev_updated_widgets: std::collections::HashSet<widget::Id>,
76+
prev_updated_widgets: fnv::FnvHashSet<widget::Id>,
7677
/// Scroll events that have been emitted during a call to `Ui::set_widgets`. These are usually
7778
/// emitted by some widget like the `Scrollbar`.
7879
///
@@ -172,10 +173,11 @@ impl Ui {
172173
maybe_widgets_capacity.map_or_else(
173174
|| (Graph::new(),
174175
graph::DepthOrder::new(),
175-
std::collections::HashSet::new()),
176+
fnv::FnvHashSet::default()),
176177
|n| (Graph::with_node_capacity(n),
177178
graph::DepthOrder::with_node_capacity(n),
178-
std::collections::HashSet::with_capacity(n)));
179+
std::collections::HashSet::with_capacity_and_hasher(n,
180+
fnv::FnvBuildHasher::default())));
179181

180182
let window = widget_graph.add_placeholder();
181183
let prev_updated_widgets = updated_widgets.clone();
@@ -270,15 +272,15 @@ impl Ui {
270272
///
271273
/// This set indicates which widgets have been instantiated since the beginning of the most
272274
/// recent `Ui::set_widgets` call.
273-
pub fn updated_widgets(&self) -> &std::collections::HashSet<widget::Id> {
275+
pub fn updated_widgets(&self) -> &fnv::FnvHashSet<widget::Id> {
274276
&self.updated_widgets
275277
}
276278

277279
/// Borrow the **Ui**'s set of updated widgets.
278280
///
279281
/// This set indicates which widgets have were instantiated during the previous call to
280282
/// `Ui::set_widgets`.
281-
pub fn prev_updated_widgets(&self) -> &std::collections::HashSet<widget::Id> {
283+
pub fn prev_updated_widgets(&self) -> &fnv::FnvHashSet<widget::Id> {
282284
&self.prev_updated_widgets
283285
}
284286

0 commit comments

Comments
 (0)