Skip to content

Commit

Permalink
refactor(proc): more -> iter/map
Browse files Browse the repository at this point in the history
Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
  • Loading branch information
bdarcus committed Jun 4, 2023
1 parent 228918c commit d0d5308
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
5 changes: 5 additions & 0 deletions processor/benches/proc_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ fn proc_benchmark(c: &mut Criterion) {
processor.sort_references(refs);
})
});
c.bench_function("grouping references", |b| {
b.iter(|| {
processor.group_references(processor.get_references());
})
});
c.bench_function("rendering references", |b| {
b.iter(|| {
processor.render_references();
Expand Down
35 changes: 16 additions & 19 deletions processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::collections::HashMap;
use std::fmt::Debug;
use std::fs;
use std::option::Option;
use std::sync::{Arc, Mutex};
use style::options::{SortOrder, StyleSortGroupKey, StyleSorting};
#[allow(unused_imports)] // for now
use style::template::{
Expand Down Expand Up @@ -387,24 +386,22 @@ impl Processor {
let refs = self.get_references();
let sorted_refs = self.sort_references(refs);
let grouped_refs = self.group_references(sorted_refs);
// REVIEW would prefer to avoid using mutable varibles here
let proc_hints = Arc::new(Mutex::new(HashMap::new()));
for (key, group) in grouped_refs {
let group_len = group.len();
// Run the processing in parallel.
group.into_par_iter().enumerate().for_each(|(index, reference)| {
let proc_hint = ProcHints {
disamb_condition: false,
group_index: index + 1,
group_length: group_len,
group_key: key.clone(),
};
let id = reference.id.as_ref().unwrap().clone();
let mut proc_hints = proc_hints.lock().unwrap();
proc_hints.insert(id, proc_hint);
});
}
Arc::try_unwrap(proc_hints).unwrap().into_inner().unwrap()
let proc_hints = grouped_refs
.iter()
.flat_map(|(key, group)| {
let group_len = group.len();
group.iter().enumerate().map(move |(index, reference)| {
let proc_hint = ProcHints {
disamb_condition: false,
group_index: index + 1,
group_length: group_len,
group_key: key.clone(),
};
(reference.id.as_ref().unwrap().clone(), proc_hint)
})
})
.collect();
proc_hints
}

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

0 comments on commit d0d5308

Please sign in to comment.