Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade indexmap and use it more #75278

Merged
merged 12 commits into from
Aug 9, 2020
Merged
8 changes: 6 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1382,9 +1382,13 @@ dependencies = [

[[package]]
name = "indexmap"
version = "1.0.2"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d"
checksum = "86b45e59b16c76b11bf9738fd5d38879d3bd28ad292d7b313608becb17ae2df9"
dependencies = [
"autocfg",
"hashbrown",
]

[[package]]
name = "installer"
Expand Down
19 changes: 5 additions & 14 deletions src/librustc_codegen_llvm/coverageinfo/mapgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use llvm::coverageinfo::CounterMappingRegion;
use log::debug;
use rustc_codegen_ssa::coverageinfo::map::{Counter, CounterExpression, Region};
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::FxIndexSet;
use rustc_llvm::RustString;

use std::ffi::CString;
Expand Down Expand Up @@ -76,13 +76,12 @@ pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
}

struct CoverageMapGenerator {
filenames: Vec<CString>,
filename_to_index: FxHashMap<CString, u32>,
filenames: FxIndexSet<CString>,
}

impl CoverageMapGenerator {
fn new() -> Self {
Self { filenames: Vec::new(), filename_to_index: FxHashMap::default() }
Self { filenames: FxIndexSet::default() }
}

/// Using the `expressions` and `counter_regions` collected for the current function, generate
Expand Down Expand Up @@ -122,16 +121,8 @@ impl CoverageMapGenerator {
let c_filename =
CString::new(file_name).expect("null error converting filename to C string");
debug!(" file_id: {} = '{:?}'", current_file_id, c_filename);
let filenames_index = match self.filename_to_index.get(&c_filename) {
Some(index) => *index,
None => {
let index = self.filenames.len() as u32;
self.filenames.push(c_filename.clone());
self.filename_to_index.insert(c_filename.clone(), index);
index
}
};
virtual_file_mapping.push(filenames_index);
let (filenames_index, _) = self.filenames.insert_full(c_filename);
virtual_file_mapping.push(filenames_index as u32);
}
mapping_regions.push(CounterMappingRegion::code_region(
counter,
Expand Down
7 changes: 5 additions & 2 deletions src/librustc_codegen_llvm/coverageinfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
}
}

pub(crate) fn write_filenames_section_to_buffer(filenames: &Vec<CString>, buffer: &RustString) {
let c_str_vec = filenames.iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
pub(crate) fn write_filenames_section_to_buffer<'a>(
filenames: impl IntoIterator<Item = &'a CString>,
buffer: &RustString,
) {
let c_str_vec = filenames.into_iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
cuviper marked this conversation as resolved.
Show resolved Hide resolved
unsafe {
llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
c_str_vec.as_ptr(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ doctest = false

[dependencies]
ena = "0.14"
indexmap = "1"
indexmap = "1.5.1"
log = { package = "tracing", version = "0.1" }
jobserver_crate = { version = "0.1.13", package = "jobserver" }
lazy_static = "1"
Expand Down
39 changes: 13 additions & 26 deletions src/librustc_data_structures/transitive_relation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::fx::FxHashMap;
use crate::fx::FxIndexSet;
use crate::stable_hasher::{HashStable, StableHasher};
use crate::sync::Lock;
use rustc_index::bit_set::BitMatrix;
Expand All @@ -13,10 +13,7 @@ mod tests;
#[derive(Clone, Debug)]
pub struct TransitiveRelation<T: Eq + Hash> {
// List of elements. This is used to map from a T to a usize.
elements: Vec<T>,

// Maps each element to an index.
map: FxHashMap<T, Index>,
elements: FxIndexSet<T>,

// List of base edges in the graph. Require to compute transitive
// closure.
Expand All @@ -39,7 +36,6 @@ impl<T: Eq + Hash> Default for TransitiveRelation<T> {
fn default() -> Self {
TransitiveRelation {
elements: Default::default(),
map: Default::default(),
edges: Default::default(),
closure: Default::default(),
}
Expand All @@ -65,20 +61,16 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
}

fn index(&self, a: &T) -> Option<Index> {
self.map.get(a).cloned()
self.elements.get_index_of(a).map(Index)
}

fn add_index(&mut self, a: T) -> Index {
let &mut TransitiveRelation { ref mut elements, ref mut closure, ref mut map, .. } = self;

*map.entry(a.clone()).or_insert_with(|| {
elements.push(a);

let (index, added) = self.elements.insert_full(a);
if added {
// if we changed the dimensions, clear the cache
*closure.get_mut() = None;

Index(elements.len() - 1)
})
*self.closure.get_mut() = None;
}
Index(index)
}

/// Applies the (partial) function to each edge and returns a new
Expand Down Expand Up @@ -430,14 +422,11 @@ where
{
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
d.read_struct("TransitiveRelation", 2, |d| {
let elements: Vec<T> = d.read_struct_field("elements", 0, |d| Decodable::decode(d))?;
let edges = d.read_struct_field("edges", 1, |d| Decodable::decode(d))?;
let map = elements
.iter()
.enumerate()
.map(|(index, elem)| (elem.clone(), Index(index)))
.collect();
Ok(TransitiveRelation { elements, edges, map, closure: Lock::new(None) })
Ok(TransitiveRelation {
elements: d.read_struct_field("elements", 0, |d| Decodable::decode(d))?,
edges: d.read_struct_field("edges", 1, |d| Decodable::decode(d))?,
closure: Lock::new(None),
})
})
}
}
Expand All @@ -452,8 +441,6 @@ where
let TransitiveRelation {
ref elements,
ref edges,
// "map" is just a copy of elements vec
map: _,
// "closure" is just a copy of the data above
closure: _,
} = *self;
Expand Down
22 changes: 5 additions & 17 deletions src/librustc_metadata/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::rmeta::*;
use log::{debug, trace};
use rustc_ast::ast;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::{join, Lrc};
use rustc_hir as hir;
Expand Down Expand Up @@ -48,8 +48,7 @@ struct EncodeContext<'a, 'tcx> {
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,

interpret_allocs: FxHashMap<interpret::AllocId, usize>,
interpret_allocs_inverse: Vec<interpret::AllocId>,
interpret_allocs: FxIndexSet<interpret::AllocId>,

// This is used to speed up Span encoding.
// The `usize` is an index into the `MonotonicVec`
Expand Down Expand Up @@ -327,17 +326,7 @@ impl<'a, 'b, 'tcx> SpecializedEncoder<ty::Predicate<'b>> for EncodeContext<'a, '

impl<'a, 'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'a, 'tcx> {
fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
use std::collections::hash_map::Entry;
let index = match self.interpret_allocs.entry(*alloc_id) {
Entry::Occupied(e) => *e.get(),
Entry::Vacant(e) => {
let idx = self.interpret_allocs_inverse.len();
self.interpret_allocs_inverse.push(*alloc_id);
e.insert(idx);
idx
}
};

let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
index.encode(self)
}
}
Expand Down Expand Up @@ -579,15 +568,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let mut n = 0;
trace!("beginning to encode alloc ids");
loop {
let new_n = self.interpret_allocs_inverse.len();
let new_n = self.interpret_allocs.len();
// if we have found new ids, serialize those, too
if n == new_n {
// otherwise, abort
break;
}
trace!("encoding {} further alloc ids", new_n - n);
for idx in n..new_n {
let id = self.interpret_allocs_inverse[idx];
let id = self.interpret_allocs[idx];
let pos = self.position() as u32;
interpret_alloc_index.push(pos);
interpret::specialized_encode_alloc_id(self, tcx, id).unwrap();
Expand Down Expand Up @@ -2015,7 +2004,6 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
predicate_shorthands: Default::default(),
source_file_cache: (source_map_files[0].clone(), 0),
interpret_allocs: Default::default(),
interpret_allocs_inverse: Default::default(),
required_source_files: Some(GrowableBitSet::with_capacity(source_map_files.len())),
is_proc_macro: tcx.sess.crate_types().contains(&CrateType::ProcMacro),
hygiene_ctxt: &hygiene_ctxt,
Expand Down
22 changes: 5 additions & 17 deletions src/librustc_middle/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ty::codec::{self as ty_codec, TyDecoder, TyEncoder};
use crate::ty::context::TyCtxt;
use crate::ty::{self, Ty};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::Diagnostic;
Expand Down Expand Up @@ -212,7 +212,6 @@ impl<'sess> OnDiskCache<'sess> {
type_shorthands: Default::default(),
predicate_shorthands: Default::default(),
interpret_allocs: Default::default(),
interpret_allocs_inverse: Vec::new(),
source_map: CachingSourceMapView::new(tcx.sess.source_map()),
file_to_file_index,
hygiene_context: &hygiene_encode_context,
Expand Down Expand Up @@ -267,15 +266,15 @@ impl<'sess> OnDiskCache<'sess> {
let mut interpret_alloc_index = Vec::new();
let mut n = 0;
loop {
let new_n = encoder.interpret_allocs_inverse.len();
let new_n = encoder.interpret_allocs.len();
// If we have found new IDs, serialize those too.
if n == new_n {
// Otherwise, abort.
break;
}
interpret_alloc_index.reserve(new_n - n);
for idx in n..new_n {
let id = encoder.interpret_allocs_inverse[idx];
let id = encoder.interpret_allocs[idx];
let pos = encoder.position() as u32;
interpret_alloc_index.push(pos);
interpret::specialized_encode_alloc_id(&mut encoder, tcx, id)?;
Expand Down Expand Up @@ -767,8 +766,7 @@ struct CacheEncoder<'a, 'tcx, E: ty_codec::TyEncoder> {
encoder: &'a mut E,
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
interpret_allocs: FxHashMap<interpret::AllocId, usize>,
interpret_allocs_inverse: Vec<interpret::AllocId>,
interpret_allocs: FxIndexSet<interpret::AllocId>,
source_map: CachingSourceMapView<'tcx>,
file_to_file_index: FxHashMap<*const SourceFile, SourceFileIndex>,
hygiene_context: &'a HygieneEncodeContext,
Expand Down Expand Up @@ -807,17 +805,7 @@ where
E: 'a + TyEncoder,
{
fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
use std::collections::hash_map::Entry;
let index = match self.interpret_allocs.entry(*alloc_id) {
Entry::Occupied(e) => *e.get(),
Entry::Vacant(e) => {
let idx = self.interpret_allocs_inverse.len();
self.interpret_allocs_inverse.push(*alloc_id);
e.insert(idx);
idx
}
};

let (index, _) = self.interpret_allocs.insert_full(*alloc_id);
index.encode(self)
}
}
Expand Down
Loading