Worse than fx (despite me hashing the station names there too) better than vanilla

This commit is contained in:
Fabian Schmidt 2024-08-28 14:07:16 +02:00
parent 07a8e7fc69
commit aaa11c7b94
3 changed files with 41 additions and 4 deletions
src/main/rust/src

View File

@ -1,3 +1,4 @@
use crate::models::hasher::CustomBuildHasher;
use crate::models::station_measurements::StationMeasurements;
use crate::utils::{hash, parse};
use std::collections::HashMap;
@ -12,8 +13,9 @@ pub fn run() {
const FILE_PATH: &str = "../../../measurements.txt";
let now = Instant::now();
thread::scope(|s| {
let mut stations: HashMap<u64, (String, StationMeasurements)> =
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
let hasher = CustomBuildHasher::default();
let mut stations: HashMap<u64, (String, StationMeasurements), CustomBuildHasher> =
HashMap::with_capacity_and_hasher(DEFAULT_HASHMAP_LENGTH, hasher);
let (tx, rx) = mpsc::channel();
let cores = thread::available_parallelism().unwrap().into();
let file = File::open(FILE_PATH).expect("File measurements.txt not found");
@ -44,8 +46,9 @@ pub fn run() {
let file = File::open(FILE_PATH).expect("File measurements.txt not found");
let mut reader = BufReader::new(&file);
reader.seek(SeekFrom::Start(currposition)).unwrap();
let mut t_stations: HashMap<u64, (String, StationMeasurements)> =
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
let hasher = CustomBuildHasher::default();
let mut t_stations: HashMap<u64, (String, StationMeasurements), CustomBuildHasher> =
HashMap::with_capacity_and_hasher(DEFAULT_HASHMAP_LENGTH, hasher);
let mut line = Vec::with_capacity(108);
loop {
let line_len = reader

View File

@ -1,2 +1,3 @@
pub mod mmap;
pub mod station_measurements;
pub mod hasher;

View File

@ -0,0 +1,33 @@
use std::hash::{BuildHasherDefault, Hasher};
use crate::utils::hash;
#[derive(Copy, Clone)]
pub struct CustomHasher(u64);
impl Default for CustomHasher {
#[inline]
fn default() -> Self {
CustomHasher(0)
}
}
impl CustomHasher {
#[inline]
pub fn with_key(key: u64) -> CustomHasher {
CustomHasher(key)
}
}
impl Hasher for CustomHasher {
#[inline]
fn finish(&self) -> u64 {
self.0
}
#[inline]
fn write(&mut self, bytes: &[u8]) {
*self = CustomHasher(hash::bytes(bytes));
}
}
pub type CustomBuildHasher = BuildHasherDefault<CustomHasher>;