From 64c4c44b3f3a22fe4edd14c58045676e0dd15ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= <60845989+jalil-salame@users.noreply.github.com> Date: Thu, 29 Aug 2024 05:25:55 +0200 Subject: [PATCH] feat: parallelize VisibilityTracker::from_crate (#424) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. ``` --- src/visibility_tracker.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/visibility_tracker.rs b/src/visibility_tracker.rs index 0125bc2..e307f5a 100644 --- a/src/visibility_tracker.rs +++ b/src/visibility_tracker.rs @@ -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)] @@ -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()); });