Skip to content

Commit

Permalink
[meta] switch to AHashMap
Browse files Browse the repository at this point in the history
Switch to ahash, which promises to be faster at lookups.

Overall, iteration through the hash map is a bit slower but
`make_package_graph` and other queries are faster.

```

make_package_graph      time:   [6.9865 ms 7.0088 ms 7.0313 ms]
                        change: [-4.0527% -3.5680% -3.1149%] (p = 0.00 < 0.05)
                        Performance has improved.

depends_on              time:   [110.99 µs 111.12 µs 111.27 µs]
                        change: [-12.629% -12.282% -11.952%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) low mild
  3 (3.00%) high mild

depends_on_cache        time:   [92.296 µs 92.522 µs 92.777 µs]
                        change: [-18.680% -18.438% -18.227%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  2 (2.00%) low mild
  3 (3.00%) high mild
  1 (1.00%) high severe

into_ids                time:   [164.61 µs 164.74 µs 164.87 µs]
                        change: [-9.3113% -8.9215% -8.5783%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) low mild
  1 (1.00%) high mild
  2 (2.00%) high severe

resolve_package_name    time:   [1.4308 µs 1.4327 µs 1.4351 µs]
                        change: [+12.664% +12.952% +13.227%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

make_package_name_hashmap
                        time:   [50.083 µs 50.112 µs 50.144 µs]
                        change: [-0.0379% +0.0879% +0.2012%] (p = 0.16 > 0.05)
                        No change in performance detected.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe

make_cycles             time:   [55.688 µs 55.770 µs 55.871 µs]
                        change: [+6.9990% +7.4064% +7.7971%] (p = 0.00 < 0.05)
                        Performance has regressed.
```
  • Loading branch information
sunshowers committed Nov 14, 2023
1 parent 1d1b9e3 commit cf590fe
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 125 deletions.
33 changes: 31 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
]

[workspace.dependencies]
ahash = "0.8.6"
guppy-workspace-hack = "0.1.0"

[patch.crates-io.guppy-workspace-hack]
Expand Down
1 change: 1 addition & 0 deletions cargo-guppy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license = "MIT OR Apache-2.0"
edition = "2018"

[dependencies]
ahash.workspace = true
camino = "1.1.6"
# disable tracing integration since we don't use it
color-eyre = { version = "0.6.2", default-features = false }
Expand Down
15 changes: 4 additions & 11 deletions cargo-guppy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod mv;

pub use crate::{core::*, mv::*};

use ahash::AHashMap;
use camino::Utf8PathBuf;
use clap::{ArgEnum, Parser};
use color_eyre::eyre::{bail, Result, WrapErr};
Expand All @@ -58,15 +59,7 @@ use guppy::{
use guppy_cmdlib::{
string_to_platform_spec, CargoMetadataOptions, CargoResolverOpts, PackagesAndFeatures,
};
use std::{
borrow::Cow,
cmp,
collections::{HashMap, HashSet},
fmt, fs,
io::Write,
iter,
path::PathBuf,
};
use std::{borrow::Cow, cmp, collections::HashSet, fmt, fs, io::Write, iter, path::PathBuf};

pub fn cmd_diff(json: bool, old: &str, new: &str) -> Result<()> {
let old_json = fs::read_to_string(old)?;
Expand Down Expand Up @@ -140,7 +133,7 @@ pub fn cmd_dups(opts: &DupsOptions) -> Result<()> {
let resolver = opts.filter_opts.make_resolver(&pkg_graph)?;
let selection = pkg_graph.query_workspace();

let mut dupe_map: HashMap<_, Vec<_>> = HashMap::new();
let mut dupe_map: AHashMap<_, Vec<_>> = AHashMap::new();
for package in selection
.resolve_with_fn(resolver)
.packages(DependencyDirection::Forward)
Expand Down Expand Up @@ -387,7 +380,7 @@ pub fn cmd_subtree_size(options: &SubtreeSizeOptions) -> Result<()> {
pkg_graph.query_workspace()
};

let mut unique_deps: HashMap<&PackageId, HashSet<&PackageId>> = HashMap::new();
let mut unique_deps: AHashMap<&PackageId, HashSet<&PackageId>> = AHashMap::new();
for package_id in selection
.resolve_with_fn(&resolver)
.package_ids(DependencyDirection::Forward)
Expand Down
5 changes: 5 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
disallowed-methods = [
# use ahash everywhere instead
"std::collections::hash::map::HashMap::new",
"std::collections::hash::map::HashMap::with_capacity",
]
1 change: 1 addition & 0 deletions fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ publish = false
edition = "2018"

[dependencies]
ahash.workspace = true
camino = "1.1.6"
guppy = { path = "../guppy" }
once_cell = "1.18.0"
Expand Down
21 changes: 11 additions & 10 deletions fixtures/src/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
},
package_id,
};
use ahash::AHashMap;
use camino::Utf8PathBuf;
use guppy::{
errors::FeatureGraphWarning,
Expand All @@ -19,24 +20,24 @@ use guppy::{
DependencyKind, PackageId, Version,
};
use pretty_assertions::assert_eq;
use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;

/// This captures metadata fields that are relevant for tests. They are meant to be written out
/// lazily as tests are filled out -- feel free to add more details as necessary!
pub struct FixtureDetails {
workspace_members: Option<BTreeMap<Utf8PathBuf, PackageId>>,
package_details: HashMap<PackageId, PackageDetails>,
link_details: HashMap<(PackageId, PackageId), LinkDetails>,
package_details: AHashMap<PackageId, PackageDetails>,
link_details: AHashMap<(PackageId, PackageId), LinkDetails>,
feature_graph_warnings: Vec<FeatureGraphWarning>,
cycles: Vec<Vec<PackageId>>,
}

impl FixtureDetails {
pub fn new(package_details: HashMap<PackageId, PackageDetails>) -> Self {
pub fn new(package_details: AHashMap<PackageId, PackageDetails>) -> Self {
Self {
workspace_members: None,
package_details,
link_details: HashMap::new(),
link_details: AHashMap::new(),
feature_graph_warnings: vec![],
cycles: vec![],
}
Expand All @@ -57,7 +58,7 @@ impl FixtureDetails {

pub fn with_link_details(
mut self,
link_details: HashMap<(PackageId, PackageId), LinkDetails>,
link_details: AHashMap<(PackageId, PackageId), LinkDetails>,
) -> Self {
self.link_details = link_details;
self
Expand Down Expand Up @@ -456,7 +457,7 @@ impl PackageDetails {
self
}

pub fn insert_into(self, map: &mut HashMap<PackageId, PackageDetails>) {
pub fn insert_into(self, map: &mut AHashMap<PackageId, PackageDetails>) {
map.insert(self.id.clone(), self);
}

Expand Down Expand Up @@ -543,7 +544,7 @@ impl LinkDetails {
self
}

pub fn insert_into(self, map: &mut HashMap<(PackageId, PackageId), Self>) {
pub fn insert_into(self, map: &mut AHashMap<(PackageId, PackageId), Self>) {
map.insert((self.from.clone(), self.to.clone()), self);
}

Expand Down Expand Up @@ -601,7 +602,7 @@ pub struct PlatformResults {
// Each pair stands for (required on, enabled on).
status: (EnabledTernary, EnabledTernary),
default_features: (EnabledTernary, EnabledTernary),
feature_statuses: HashMap<String, (EnabledTernary, EnabledTernary)>,
feature_statuses: AHashMap<String, (EnabledTernary, EnabledTernary)>,
}

impl PlatformResults {
Expand All @@ -612,7 +613,7 @@ impl PlatformResults {
Self {
status,
default_features,
feature_statuses: HashMap::new(),
feature_statuses: AHashMap::new(),
}
}

Expand Down
Loading

0 comments on commit cf590fe

Please sign in to comment.