Skip to content

Commit

Permalink
Get rid of redundant HashSet
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Apr 14, 2018
1 parent 748e71e commit 7f7d4c3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/librustc/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub fn specialized_encode_alloc_id<
AllocKind::Fn.encode(encoder)?;
fn_instance.encode(encoder)?;
} else if let Some(did) = tcx.interpret_interner.get_static(alloc_id) {
// referring to statics doesn't need to know about their allocations, just hash the DefId
// referring to statics doesn't need to know about their allocations, just about its DefId
AllocKind::Static.encode(encoder)?;
did.encode(encoder)?;
} else {
Expand Down
31 changes: 15 additions & 16 deletions src/librustc/ty/maps/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ impl<'sess> OnDiskCache<'sess> {
predicate_shorthands: FxHashMap(),
expn_info_shorthands: FxHashMap(),
interpret_allocs: FxHashMap(),
interpret_alloc_ids: FxHashSet(),
interpret_allocs_inverse: Vec::new(),
codemap: CachingCodemapView::new(tcx.sess.codemap()),
file_to_file_index,
Expand Down Expand Up @@ -284,7 +283,12 @@ impl<'sess> OnDiskCache<'sess> {
let mut interpret_alloc_index = Vec::new();
let mut n = 0;
loop {
let new_n = encoder.interpret_alloc_ids.len();
let new_n = encoder.interpret_allocs_inverse.len();
// if we have found new ids, serialize those, too
if n == new_n {
// otherwise, abort
break;
}
for idx in n..new_n {
let id = encoder.interpret_allocs_inverse[idx];
let pos = AbsoluteBytePos::new(encoder.position());
Expand All @@ -295,11 +299,6 @@ impl<'sess> OnDiskCache<'sess> {
id,
)?;
}
// if we have found new ids, serialize those, too
if n == new_n {
// otherwise, abort
break;
}
n = new_n;
}
interpret_alloc_index
Expand Down Expand Up @@ -802,7 +801,6 @@ struct CacheEncoder<'enc, 'a, 'tcx, E>
expn_info_shorthands: FxHashMap<Mark, AbsoluteBytePos>,
interpret_allocs: FxHashMap<interpret::AllocId, usize>,
interpret_allocs_inverse: Vec<interpret::AllocId>,
interpret_alloc_ids: FxHashSet<interpret::AllocId>,
codemap: CachingCodemapView<'tcx>,
file_to_file_index: FxHashMap<*const FileMap, FileMapIndex>,
}
Expand Down Expand Up @@ -839,14 +837,15 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<interpret::AllocId> for CacheEncoder<
where E: 'enc + ty_codec::TyEncoder
{
fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
let index = if self.interpret_alloc_ids.insert(*alloc_id) {
let idx = self.interpret_alloc_ids.len() - 1;
assert_eq!(idx, self.interpret_allocs_inverse.len());
self.interpret_allocs_inverse.push(*alloc_id);
assert!(self.interpret_allocs.insert(*alloc_id, idx).is_none());
idx
} else {
self.interpret_allocs[alloc_id]
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
},
};

index.encode(self)
Expand Down
25 changes: 12 additions & 13 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc::ty::{self, Ty, TyCtxt, ReprOptions, SymbolName};
use rustc::ty::codec::{self as ty_codec, TyEncoder};

use rustc::session::config::{self, CrateTypeProcMacro};
use rustc::util::nodemap::{FxHashMap, FxHashSet};
use rustc::util::nodemap::FxHashMap;

use rustc_data_structures::stable_hasher::StableHasher;
use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
Expand Down Expand Up @@ -62,7 +62,6 @@ pub struct EncodeContext<'a, 'tcx: 'a> {

interpret_allocs: FxHashMap<interpret::AllocId, usize>,
interpret_allocs_inverse: Vec<interpret::AllocId>,
interpret_alloc_ids: FxHashSet<interpret::AllocId>,

// This is used to speed up Span encoding.
filemap_cache: Lrc<FileMap>,
Expand Down Expand Up @@ -199,14 +198,15 @@ impl<'a, 'tcx> SpecializedEncoder<Ty<'tcx>> for EncodeContext<'a, 'tcx> {

impl<'a, 'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'a, 'tcx> {
fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
let index = if self.interpret_alloc_ids.insert(*alloc_id) {
let idx = self.interpret_alloc_ids.len() - 1;
assert_eq!(idx, self.interpret_allocs_inverse.len());
self.interpret_allocs_inverse.push(*alloc_id);
assert!(self.interpret_allocs.insert(*alloc_id, idx).is_none());
idx
} else {
self.interpret_allocs[alloc_id]
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
},
};

index.encode(self)
Expand Down Expand Up @@ -456,7 +456,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let mut n = 0;
trace!("beginning to encode alloc ids");
loop {
let new_n = self.interpret_alloc_ids.len();
let new_n = self.interpret_allocs_inverse.len();
// if we have found new ids, serialize those, too
if n == new_n {
// otherwise, abort
Expand Down Expand Up @@ -487,7 +487,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateTypeProcMacro);
let has_default_lib_allocator =
attr::contains_name(tcx.hir.krate_attrs(), "default_lib_allocator");
let has_global_allocator = tcx.sess.has_global_allocator.get();
let has_global_allocator = *tcx.sess.has_global_allocator.get();

let root = self.lazy(&CrateRoot {
name: tcx.crate_name(LOCAL_CRATE),
Expand Down Expand Up @@ -1792,7 +1792,6 @@ pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
filemap_cache: tcx.sess.codemap().files()[0].clone(),
interpret_allocs: Default::default(),
interpret_allocs_inverse: Default::default(),
interpret_alloc_ids: Default::default(),
};

// Encode the rustc version string in a predictable location.
Expand Down

0 comments on commit 7f7d4c3

Please sign in to comment.