From 18ff915c8a214b84a97e59d28cb647b83aaec8e6 Mon Sep 17 00:00:00 2001 From: Arlo Siemsen Date: Wed, 15 Jul 2020 10:43:34 -0700 Subject: [PATCH] Make `cargo metadata` output deterministic Uses BTreeMap instead of HashMap for the `cargo metadata` command. The change did not cause a measurable performance impact for running `cargo metadata` on `cargo` itself. Fixes #8477 --- src/cargo/ops/cargo_output_metadata.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/cargo/ops/cargo_output_metadata.rs b/src/cargo/ops/cargo_output_metadata.rs index 56def031247..51fe4e725e9 100644 --- a/src/cargo/ops/cargo_output_metadata.rs +++ b/src/cargo/ops/cargo_output_metadata.rs @@ -6,7 +6,7 @@ use crate::ops::{self, Packages}; use crate::util::CargoResult; use cargo_platform::Platform; use serde::Serialize; -use std::collections::HashMap; +use std::collections::BTreeMap; use std::path::PathBuf; const VERSION: u32 = 1; @@ -130,7 +130,7 @@ fn build_resolve_graph( // Download all Packages. This is needed to serialize the information // for every package. In theory this could honor target filtering, // but that would be somewhat complex. - let mut package_map: HashMap = ws_resolve + let package_map: BTreeMap = ws_resolve .pkg_set .get_many(ws_resolve.pkg_set.package_ids())? .into_iter() @@ -140,7 +140,7 @@ fn build_resolve_graph( // Start from the workspace roots, and recurse through filling out the // map, filtering targets as necessary. - let mut node_map = HashMap::new(); + let mut node_map = BTreeMap::new(); for member_pkg in ws.members() { build_resolve_graph_r( &mut node_map, @@ -153,21 +153,22 @@ fn build_resolve_graph( } // Get a Vec of Packages. let actual_packages = package_map - .drain() + .into_iter() .filter_map(|(pkg_id, pkg)| node_map.get(&pkg_id).map(|_| pkg)) .collect(); + let mr = MetadataResolve { - nodes: node_map.drain().map(|(_pkg_id, node)| node).collect(), + nodes: node_map.into_iter().map(|(_pkg_id, node)| node).collect(), root: ws.current_opt().map(|pkg| pkg.package_id()), }; Ok((actual_packages, mr)) } fn build_resolve_graph_r( - node_map: &mut HashMap, + node_map: &mut BTreeMap, pkg_id: PackageId, resolve: &Resolve, - package_map: &HashMap, + package_map: &BTreeMap, target_data: &RustcTargetData, requested_kinds: &[CompileKind], ) {