diff --git a/src/main/rust/src/implementations/libraries.rs b/src/main/rust/src/implementations/libraries.rs index eef6256..a055ba9 100644 --- a/src/main/rust/src/implementations/libraries.rs +++ b/src/main/rust/src/implementations/libraries.rs @@ -17,7 +17,8 @@ pub fn run() { let mmap_ptr = mmap.as_ptr(); let file_length = mmap.len(); let hasher = FxBuildHasher; - let mut stations: HashMap = + // Even if I could now just use the byte slice as a key, doing the hash is still faster + let mut stations: HashMap = HashMap::with_capacity_and_hasher(DEFAULT_HASHMAP_LENGTH, hasher); let (tx, rx) = mpsc::channel(); let cores = thread::available_parallelism().unwrap().into(); @@ -43,7 +44,7 @@ pub fn run() { let (start, end) = *bounds.get(i).unwrap(); let mmap_slice = unsafe { from_raw_parts(mmap_ptr.add(start), end - start) }; s.spawn(move || { - let mut t_stations: HashMap = + let mut t_stations: HashMap = HashMap::with_capacity_and_hasher(DEFAULT_HASHMAP_LENGTH, hasher); for line in mmap_slice.split(|&byte| byte == b'\n') { if line.is_empty() { @@ -51,7 +52,6 @@ pub fn run() { } let (station, temp) = line.rsplit_once(|&byte| byte == b';').unwrap(); let hash = hash::bytes(station); - let station = unsafe { std::str::from_utf8_unchecked(station) }; let temp = parse::temp(temp); let measurements_option = t_stations.get_mut(&hash); if let Some((_, measurements)) = measurements_option { @@ -63,7 +63,7 @@ pub fn run() { count: 1, sum: temp, }; - t_stations.insert(hash, (station.to_string(), measurements)); + t_stations.insert(hash, (station, measurements)); } } let _ = tx.send(t_stations); @@ -76,13 +76,14 @@ pub fn run() { if let Some((_, joined_measurements)) = joined_measurements_options { joined_measurements.merge(measurements); } else { - stations.insert(*hash, (station.to_owned(), *measurements)); + stations.insert(*hash, (station, *measurements)); } } } let mut stations: Vec = stations .iter() .map(|(_, (station, measurements))| { + let station = unsafe { std::str::from_utf8_unchecked(station) }; let measurements = measurements.to_string(); #[cfg(feature = "json")] {