added multithreaded version, no mmap optimization
This commit is contained in:
parent
003cf323c4
commit
4fdef18833
|
@ -3,8 +3,6 @@ name = "onebrc"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fxhash = "0.2.1"
|
fxhash = "0.2.1"
|
||||||
rayon = "1.8.0"
|
rayon = "1.8.0"
|
||||||
|
|
|
@ -2,5 +2,4 @@
|
||||||
|
|
||||||
* Fastest single threaded runtime: 29.7s (4.5s to read the file into memory, 24s to process rows and ~150micros to generate the output)
|
* Fastest single threaded runtime: 29.7s (4.5s to read the file into memory, 24s to process rows and ~150micros to generate the output)
|
||||||
|
|
||||||
|
* Fastest multi threaded runtime on a machine with 12 cores: read = 4.682276171s processed = 2.057576429s output_gen = 188.113µs
|
||||||
* Fastest multi threaded runtime on a machine with 12 cores: -
|
|
||||||
|
|
36
src/main.rs
36
src/main.rs
|
@ -1,10 +1,8 @@
|
||||||
use fxhash::FxHashMap;
|
use fxhash::FxHashMap;
|
||||||
use std::fs::File;
|
use rayon::prelude::*;
|
||||||
use std::io::Read;
|
use std::{fs::File, io::Read, time::Instant};
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut map = FxHashMap::default();
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
|
|
||||||
let t1 = Instant::now();
|
let t1 = Instant::now();
|
||||||
|
@ -18,10 +16,12 @@ fn main() {
|
||||||
|
|
||||||
let t2 = Instant::now();
|
let t2 = Instant::now();
|
||||||
|
|
||||||
for line in buf.split(|&x| x == b'\n') {
|
let map: FxHashMap<&[u8], (i16, i32, i16, usize)> = buf
|
||||||
|
.par_split(|&x| x == b'\n')
|
||||||
|
.filter_map(|line| {
|
||||||
let l = line.len();
|
let l = line.len();
|
||||||
if l == 0 {
|
if l == 0 {
|
||||||
continue;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let loc = if line[l - 6] == b';' {
|
let loc = if line[l - 6] == b';' {
|
||||||
|
@ -39,16 +39,36 @@ fn main() {
|
||||||
|
|
||||||
let val = parse_float(val);
|
let val = parse_float(val);
|
||||||
|
|
||||||
let (min, sum, max, counter) = map
|
Some((city, val))
|
||||||
|
})
|
||||||
|
.fold(FxHashMap::default, |mut a, (city, val)| {
|
||||||
|
let (min, sum, max, counter) = a
|
||||||
.entry(city)
|
.entry(city)
|
||||||
.or_insert_with(|| (std::i16::MAX, 0, std::i16::MIN, 0));
|
.or_insert_with(|| (std::i16::MAX, 0, std::i16::MIN, 0usize));
|
||||||
|
|
||||||
*min = val.min(*min);
|
*min = val.min(*min);
|
||||||
*max = val.max(*max);
|
*max = val.max(*max);
|
||||||
*counter += 1;
|
*counter += 1;
|
||||||
*sum += val as i32;
|
*sum += val as i32;
|
||||||
|
|
||||||
|
a
|
||||||
|
})
|
||||||
|
.reduce_with(|mut m1, m2| {
|
||||||
|
for (k, (min1, sum1, max1, counter1)) in m2 {
|
||||||
|
let (min, sum, max, counter) = m1
|
||||||
|
.entry(k)
|
||||||
|
.or_insert_with(|| (std::i16::MAX, 0, std::i16::MIN, 0usize));
|
||||||
|
|
||||||
|
*min = min1.min(*min);
|
||||||
|
*max = max1.max(*max);
|
||||||
|
*counter += counter1;
|
||||||
|
*sum += sum1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m1
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let t2_elapsed = t2.elapsed();
|
let t2_elapsed = t2.elapsed();
|
||||||
|
|
||||||
let t3 = Instant::now();
|
let t3 = Instant::now();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user