Formatting

This commit is contained in:
Fabian Schmidt 2024-08-01 10:23:14 +02:00
parent 34768d3ec1
commit 8eefe06e8b
2 changed files with 36 additions and 27 deletions

View File

@ -1,14 +1,10 @@
#![feature(slice_split_once)] #![feature(slice_split_once)]
use std::{
fs::File,
io::BufReader,
thread,
};
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{BufRead, Seek, SeekFrom}; use std::io::{BufRead, Seek, SeekFrom};
use std::sync::mpsc; use std::sync::mpsc;
use std::time::Instant; use std::time::Instant;
use std::{fs::File, io::BufReader, thread};
use onebrc::{hashstr, parse_temp, StationMeasurements}; use onebrc::{hashstr, parse_temp, StationMeasurements};
@ -18,7 +14,8 @@ fn main() {
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)> = HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH); let mut stations: HashMap<usize, (String, StationMeasurements)> =
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
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 file = File::open(FILE_PATH).expect("File measurements.txt not found"); let file = File::open(FILE_PATH).expect("File measurements.txt not found");
@ -30,9 +27,13 @@ fn main() {
for i in 1..cores { for i in 1..cores {
let mut reader = BufReader::new(&file); let mut reader = BufReader::new(&file);
let mut byte_start = chunk_length * i; 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 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; byte_start += line_len;
bounds.push(byte_start as u64); bounds.push(byte_start as u64);
} }
@ -40,7 +41,7 @@ fn main() {
for i in 0..cores { for i in 0..cores {
let tx = tx.clone(); let tx = tx.clone();
let mut currposition = *bounds.get(i).unwrap(); let mut currposition = *bounds.get(i).unwrap();
let end = *bounds.get(i+1).unwrap(); let end = *bounds.get(i + 1).unwrap();
s.spawn(move || { s.spawn(move || {
let file = File::open(FILE_PATH).expect("File measurements.txt not found"); let file = File::open(FILE_PATH).expect("File measurements.txt not found");
let mut reader = BufReader::new(&file); let mut reader = BufReader::new(&file);
@ -49,7 +50,9 @@ fn main() {
HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH); HashMap::with_capacity(DEFAULT_HASHMAP_LENGTH);
let mut line = Vec::with_capacity(108); let mut line = Vec::with_capacity(108);
loop { 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 { if line_len == 0 {
break; break;
} }
@ -89,17 +92,20 @@ fn main() {
} }
} }
} }
let mut stations: Vec<String> = stations.iter().map(|(_, (station, measurements))| { let mut stations: Vec<String> = stations
let measurements = measurements.to_string(); .iter()
#[cfg(feature = "json")] .map(|(_, (station, measurements))| {
{ let measurements = measurements.to_string();
format!("{{\"{station}\":\"{measurements}\"}}") #[cfg(feature = "json")]
} {
#[cfg(not(feature = "json"))] format!("{{\"{station}\":\"{measurements}\"}}")
{ }
format!("{station}={measurements}") #[cfg(not(feature = "json"))]
} {
}).collect(); format!("{station}={measurements}")
}
})
.collect();
stations.sort(); stations.sort();
let stations = stations.join(","); let stations = stations.join(",");
#[cfg(feature = "json")] #[cfg(feature = "json")]

View File

@ -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]), (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, 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]), (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 { if is_negative {
-as_decimal -as_decimal
@ -127,13 +130,11 @@ pub fn new_parse_temp(bytes: &[u8]) -> isize {
#[inline] #[inline]
pub fn get_pos(bytes: &[u8], find: u8) -> Option<u32> { pub fn get_pos(bytes: &[u8], find: u8) -> Option<u32> {
let chunks = bytes.windows(4); let chunks = bytes.windows(4);
let mut pos = 0; for (pos, chunk) in chunks.enumerate() {
for chunk in chunks {
let inner_pos = get_pos_in_chunk(chunk, find); let inner_pos = get_pos_in_chunk(chunk, find);
if inner_pos < chunk.len() as u32 { if inner_pos < chunk.len() as u32 {
return Some(pos + inner_pos); return Some(pos as u32 + inner_pos);
} }
pos += 1;
} }
None None
} }
@ -193,7 +194,9 @@ mod tests {
#[test] #[test]
fn test_getpos() { 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 semi_bytes = semi_bytes.as_slice();
let pos = get_pos(semi_bytes, b';').unwrap(); let pos = get_pos(semi_bytes, b';').unwrap();
assert_eq!(pos, 8); assert_eq!(pos, 8);