From 570bbc55be03eefdd2bb1598bd2137df8f6952f4 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 5 May 2022 17:39:12 -0700 Subject: [PATCH] WIP: Try custom index types for indexmap Looking for real-world results on bluss/indexmap#147. --- Cargo.lock | 35 ++++++++++------ compiler/rustc_borrowck/src/borrow_set.rs | 15 ++++--- compiler/rustc_borrowck/src/dataflow.rs | 3 +- .../rustc_borrowck/src/region_infer/values.rs | 10 ++--- .../src/coverageinfo/mapgen.rs | 4 +- compiler/rustc_data_structures/Cargo.toml | 3 +- compiler/rustc_data_structures/src/fx.rs | 5 ++- .../src/stable_hasher.rs | 4 +- compiler/rustc_index/Cargo.toml | 1 + compiler/rustc_index/src/lib.rs | 4 ++ compiler/rustc_infer/src/traits/util.rs | 2 +- compiler/rustc_macros/src/newtype.rs | 40 +++++++++++++++++++ compiler/rustc_serialize/Cargo.toml | 3 +- .../rustc_serialize/src/collection_impls.rs | 14 ++++--- compiler/rustc_span/src/span_encoding.rs | 6 +-- compiler/rustc_ty_utils/src/ty.rs | 2 +- 16 files changed, 105 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 672f06a172b20..d8343e38b1d64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -261,7 +261,7 @@ version = "0.1.0" dependencies = [ "anyhow", "curl", - "indexmap", + "indexmap 1.8.0", "serde", "serde_json", "toml", @@ -337,7 +337,7 @@ dependencies = [ "humantime 2.0.1", "ignore", "im-rc", - "indexmap", + "indexmap 1.8.0", "itertools", "jobserver", "lazy_static", @@ -562,7 +562,7 @@ dependencies = [ "chalk-derive", "chalk-ir", "ena", - "indexmap", + "indexmap 1.8.0", "itertools", "petgraph", "rustc-hash", @@ -608,7 +608,7 @@ checksum = "6d76c22c9b9b215eeb8d016ad3a90417bd13cb24cf8142756e6472445876cab7" dependencies = [ "atty", "bitflags", - "indexmap", + "indexmap 1.8.0", "lazy_static", "os_str_bytes", "strsim 0.10.0", @@ -1574,7 +1574,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" dependencies = [ "fallible-iterator", - "indexmap", + "indexmap 1.8.0", "stable_deref_trait", ] @@ -1821,10 +1821,18 @@ checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" dependencies = [ "autocfg", "hashbrown 0.11.2", - "rustc-rayon", "serde", ] +[[package]] +name = "indexmap" +version = "2.0.0-pre" +source = "git+https://github.com/cuviper/indexmap?branch=indexable#3b6d807f460308e6de30cd34f9516aca624615f2" +dependencies = [ + "hashbrown 0.12.0", + "rustc-rayon", +] + [[package]] name = "indoc" version = "1.0.3" @@ -2515,7 +2523,7 @@ dependencies = [ "crc32fast", "flate2", "hashbrown 0.11.2", - "indexmap", + "indexmap 1.8.0", "memchr", ] @@ -2777,7 +2785,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "467d164a6de56270bd7c4d070df81d07beace25012d5103ced4e9ff08d6afdb7" dependencies = [ "fixedbitset", - "indexmap", + "indexmap 1.8.0", ] [[package]] @@ -3676,7 +3684,7 @@ dependencies = [ "bitflags", "cfg-if 0.1.10", "ena", - "indexmap", + "indexmap 2.0.0-pre", "jobserver", "libc", "measureme 10.0.0", @@ -3858,6 +3866,7 @@ name = "rustc_index" version = "0.0.0" dependencies = [ "arrayvec", + "indexmap 2.0.0-pre", "rustc_macros", "rustc_serialize", "smallvec", @@ -4320,7 +4329,7 @@ dependencies = [ name = "rustc_serialize" version = "0.0.0" dependencies = [ - "indexmap", + "indexmap 2.0.0-pre", "rustc_macros", "smallvec", ] @@ -4731,7 +4740,7 @@ version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" dependencies = [ - "indexmap", + "indexmap 1.8.0", "itoa", "ryu", "serde", @@ -5326,7 +5335,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "744e9ed5b352340aa47ce033716991b5589e23781acb97cad37d4ea70560f55b" dependencies = [ "combine", - "indexmap", + "indexmap 1.8.0", "itertools", "kstring 1.0.6", "serde", @@ -5339,7 +5348,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba98375fd631b83696f87c64e4ed8e29e6a1f3404d6aed95fa95163bad38e705" dependencies = [ "combine", - "indexmap", + "indexmap 1.8.0", "itertools", "kstring 2.0.0", "serde", diff --git a/compiler/rustc_borrowck/src/borrow_set.rs b/compiler/rustc_borrowck/src/borrow_set.rs index 4a9904891ecad..b4564e81dad5a 100644 --- a/compiler/rustc_borrowck/src/borrow_set.rs +++ b/compiler/rustc_borrowck/src/borrow_set.rs @@ -18,7 +18,7 @@ pub struct BorrowSet<'tcx> { /// by the `Location` of the assignment statement in which it /// appears on the right hand side. Thus the location is the map /// key, and its position in the map corresponds to `BorrowIndex`. - pub location_map: FxIndexMap>, + pub location_map: FxIndexMap, BorrowIndex>, /// Locations which activate borrows. /// NOTE: a given location may activate more than one borrow in the future @@ -36,7 +36,7 @@ impl<'tcx> Index for BorrowSet<'tcx> { type Output = BorrowData<'tcx>; fn index(&self, index: BorrowIndex) -> &BorrowData<'tcx> { - &self.location_map[index.as_usize()] + &self.location_map[index] } } @@ -165,7 +165,7 @@ impl<'tcx> BorrowSet<'tcx> { } crate fn get_index_of(&self, location: &Location) -> Option { - self.location_map.get_index_of(location).map(BorrowIndex::from) + self.location_map.get_index_of(location) } crate fn contains(&self, location: &Location) -> bool { @@ -176,7 +176,7 @@ impl<'tcx> BorrowSet<'tcx> { struct GatherBorrows<'a, 'tcx> { tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, - location_map: FxIndexMap>, + location_map: FxIndexMap, BorrowIndex>, activation_map: FxHashMap>, local_map: FxHashMap>, @@ -217,7 +217,6 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> { assigned_place: *assigned_place, }; let (idx, _) = self.location_map.insert_full(location, borrow); - let idx = BorrowIndex::from(idx); self.insert_as_pending_if_two_phase(location, assigned_place, kind, idx); @@ -237,7 +236,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> { // // TMP = &mut place if let Some(&borrow_index) = self.pending_activations.get(temp) { - let borrow_data = &mut self.location_map[borrow_index.as_usize()]; + let borrow_data = &mut self.location_map[borrow_index]; // Watch out: the use of TMP in the borrow itself // doesn't count as an activation. =) @@ -326,7 +325,7 @@ impl<'a, 'tcx> GatherBorrows<'a, 'tcx> { // Consider the borrow not activated to start. When we find an activation, we'll update // this field. { - let borrow_data = &mut self.location_map[borrow_index.as_usize()]; + let borrow_data = &mut self.location_map[borrow_index]; borrow_data.activation_location = TwoPhaseActivation::NotActivated; } @@ -342,7 +341,7 @@ impl<'a, 'tcx> GatherBorrows<'a, 'tcx> { at borrow_index: {:?} with associated data {:?}", temp, old_index, - self.location_map[old_index.as_usize()] + self.location_map[old_index] ); } } diff --git a/compiler/rustc_borrowck/src/dataflow.rs b/compiler/rustc_borrowck/src/dataflow.rs index d38e89cd79edd..f9d3c08a55bcf 100644 --- a/compiler/rustc_borrowck/src/dataflow.rs +++ b/compiler/rustc_borrowck/src/dataflow.rs @@ -107,7 +107,8 @@ impl_visitable! { rustc_index::newtype_index! { pub struct BorrowIndex { - DEBUG_FORMAT = "bw{}" + DEBUG_FORMAT = "bw{}", + INDEXMAP, } } diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 4a70535c63bea..f2bde135bb692 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -93,7 +93,7 @@ rustc_index::newtype_index! { rustc_index::newtype_index! { /// A single integer representing a `ty::Placeholder`. - pub struct PlaceholderIndex { DEBUG_FORMAT = "PlaceholderIndex({})" } + pub struct PlaceholderIndex { DEBUG_FORMAT = "PlaceholderIndex({})", INDEXMAP } } /// An individual element in a region value -- the value of a @@ -179,21 +179,21 @@ impl LivenessValues { /// NLL. #[derive(Default)] crate struct PlaceholderIndices { - indices: FxIndexSet, + indices: FxIndexSet, } impl PlaceholderIndices { crate fn insert(&mut self, placeholder: ty::PlaceholderRegion) -> PlaceholderIndex { let (index, _) = self.indices.insert_full(placeholder); - index.into() + index } crate fn lookup_index(&self, placeholder: ty::PlaceholderRegion) -> PlaceholderIndex { - self.indices.get_index_of(&placeholder).unwrap().into() + self.indices.get_index_of(&placeholder).unwrap() } crate fn lookup_placeholder(&self, placeholder: PlaceholderIndex) -> ty::PlaceholderRegion { - self.indices[placeholder.index()] + self.indices[placeholder] } crate fn len(&self) -> usize { diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs index 58f391692c49c..0446f8f55fcc7 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs @@ -121,7 +121,7 @@ pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) { } struct CoverageMapGenerator { - filenames: FxIndexSet, + filenames: FxIndexSet, } impl CoverageMapGenerator { @@ -185,7 +185,7 @@ impl CoverageMapGenerator { .expect("null error converting filename to C string"); debug!(" file_id: {} = '{:?}'", current_file_id, c_filename); let (filenames_index, _) = self.filenames.insert_full(c_filename); - virtual_file_mapping.push(filenames_index as u32); + virtual_file_mapping.push(filenames_index); } debug!("Adding counter {:?} to map for {:?}", counter, region); mapping_regions.push(CounterMappingRegion::code_region( diff --git a/compiler/rustc_data_structures/Cargo.toml b/compiler/rustc_data_structures/Cargo.toml index 7cc8b5c20339a..0a299461f2934 100644 --- a/compiler/rustc_data_structures/Cargo.toml +++ b/compiler/rustc_data_structures/Cargo.toml @@ -9,7 +9,8 @@ doctest = false [dependencies] arrayvec = { version = "0.7", default-features = false } ena = "0.14" -indexmap = { version = "1.8.0" } +# indexmap = { version = "1.8.0" } +indexmap = { git = "https://github.com/cuviper/indexmap", branch = "indexable" } tracing = "0.1" jobserver_crate = { version = "0.1.13", package = "jobserver" } rustc_serialize = { path = "../rustc_serialize" } diff --git a/compiler/rustc_data_structures/src/fx.rs b/compiler/rustc_data_structures/src/fx.rs index bbeb193dba32b..603f7be1a22ca 100644 --- a/compiler/rustc_data_structures/src/fx.rs +++ b/compiler/rustc_data_structures/src/fx.rs @@ -2,8 +2,9 @@ use std::hash::BuildHasherDefault; pub use rustc_hash::{FxHashMap, FxHashSet, FxHasher}; -pub type FxIndexMap = indexmap::IndexMap>; -pub type FxIndexSet = indexmap::IndexSet>; +pub type FxIndexMap = + indexmap::IndexMap, Idx>; +pub type FxIndexSet = indexmap::IndexSet, Idx>; #[macro_export] macro_rules! define_id_collections { diff --git a/compiler/rustc_data_structures/src/stable_hasher.rs b/compiler/rustc_data_structures/src/stable_hasher.rs index 25353290fd50c..52af67bcb5214 100644 --- a/compiler/rustc_data_structures/src/stable_hasher.rs +++ b/compiler/rustc_data_structures/src/stable_hasher.rs @@ -354,7 +354,7 @@ impl, CTX> HashStable for Vec { } } -impl HashStable for indexmap::IndexMap +impl HashStable for indexmap::IndexMap where K: HashStable + Eq + Hash, V: HashStable, @@ -369,7 +369,7 @@ where } } -impl HashStable for indexmap::IndexSet +impl HashStable for indexmap::IndexSet where K: HashStable + Eq + Hash, R: BuildHasher, diff --git a/compiler/rustc_index/Cargo.toml b/compiler/rustc_index/Cargo.toml index 89419bfce6f5b..5eb0bbd811aa3 100644 --- a/compiler/rustc_index/Cargo.toml +++ b/compiler/rustc_index/Cargo.toml @@ -8,6 +8,7 @@ doctest = false [dependencies] arrayvec = { version = "0.7", default-features = false } +indexmap = { git = "https://github.com/cuviper/indexmap", branch = "indexable" } rustc_serialize = { path = "../rustc_serialize" } rustc_macros = { path = "../rustc_macros" } smallvec = "1" diff --git a/compiler/rustc_index/src/lib.rs b/compiler/rustc_index/src/lib.rs index 33c3c536f119b..f0f1439eec0f4 100644 --- a/compiler/rustc_index/src/lib.rs +++ b/compiler/rustc_index/src/lib.rs @@ -21,3 +21,7 @@ macro_rules! static_assert_size { const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()]; }; } + +pub mod map { + pub use indexmap::{Indexable, IndexMap}; +} diff --git a/compiler/rustc_infer/src/traits/util.rs b/compiler/rustc_infer/src/traits/util.rs index 674c75fdee561..2ef2023dc9962 100644 --- a/compiler/rustc_infer/src/traits/util.rs +++ b/compiler/rustc_infer/src/traits/util.rs @@ -325,7 +325,7 @@ pub fn transitive_bounds_that_define_assoc_type<'tcx>( assoc_name: Ident, ) -> impl Iterator> { let mut stack: Vec<_> = bounds.collect(); - let mut visited = FxIndexSet::default(); + let mut visited = FxIndexSet::<_, usize>::default(); std::iter::from_fn(move || { while let Some(trait_ref) = stack.pop() { diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index c8b31cd0c4d78..9489d66d32390 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -11,6 +11,7 @@ mod kw { syn::custom_keyword!(ENCODABLE); syn::custom_keyword!(custom); syn::custom_keyword!(ORD_IMPL); + syn::custom_keyword!(INDEXMAP); } #[derive(Debug)] @@ -44,6 +45,7 @@ impl Parse for Newtype { let mut consts = Vec::new(); let mut encodable = true; let mut ord = true; + let mut indexmap = false; // Parse an optional trailing comma let try_comma = || -> Result<()> { @@ -105,9 +107,16 @@ impl Parse for Newtype { body.parse::()?; body.parse::()?; body.parse::()?; + try_comma()?; ord = false; continue; } + if body.lookahead1().peek(kw::INDEXMAP) { + body.parse::()?; + try_comma()?; + indexmap = true; + continue; + } // We've parsed everything that the user provided, so we're done if body.is_empty() { @@ -193,6 +202,36 @@ impl Parse for Newtype { } }; + let indexmap_impl = if indexmap { + quote! { + impl ::rustc_index::map::Indexable for #name {} + + impl ::std::ops::Index<#name> + for ::rustc_index::map::IndexMap + { + type Output = V; + + fn index(&self, index: #name) -> &V { + self.get_index(index) + .expect("IndexMap: index out of bounds") + .1 + } + } + + impl ::std::ops::IndexMut<#name> + for ::rustc_index::map::IndexMap + { + fn index_mut(&mut self, index: #name) -> &mut V { + self.get_index_mut(index) + .expect("IndexMap: index out of bounds") + .1 + } + } + } + } else { + quote! {} + }; + Ok(Self(quote! { #(#attrs)* #[derive(Clone, Copy, PartialEq, Eq, Hash, #(#derive_paths),*)] @@ -323,6 +362,7 @@ impl Parse for Newtype { #encodable_impls #debug_impl + #indexmap_impl })) } } diff --git a/compiler/rustc_serialize/Cargo.toml b/compiler/rustc_serialize/Cargo.toml index f6b9e17e58ed9..ecd642c43e401 100644 --- a/compiler/rustc_serialize/Cargo.toml +++ b/compiler/rustc_serialize/Cargo.toml @@ -4,7 +4,8 @@ version = "0.0.0" edition = "2021" [dependencies] -indexmap = "1.8.0" +#indexmap = "1.8.0" +indexmap = { git = "https://github.com/cuviper/indexmap", branch = "indexable" } smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } [dev-dependencies] diff --git a/compiler/rustc_serialize/src/collection_impls.rs b/compiler/rustc_serialize/src/collection_impls.rs index dee6dc010feef..a0977b92e1445 100644 --- a/compiler/rustc_serialize/src/collection_impls.rs +++ b/compiler/rustc_serialize/src/collection_impls.rs @@ -197,7 +197,7 @@ where } } -impl Encodable for indexmap::IndexMap +impl Encodable for indexmap::IndexMap where K: Encodable + Hash + Eq, V: Encodable, @@ -214,13 +214,14 @@ where } } -impl Decodable for indexmap::IndexMap +impl Decodable for indexmap::IndexMap where K: Decodable + Hash + Eq, V: Decodable, S: BuildHasher + Default, + Idx: indexmap::Indexable, { - fn decode(d: &mut D) -> indexmap::IndexMap { + fn decode(d: &mut D) -> Self { let len = d.read_usize(); let state = Default::default(); let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state); @@ -233,7 +234,7 @@ where } } -impl Encodable for indexmap::IndexSet +impl Encodable for indexmap::IndexSet where T: Encodable + Hash + Eq, S: BuildHasher, @@ -248,12 +249,13 @@ where } } -impl Decodable for indexmap::IndexSet +impl Decodable for indexmap::IndexSet where T: Decodable + Hash + Eq, S: BuildHasher + Default, + Idx: indexmap::Indexable, { - fn decode(d: &mut D) -> indexmap::IndexSet { + fn decode(d: &mut D) -> Self { let len = d.read_usize(); let state = Default::default(); let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state); diff --git a/compiler/rustc_span/src/span_encoding.rs b/compiler/rustc_span/src/span_encoding.rs index 3ee329e9736f2..91a46793a7d45 100644 --- a/compiler/rustc_span/src/span_encoding.rs +++ b/compiler/rustc_span/src/span_encoding.rs @@ -126,20 +126,20 @@ impl Span { // Interned format. debug_assert!(self.ctxt_or_zero == 0); let index = self.base_or_index; - with_span_interner(|interner| interner.spans[index as usize]) + with_span_interner(|interner| interner.spans[index]) } } } #[derive(Default)] pub struct SpanInterner { - spans: FxIndexSet, + spans: FxIndexSet, } impl SpanInterner { fn intern(&mut self, span_data: &SpanData) -> u32 { let (index, _) = self.spans.insert_full(*span_data); - index as u32 + index } } diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 6ad71bdb48169..4506b61223554 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -294,7 +294,7 @@ fn well_formed_types_in_env<'tcx>( }; // FIXME(eddyb) isn't the unordered nature of this a hazard? - let mut inputs = FxIndexSet::default(); + let mut inputs = FxIndexSet::<_, usize>::default(); match node_kind { // In a trait impl, we assume that the header trait ref and all its