From 2efc64c64900c465f397109fff75b318cd77d173 Mon Sep 17 00:00:00 2001 From: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> Date: Thu, 16 Jan 2025 13:50:38 -0500 Subject: [PATCH] Use rustc-hash throughout when enabled. (#713) --- .github/workflows/ci.yml | 6 ++++ src/adapter/optimizations/impl_lookup.rs | 4 +++ src/adapter/optimizations/method_lookup.rs | 8 ++++- src/indexed_crate.rs | 42 ++++++++++++---------- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50c0c27..a40fc6f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -94,6 +94,12 @@ jobs: - name: test with rayon run: cargo test --features rayon + - name: compile with rayon and rustc-hash + run: cargo test --no-run --features rayon,rustc-hash + + - name: test with rayon and rustc-hash + run: cargo test --features rayon,rustc-hash + publish: name: Publish to crates.io runs-on: ubuntu-latest diff --git a/src/adapter/optimizations/impl_lookup.rs b/src/adapter/optimizations/impl_lookup.rs index 3e4156b..3718236 100644 --- a/src/adapter/optimizations/impl_lookup.rs +++ b/src/adapter/optimizations/impl_lookup.rs @@ -1,5 +1,9 @@ +#[cfg(not(feature = "rustc-hash"))] use std::collections::HashMap; +#[cfg(feature = "rustc-hash")] +use rustc_hash::FxHashMap as HashMap; + use rustdoc_types::{Id, Item}; use trustfall::{ provider::{ diff --git a/src/adapter/optimizations/method_lookup.rs b/src/adapter/optimizations/method_lookup.rs index 3b249d4..44cf72f 100644 --- a/src/adapter/optimizations/method_lookup.rs +++ b/src/adapter/optimizations/method_lookup.rs @@ -1,5 +1,11 @@ use std::collections::{BTreeSet, HashMap}; +#[cfg(not(feature = "rustc-hash"))] +use std::collections::HashMap as OurHashMap; + +#[cfg(feature = "rustc-hash")] +use rustc_hash::FxHashMap as OurHashMap; + use rustdoc_types::{Id, Impl, Item, ItemEnum, Type}; use trustfall::{ provider::{ @@ -149,7 +155,7 @@ fn resolve_method_from_candidate_value<'a>( fn resolve_impl_method_by_name<'a>( origin: Origin, - impl_index: &'a HashMap, Vec<(&'a Item, &'a Item)>>, + impl_index: &'a OurHashMap, Vec<(&'a Item, &'a Item)>>, impl_owner_id: &'a Id, impl_id: &'a Id, method_name: &str, diff --git a/src/indexed_crate.rs b/src/indexed_crate.rs index 83ae38b..a9a7fcf 100644 --- a/src/indexed_crate.rs +++ b/src/indexed_crate.rs @@ -1,9 +1,15 @@ use std::{ borrow::Borrow, - collections::{hash_map::Entry, HashMap, HashSet}, + collections::{hash_map::Entry, HashMap}, sync::Arc, }; +#[cfg(not(feature = "rustc-hash"))] +use std::collections::{HashMap as OurHashMap, HashSet as OurHashSet}; + +#[cfg(feature = "rustc-hash")] +use rustc_hash::{FxHashMap as OurHashMap, FxHashSet as OurHashSet}; + #[cfg(feature = "rayon")] use rayon::prelude::*; use rustdoc_types::{Crate, Id, Item}; @@ -29,7 +35,7 @@ impl Borrow> for DependencyKey { pub(crate) struct PackageData { pub(crate) package: cargo_metadata::Package, - features: HashMap>, + features: OurHashMap>, // (dependency, target selector), parallel to `package.dependencies` dependency_info: Vec<(cargo_toml::Dependency, Option)>, @@ -77,7 +83,7 @@ impl From for PackageData { pub struct PackageStorage { pub(crate) own_crate: Crate, pub(crate) package_data: Option, - pub(crate) dependencies: HashMap, + pub(crate) dependencies: OurHashMap, } impl PackageStorage { @@ -108,7 +114,7 @@ pub struct PackageIndex<'a> { pub(crate) own_crate: IndexedCrate<'a>, pub(crate) features: Option>, #[allow(dead_code)] - pub(crate) dependencies: HashMap>, + pub(crate) dependencies: OurHashMap>, } impl<'a> PackageIndex<'a> { @@ -185,18 +191,18 @@ pub struct IndexedCrate<'a> { pub(crate) visibility_tracker: VisibilityTracker<'a>, /// index: importable name (in any namespace) -> list of items under that name - pub(crate) imports_index: Option, Vec<(&'a Item, Modifiers)>>>, + pub(crate) imports_index: Option, Vec<(&'a Item, Modifiers)>>>, /// index: impl owner + impl'd item name -> list of (impl itself, the named item)) - pub(crate) impl_index: Option, Vec<(&'a Item, &'a Item)>>>, + pub(crate) impl_index: Option, Vec<(&'a Item, &'a Item)>>>, /// index: method ("owned function") `Id` -> the struct/enum/union/trait that defines it; /// functions at top level will not have an index entry here - pub(crate) fn_owner_index: Option>, + pub(crate) fn_owner_index: Option>, /// index: function export name (`#[no_mangle]` or `#[export_name = "..."]` attribute) /// -> function item with that export name; exported symbol names must be unique. - pub(crate) export_name_index: Option>, + pub(crate) export_name_index: Option>, /// Trait items defined in external crates are not present in the `inner: &Crate` field, /// even if they are implemented by a type in that crate. This also includes @@ -211,14 +217,14 @@ pub struct IndexedCrate<'a> { /// /// A more complete future solution may generate multiple crates' rustdoc JSON /// and link to the external crate's trait items as necessary. - pub(crate) manually_inlined_builtin_traits: HashMap, + pub(crate) manually_inlined_builtin_traits: OurHashMap, } /// Map a Key to a List (Vec) of values /// /// It also has some nice operations for pushing a value to the list, or extending the list with /// many values. -struct MapList(HashMap>); +struct MapList(OurHashMap>); #[cfg(feature = "rayon")] impl FromParallelIterator<(K, V)> @@ -270,11 +276,11 @@ impl Extend<(K, V)> for MapList { impl MapList { #[inline] pub fn new() -> Self { - Self(HashMap::new()) + Self(OurHashMap::default()) } #[inline] - pub fn into_inner(self) -> HashMap> { + pub fn into_inner(self) -> OurHashMap> { self.0 } @@ -339,7 +345,7 @@ fn build_impl_index(index: &HashMap) -> MapList, (&Item, rustdoc_types::ItemEnum::Impl(impl_inner) => impl_inner, _ => unreachable!("expected impl but got another item type: {impl_item:?}"), }; - let trait_provided_methods: HashSet<_> = impl_inner + let trait_provided_methods: OurHashSet<_> = impl_inner .provided_trait_methods .iter() .map(|x| x.as_str()) @@ -403,7 +409,7 @@ fn build_impl_index(index: &HashMap) -> MapList, (&Item, impl<'a> IndexedCrate<'a> { pub fn new(crate_: &'a Crate) -> Self { - let mut value = Self { + let mut value: IndexedCrate<'a> = Self { inner: crate_, visibility_tracker: VisibilityTracker::from_crate(crate_), manually_inlined_builtin_traits: create_manually_inlined_builtin_traits(crate_), @@ -505,7 +511,7 @@ impl<'a> IndexedCrate<'a> { } } -fn build_fn_owner_index(index: &HashMap) -> HashMap { +fn build_fn_owner_index(index: &HashMap) -> OurHashMap { #[cfg(feature = "rayon")] let iter = index.par_iter().map(|(_, value)| value); #[cfg(not(feature = "rayon"))] @@ -579,7 +585,7 @@ fn build_fn_owner_index(index: &HashMap) -> HashMap { .collect() } -fn build_export_name_index(index: &HashMap) -> HashMap<&str, &Item> { +fn build_export_name_index(index: &HashMap) -> OurHashMap<&str, &Item> { #[cfg(feature = "rayon")] let iter = index.par_iter().map(|(_, value)| value); #[cfg(not(feature = "rayon"))] @@ -786,7 +792,7 @@ fn new_trait(manual_trait_item: &ManualTraitItem, id: Id, crate_id: u32) -> Item span: None, visibility: rustdoc_types::Visibility::Public, docs: None, - links: HashMap::new(), + links: HashMap::default(), attrs: Vec::new(), deprecation: None, inner: rustdoc_types::ItemEnum::Trait(rustdoc_types::Trait { @@ -818,7 +824,7 @@ fn new_trait(manual_trait_item: &ManualTraitItem, id: Id, crate_id: u32) -> Item } } -fn create_manually_inlined_builtin_traits(crate_: &Crate) -> HashMap { +fn create_manually_inlined_builtin_traits(crate_: &Crate) -> OurHashMap { let paths = &crate_.paths; // `paths` may have thousands of items.