From 8eefe06e8b5fae21e853a1c132d509261555c38a Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Thu, 1 Aug 2024 10:23:14 +0200 Subject: [PATCH] Formatting --- src/main/rust/src/bin/multi_threaded.rs | 48 ++++++++++++++----------- src/main/rust/src/lib.rs | 15 ++++---- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/main/rust/src/bin/multi_threaded.rs b/src/main/rust/src/bin/multi_threaded.rs index 4cedf7b..84a3369 100644 --- a/src/main/rust/src/bin/multi_threaded.rs +++ b/src/main/rust/src/bin/multi_threaded.rs @@ -1,14 +1,10 @@ #![feature(slice_split_once)] -use std::{ - fs::File, - io::BufReader, - thread, -}; use std::collections::HashMap; use std::io::{BufRead, Seek, SeekFrom}; use std::sync::mpsc; use std::time::Instant; +use std::{fs::File, io::BufReader, thread}; use onebrc::{hashstr, parse_temp, StationMeasurements}; @@ -18,7 +14,8 @@ fn main() { const FILE_PATH: &str = "../../../measurements.txt"; let now = Instant::now(); thread::scope(|s| { - let mut stations: HashMap = HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH); + let mut stations: HashMap = + HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH); let (tx, rx) = mpsc::channel(); let cores = thread::available_parallelism().unwrap().into(); let file = File::open(FILE_PATH).expect("File measurements.txt not found"); @@ -30,9 +27,13 @@ fn main() { for i in 1..cores { let mut reader = BufReader::new(&file); let mut byte_start = chunk_length * i; - reader.seek(SeekFrom::Start(byte_start as u64)).expect("could not seek"); + reader + .seek(SeekFrom::Start(byte_start as u64)) + .expect("could not seek"); let mut line = Vec::with_capacity(108); - let line_len = reader.read_until(b'\n', &mut line).expect("could not read bytes"); + let line_len = reader + .read_until(b'\n', &mut line) + .expect("could not read bytes"); byte_start += line_len; bounds.push(byte_start as u64); } @@ -40,7 +41,7 @@ fn main() { for i in 0..cores { let tx = tx.clone(); let mut currposition = *bounds.get(i).unwrap(); - let end = *bounds.get(i+1).unwrap(); + let end = *bounds.get(i + 1).unwrap(); s.spawn(move || { let file = File::open(FILE_PATH).expect("File measurements.txt not found"); let mut reader = BufReader::new(&file); @@ -49,7 +50,9 @@ fn main() { HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH); let mut line = Vec::with_capacity(108); loop { - let line_len = reader.read_until(b'\n', &mut line).expect("could not read bytes"); + let line_len = reader + .read_until(b'\n', &mut line) + .expect("could not read bytes"); if line_len == 0 { break; } @@ -89,17 +92,20 @@ fn main() { } } } - let mut stations: Vec = stations.iter().map(|(_, (station, measurements))| { - let measurements = measurements.to_string(); - #[cfg(feature = "json")] - { - format!("{{\"{station}\":\"{measurements}\"}}") - } - #[cfg(not(feature = "json"))] - { - format!("{station}={measurements}") - } - }).collect(); + let mut stations: Vec = stations + .iter() + .map(|(_, (station, measurements))| { + let measurements = measurements.to_string(); + #[cfg(feature = "json")] + { + format!("{{\"{station}\":\"{measurements}\"}}") + } + #[cfg(not(feature = "json"))] + { + format!("{station}={measurements}") + } + }) + .collect(); stations.sort(); let stations = stations.join(","); #[cfg(feature = "json")] diff --git a/src/main/rust/src/lib.rs b/src/main/rust/src/lib.rs index b65777f..393c15e 100644 --- a/src/main/rust/src/lib.rs +++ b/src/main/rust/src/lib.rs @@ -62,7 +62,10 @@ pub fn parse_temp(bytes: &[u8]) -> isize { (true, 5) => get_digit(bytes[1]) * 100 + get_digit(bytes[2]) * 10 + get_digit(bytes[4]), (false, 3) => get_digit(bytes[0]) * 10 + get_digit(bytes[2]), (false, 4) => get_digit(bytes[0]) * 100 + get_digit(bytes[1]) * 10 + get_digit(bytes[3]), - _x => panic!("could not parse temp: is_negative = {is_negative}, length = {}", bytes.len()), + _x => panic!( + "could not parse temp: is_negative = {is_negative}, length = {}", + bytes.len() + ), }; if is_negative { -as_decimal @@ -127,13 +130,11 @@ pub fn new_parse_temp(bytes: &[u8]) -> isize { #[inline] pub fn get_pos(bytes: &[u8], find: u8) -> Option { let chunks = bytes.windows(4); - let mut pos = 0; - for chunk in chunks { + for (pos, chunk) in chunks.enumerate() { let inner_pos = get_pos_in_chunk(chunk, find); if inner_pos < chunk.len() as u32 { - return Some(pos + inner_pos); + return Some(pos as u32 + inner_pos); } - pos += 1; } None } @@ -193,7 +194,9 @@ mod tests { #[test] fn test_getpos() { - let semi_bytes = vec![0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, b';', 0_u8, 0_u8]; + let semi_bytes = vec![ + 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, b';', 0_u8, 0_u8, + ]; let semi_bytes = semi_bytes.as_slice(); let pos = get_pos(semi_bytes, b';').unwrap(); assert_eq!(pos, 8);