Parallelize groupby with Rayon in Rust

Viewed 62

I am trying to do something where I have a little logic in groupby. Consider this working code for example:

use itertools::Itertools;
let counts = vec![Some(1), Some(1), None, None, Some(1), Some(1), Some(3), Some(3)];
let vals = vec![1, 3, 2, 2, 1, 0, 1, 2];
let mut hm1: HashMap<u32,u32> = HashMap::new();
let groups = &(counts.into_iter().zip(vals.into_iter())).group_by(|(d1,_d2)| *d1);

groups.into_iter()
.map(|(key, group)| {
    group.into_iter()
    .map(|(count, val)| {
        hm1.entry(val).and_modify(|e| *e += 1).or_insert(1);
        }).collect::<Vec<_>>();
    }).collect::<Vec<_>>();    

Here, I am essentially creating a histogram in HashMap. Please ignore the fact that in this particular case I do not actually need to groupby to get my HashMap. It's illustrative.

Now, I am trying to understand how I can parallelize this type of code with Rayon. Upon searching, I found this thread that says groupby in itertools cannot be used with Rayon. Reading the Rayon documentation, I could not find any way to achieve this. Note that I don't particularly care about using itertools, so any other solution that parallelizes a groupby in rust would work.

0 Answers
Related