-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwordcount_opt.rs
61 lines (52 loc) · 1.74 KB
/
wordcount_opt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
use std::time::Instant;
use renoir::prelude::*;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main() {
env_logger::init();
let (config, args) = RuntimeConfig::from_args();
if args.len() != 2 {
panic!("Pass the dataset path as an argument");
}
let path = &args[1];
config.spawn_remote_workers();
let env = StreamContext::new(config);
let result = env
.stream_file(path)
.batch_mode(BatchMode::fixed(1024))
.fold_assoc(
HashMap::<String, u64, BuildHasherDefault<wyhash::WyHash>>::default(),
|acc, line| {
let mut word = String::with_capacity(8);
for c in line.chars() {
if c.is_ascii_alphabetic() {
word.push(c.to_ascii_lowercase());
} else if !word.is_empty() {
let key = std::mem::replace(&mut word, String::with_capacity(8));
*acc.entry(key).or_default() += 1;
}
}
},
|a, mut b| {
for (k, v) in b.drain() {
*a.entry(k).or_default() += v;
}
},
)
.collect_vec();
let start = Instant::now();
env.execute_blocking();
let elapsed = start.elapsed();
if let Some(_res) = result.get() {
eprintln!("Output: {:?}", _res[0].len());
println!("{elapsed:?}");
// use itertools::Itertools;
// _res.iter()
// .sorted_by_key(|t| t.1)
// .rev()
// .take(10)
// .for_each(|(k, v)| eprintln!("{:>10}:{:>10}", k, v));
}
}