Skip to content

Commit

Permalink
feat: parallelize VisibilityTracker::from_crate (#424) (#426)
Browse files Browse the repository at this point in the history
Small perf gains as there isn't much we can parallelize without more
code massaging:

```
(92a9868) ❯ cargo criterion --bench indexed_crate --features rayon
IndexedCrate/new(aws-sdk-ec2)
                        time:   [574.13 ms 575.49 ms 576.92 ms]

(changes) ❯ cargo criterion --bench indexed_crate --features rayon
IndexedCrate/new(aws-sdk-ec2)
                        time:   [557.73 ms 559.38 ms 560.96 ms]
                        change: [-3.1709% -2.8003% -2.4380%] (p = 0.00 < 0.05)
                        Performance has improved.
```

Co-authored-by: Jalil David Salamé Messina <60845989+jalil-salame@users.noreply.github.com>
  • Loading branch information
obi1kenobi and jalil-salame committed Aug 29, 2024
1 parent 9514859 commit 431adb9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/visibility_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::collections::{HashMap, HashSet};

use rustdoc_types::{Crate, GenericArgs, Id, Item, ItemEnum, TypeAlias, Visibility};

#[cfg(feature = "rayon")]
use rayon::prelude::*;

use crate::{attributes::Attribute, ImportablePath};

#[derive(Debug, Clone)]
Expand All @@ -17,9 +20,14 @@ impl<'a> VisibilityTracker<'a> {
pub(crate) fn from_crate(crate_: &'a Crate) -> Self {
let mut visible_parent_ids = compute_parent_ids_for_public_items(crate_);

#[cfg(feature = "rayon")]
let iter = visible_parent_ids.par_iter_mut();
#[cfg(not(feature = "rayon"))]
let iter = visible_parent_ids.iter_mut();

// Sort and deduplicate parent ids.
// This ensures a consistent order, since queries can observe this order directly.
visible_parent_ids.iter_mut().for_each(|(_id, parent_ids)| {
iter.for_each(|(_id, parent_ids)| {
parent_ids.sort_unstable_by_key(|id| id.0.as_str());
parent_ids.dedup_by_key(|id| id.0.as_str());
});
Expand Down

0 comments on commit 431adb9

Please sign in to comment.