Use FxHashMap for multi_threaded_smol.rs

This commit is contained in:
Fabian Schmidt 2024-08-19 10:57:18 +02:00
parent 7add8793a5
commit 2a89d061a0

View File

@ -1,10 +1,9 @@
use smol::fs::File; use smol::fs::File;
use smol::io::{AsyncBufReadExt, AsyncSeekExt, BufReader, SeekFrom}; use smol::io::{AsyncBufReadExt, AsyncSeekExt, BufReader, SeekFrom};
use rustc_hash::{FxHashMap as HashMap, FxBuildHasher};
use crate::models::station_measurements::StationMeasurements; use crate::models::station_measurements::StationMeasurements;
use crate::utils::parse; use crate::utils::parse;
use crate::utils::parse::hashstr;
use std::collections::HashMap;
use std::sync::mpsc; use std::sync::mpsc;
use std::thread; use std::thread;
use std::time::Instant; use std::time::Instant;
@ -15,8 +14,9 @@ pub fn run() {
const FILE_PATH: &str = "../../../measurements.txt"; const FILE_PATH: &str = "../../../measurements.txt";
let now = Instant::now(); let now = Instant::now();
thread::scope(|s| { thread::scope(|s| {
let mut stations: HashMap<usize, (String, StationMeasurements)> = let hasher = FxBuildHasher::default();
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH); let mut stations: HashMap<String, StationMeasurements> =
HashMap::with_capacity_and_hasher(DEFAULT_HASHMAP_LENGTH, hasher);
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let cores = thread::available_parallelism().unwrap().into(); let cores = thread::available_parallelism().unwrap().into();
let bounds = smol::block_on(async { let bounds = smol::block_on(async {
@ -57,8 +57,8 @@ pub fn run() {
.expect("File measurements.txt not found"); .expect("File measurements.txt not found");
let mut reader = BufReader::new(&mut file); let mut reader = BufReader::new(&mut file);
reader.seek(SeekFrom::Start(currposition)).await.unwrap(); reader.seek(SeekFrom::Start(currposition)).await.unwrap();
let mut t_stations: HashMap<usize, (String, StationMeasurements)> = let mut t_stations: HashMap<String, StationMeasurements> =
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH); HashMap::with_capacity_and_hasher(DEFAULT_HASHMAP_LENGTH, hasher);
let mut line = Vec::with_capacity(108); let mut line = Vec::with_capacity(108);
loop { loop {
let line_len = reader let line_len = reader
@ -69,11 +69,10 @@ pub fn run() {
break; break;
} }
let (station, temp) = line.rsplit_once(|&byte| byte == b';').unwrap(); let (station, temp) = line.rsplit_once(|&byte| byte == b';').unwrap();
let hash = hashstr(station);
let station = unsafe { String::from_utf8_unchecked(Vec::from(station)) }; let station = unsafe { String::from_utf8_unchecked(Vec::from(station)) };
let temp = parse::temp(temp.split_last().unwrap().1); let temp = parse::temp(temp.split_last().unwrap().1);
let measurements_option = t_stations.get_mut(&hash); let measurements_option = t_stations.get_mut(&station);
if let Some((_, measurements)) = measurements_option { if let Some(measurements) = measurements_option {
measurements.update(temp); measurements.update(temp);
} else { } else {
let measurements = StationMeasurements { let measurements = StationMeasurements {
@ -82,7 +81,7 @@ pub fn run() {
count: 1, count: 1,
sum: temp, sum: temp,
}; };
t_stations.insert(hash, (station, measurements)); t_stations.insert(station, measurements);
} }
currposition += line_len as u64; currposition += line_len as u64;
if currposition >= end { if currposition >= end {
@ -96,18 +95,18 @@ pub fn run() {
} }
drop(tx); drop(tx);
while let Ok(t_stations) = rx.recv() { while let Ok(t_stations) = rx.recv() {
for (&hash, (station, measurements)) in t_stations.iter() { for (station, measurements) in t_stations.iter() {
let joined_measurements_options = stations.get_mut(&hash); let joined_measurements_options = stations.get_mut(station);
if let Some((_, joined_measurements)) = joined_measurements_options { if let Some(joined_measurements) = joined_measurements_options {
joined_measurements.merge(measurements); joined_measurements.merge(measurements);
} else { } else {
stations.insert(hash, (station.to_owned(), *measurements)); stations.insert(station.to_owned(), *measurements);
} }
} }
} }
let mut stations: Vec<String> = stations let mut stations: Vec<String> = stations
.iter() .iter()
.map(|(_, (station, measurements))| { .map(|(station, measurements)| {
let measurements = measurements.to_string(); let measurements = measurements.to_string();
#[cfg(feature = "json")] #[cfg(feature = "json")]
{ {