Skip to content

Commit d0d5308

Browse files
committed
refactor(proc): more -> iter/map
Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
1 parent 228918c commit d0d5308

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

processor/benches/proc_bench.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ fn proc_benchmark(c: &mut Criterion) {
1414
processor.sort_references(refs);
1515
})
1616
});
17+
c.bench_function("grouping references", |b| {
18+
b.iter(|| {
19+
processor.group_references(processor.get_references());
20+
})
21+
});
1722
c.bench_function("rendering references", |b| {
1823
b.iter(|| {
1924
processor.render_references();

processor/src/lib.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::collections::HashMap;
99
use std::fmt::Debug;
1010
use std::fs;
1111
use std::option::Option;
12-
use std::sync::{Arc, Mutex};
1312
use style::options::{SortOrder, StyleSortGroupKey, StyleSorting};
1413
#[allow(unused_imports)] // for now
1514
use style::template::{
@@ -387,24 +386,22 @@ impl Processor {
387386
let refs = self.get_references();
388387
let sorted_refs = self.sort_references(refs);
389388
let grouped_refs = self.group_references(sorted_refs);
390-
// REVIEW would prefer to avoid using mutable varibles here
391-
let proc_hints = Arc::new(Mutex::new(HashMap::new()));
392-
for (key, group) in grouped_refs {
393-
let group_len = group.len();
394-
// Run the processing in parallel.
395-
group.into_par_iter().enumerate().for_each(|(index, reference)| {
396-
let proc_hint = ProcHints {
397-
disamb_condition: false,
398-
group_index: index + 1,
399-
group_length: group_len,
400-
group_key: key.clone(),
401-
};
402-
let id = reference.id.as_ref().unwrap().clone();
403-
let mut proc_hints = proc_hints.lock().unwrap();
404-
proc_hints.insert(id, proc_hint);
405-
});
406-
}
407-
Arc::try_unwrap(proc_hints).unwrap().into_inner().unwrap()
389+
let proc_hints = grouped_refs
390+
.iter()
391+
.flat_map(|(key, group)| {
392+
let group_len = group.len();
393+
group.iter().enumerate().map(move |(index, reference)| {
394+
let proc_hint = ProcHints {
395+
disamb_condition: false,
396+
group_index: index + 1,
397+
group_length: group_len,
398+
group_key: key.clone(),
399+
};
400+
(reference.id.as_ref().unwrap().clone(), proc_hint)
401+
})
402+
})
403+
.collect();
404+
proc_hints
408405
}
409406

410407
/// Return a string to use for grouping for a given reference, using instructions in the style.

0 commit comments

Comments
 (0)