Skip to content

Commit 9f20bad

Browse files
Steven Gumichaelwoerister
authored andcommitted
Replaces HashMap with rustc_hash::FxHashMap in Tools.
1 parent ba0ecee commit 9f20bad

File tree

7 files changed

+25
-21
lines changed

7 files changed

+25
-21
lines changed

crox/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2018"
66

77
[dependencies]
88
measureme = { "path" = "../measureme" }
9+
rustc-hash = "1.0.1"
910
serde = { version = "1.0", features = [ "derive" ] }
1011
serde_json = "1.0"
1112
structopt = "0.2"

crox/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::BTreeMap;
1+
use rustc_hash::FxHashMap;
22
use std::fs;
33
use std::io::BufWriter;
44
use std::path::PathBuf;
@@ -38,7 +38,7 @@ struct Event {
3838
process_id: u32,
3939
#[serde(rename = "tid")]
4040
thread_id: u64,
41-
args: Option<BTreeMap<String, String>>,
41+
args: Option<FxHashMap<String, String>>,
4242
}
4343

4444
#[derive(StructOpt, Debug)]
@@ -56,12 +56,12 @@ struct Opt {
5656
fn generate_thread_to_collapsed_thread_mapping(
5757
opt: &Opt,
5858
data: &ProfilingData,
59-
) -> BTreeMap<u64, u64> {
60-
let mut thread_to_collapsed_thread: BTreeMap<u64, u64> = BTreeMap::new();
59+
) -> FxHashMap<u64, u64> {
60+
let mut thread_to_collapsed_thread: FxHashMap<u64, u64> = FxHashMap::default();
6161

6262
if opt.collapse_threads {
6363
// collect start and end times for all threads
64-
let mut thread_start_and_end: BTreeMap<u64, (SystemTime, SystemTime)> = BTreeMap::new();
64+
let mut thread_start_and_end: FxHashMap<u64, (SystemTime, SystemTime)> = FxHashMap::default();
6565
for event in data.iter() {
6666
thread_start_and_end
6767
.entry(event.thread_id)

summarize/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license = "MIT OR Apache-2.0"
88
[dependencies]
99
measureme = { path = "../measureme" }
1010
prettytable-rs = "0.8"
11+
rustc-hash = "1.0.1"
1112
serde = { version = "1.0", features = [ "derive" ] }
1213
serde_json = "1.0"
1314
structopt = "0.2"

summarize/src/analysis.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::query_data::{QueryData, Results};
22
use measureme::rustc::*;
33
use measureme::{Event, ProfilingData, Timestamp};
4+
use rustc_hash::FxHashMap;
45
use std::borrow::Cow;
5-
use std::collections::HashMap;
6-
use std::time::{Duration, SystemTime};
6+
use std::time::SystemTime;
77

88
/// Collects accumulated summary data for the given ProfilingData.
99
///
@@ -116,8 +116,8 @@ pub fn perform_analysis(data: ProfilingData) -> Results {
116116
end: SystemTime,
117117
}
118118

119-
let mut query_data = HashMap::<String, QueryData>::new();
120-
let mut threads = HashMap::<_, PerThreadState>::new();
119+
let mut query_data = FxHashMap::<String, QueryData>::default();
120+
let mut threads = FxHashMap::<_, PerThreadState>::default();
121121

122122
let mut record_event_data = |label: &Cow<'_, str>, f: &dyn Fn(&mut QueryData)| {
123123
if let Some(data) = query_data.get_mut(&label[..]) {
@@ -219,6 +219,7 @@ pub fn perform_analysis(data: ProfilingData) -> Results {
219219
#[cfg(test)]
220220
mod tests {
221221
use super::*;
222+
use std::time::Duration;
222223
use measureme::ProfilingDataBuilder;
223224

224225
#[test]

summarize/src/diff.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::query_data::{QueryData, QueryDataDiff, Results};
22
use crate::signed_duration::SignedDuration;
3+
use rustc_hash::{FxHashMap, FxHashSet};
34
use serde::{Deserialize, Serialize};
4-
use std::collections::{HashMap, HashSet};
55
use std::time::Duration;
66

77
#[derive(Serialize, Deserialize)]
@@ -10,8 +10,8 @@ pub struct DiffResults {
1010
pub total_time: SignedDuration,
1111
}
1212

13-
fn build_query_lookup(query_data: &[QueryData]) -> HashMap<&str, usize> {
14-
let mut lookup = HashMap::with_capacity(query_data.len());
13+
fn build_query_lookup(query_data: &[QueryData]) -> FxHashMap<&str, usize> {
14+
let mut lookup = FxHashMap::with_capacity_and_hasher(query_data.len(), Default::default());
1515
for i in 0..query_data.len() {
1616
lookup.insert(&query_data[i].label[..], i);
1717
}
@@ -28,7 +28,7 @@ pub fn calculate_diff(base: Results, change: Results) -> DiffResults {
2828
let base_data = build_query_lookup(&base.query_data);
2929
let change_data = build_query_lookup(&change.query_data);
3030

31-
let mut all_labels = HashSet::with_capacity(base.query_data.len() + change.query_data.len());
31+
let mut all_labels = FxHashSet::with_capacity_and_hasher(base.query_data.len() + change.query_data.len(), Default::default());
3232
for query_data in base.query_data.iter().chain(&change.query_data) {
3333
all_labels.insert(&query_data.label[..]);
3434
}

tools_lib/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2018"
66
license = "MIT OR Apache-2.0"
77

88
[dependencies]
9-
measureme = { path = "../measureme" }
9+
measureme = { path = "../measureme" }
10+
rustc-hash = "1.0.1"

tools_lib/src/stack_collapse.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use rustc_hash::FxHashMap;
12
use std::cmp;
2-
use std::collections::HashMap;
33
use std::time::SystemTime;
44

55
use measureme::{Event, ProfilingData};
@@ -17,9 +17,9 @@ struct PerThreadState<'a> {
1717
/// Uses a variation of the algorithm in `summarize`.
1818
// Original implementation provided by @andjo403 in
1919
// https://github.com/michaelwoerister/measureme/pull/1
20-
pub fn collapse_stacks<'a>(profiling_data: &ProfilingData) -> HashMap<String, u64> {
21-
let mut counters = HashMap::new();
22-
let mut threads = HashMap::<_, PerThreadState<'_>>::new();
20+
pub fn collapse_stacks<'a>(profiling_data: &ProfilingData) -> FxHashMap<String, u64> {
21+
let mut counters = FxHashMap::default();
22+
let mut threads = FxHashMap::<_, PerThreadState<'_>>::default();
2323

2424
for current_event in profiling_data
2525
.iter()
@@ -95,7 +95,7 @@ pub fn collapse_stacks<'a>(profiling_data: &ProfilingData) -> HashMap<String, u6
9595
#[cfg(test)]
9696
mod test {
9797
use measureme::ProfilingDataBuilder;
98-
use std::collections::HashMap;
98+
use rustc_hash::FxHashMap;
9999

100100
#[test]
101101
fn basic_test() {
@@ -124,7 +124,7 @@ mod test {
124124

125125
let recorded_stacks = super::collapse_stacks(&profiling_data);
126126

127-
let mut expected_stacks = HashMap::<String, u64>::new();
127+
let mut expected_stacks = FxHashMap::<String, u64>::default();
128128
expected_stacks.insert("rustc;e2;e1;e3".into(), 2);
129129
expected_stacks.insert("rustc;e2;e1".into(), 2);
130130
expected_stacks.insert("rustc;e2".into(), 2);
@@ -161,7 +161,7 @@ mod test {
161161

162162
let recorded_stacks = super::collapse_stacks(&profiling_data);
163163

164-
let mut expected_stacks = HashMap::<String, u64>::new();
164+
let mut expected_stacks = FxHashMap::<String, u64>::default();
165165
expected_stacks.insert("rustc;e2;e3".into(), 1);
166166
expected_stacks.insert("rustc;e2".into(), 2);
167167
expected_stacks.insert("rustc;e1".into(), 3);

0 commit comments

Comments
 (0)