Skip to content

Commit 7f7d4c3

Browse files
committed
Get rid of redundant HashSet
1 parent 748e71e commit 7f7d4c3

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

src/librustc/mir/interpret/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub fn specialized_encode_alloc_id<
178178
AllocKind::Fn.encode(encoder)?;
179179
fn_instance.encode(encoder)?;
180180
} else if let Some(did) = tcx.interpret_interner.get_static(alloc_id) {
181-
// referring to statics doesn't need to know about their allocations, just hash the DefId
181+
// referring to statics doesn't need to know about their allocations, just about its DefId
182182
AllocKind::Static.encode(encoder)?;
183183
did.encode(encoder)?;
184184
} else {

src/librustc/ty/maps/on_disk_cache.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ impl<'sess> OnDiskCache<'sess> {
201201
predicate_shorthands: FxHashMap(),
202202
expn_info_shorthands: FxHashMap(),
203203
interpret_allocs: FxHashMap(),
204-
interpret_alloc_ids: FxHashSet(),
205204
interpret_allocs_inverse: Vec::new(),
206205
codemap: CachingCodemapView::new(tcx.sess.codemap()),
207206
file_to_file_index,
@@ -284,7 +283,12 @@ impl<'sess> OnDiskCache<'sess> {
284283
let mut interpret_alloc_index = Vec::new();
285284
let mut n = 0;
286285
loop {
287-
let new_n = encoder.interpret_alloc_ids.len();
286+
let new_n = encoder.interpret_allocs_inverse.len();
287+
// if we have found new ids, serialize those, too
288+
if n == new_n {
289+
// otherwise, abort
290+
break;
291+
}
288292
for idx in n..new_n {
289293
let id = encoder.interpret_allocs_inverse[idx];
290294
let pos = AbsoluteBytePos::new(encoder.position());
@@ -295,11 +299,6 @@ impl<'sess> OnDiskCache<'sess> {
295299
id,
296300
)?;
297301
}
298-
// if we have found new ids, serialize those, too
299-
if n == new_n {
300-
// otherwise, abort
301-
break;
302-
}
303302
n = new_n;
304303
}
305304
interpret_alloc_index
@@ -802,7 +801,6 @@ struct CacheEncoder<'enc, 'a, 'tcx, E>
802801
expn_info_shorthands: FxHashMap<Mark, AbsoluteBytePos>,
803802
interpret_allocs: FxHashMap<interpret::AllocId, usize>,
804803
interpret_allocs_inverse: Vec<interpret::AllocId>,
805-
interpret_alloc_ids: FxHashSet<interpret::AllocId>,
806804
codemap: CachingCodemapView<'tcx>,
807805
file_to_file_index: FxHashMap<*const FileMap, FileMapIndex>,
808806
}
@@ -839,14 +837,15 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<interpret::AllocId> for CacheEncoder<
839837
where E: 'enc + ty_codec::TyEncoder
840838
{
841839
fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
842-
let index = if self.interpret_alloc_ids.insert(*alloc_id) {
843-
let idx = self.interpret_alloc_ids.len() - 1;
844-
assert_eq!(idx, self.interpret_allocs_inverse.len());
845-
self.interpret_allocs_inverse.push(*alloc_id);
846-
assert!(self.interpret_allocs.insert(*alloc_id, idx).is_none());
847-
idx
848-
} else {
849-
self.interpret_allocs[alloc_id]
840+
use std::collections::hash_map::Entry;
841+
let index = match self.interpret_allocs.entry(*alloc_id) {
842+
Entry::Occupied(e) => *e.get(),
843+
Entry::Vacant(e) => {
844+
let idx = self.interpret_allocs_inverse.len();
845+
self.interpret_allocs_inverse.push(*alloc_id);
846+
e.insert(idx);
847+
idx
848+
},
850849
};
851850

852851
index.encode(self)

src/librustc_metadata/encoder.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc::ty::{self, Ty, TyCtxt, ReprOptions, SymbolName};
2929
use rustc::ty::codec::{self as ty_codec, TyEncoder};
3030

3131
use rustc::session::config::{self, CrateTypeProcMacro};
32-
use rustc::util::nodemap::{FxHashMap, FxHashSet};
32+
use rustc::util::nodemap::FxHashMap;
3333

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

6363
interpret_allocs: FxHashMap<interpret::AllocId, usize>,
6464
interpret_allocs_inverse: Vec<interpret::AllocId>,
65-
interpret_alloc_ids: FxHashSet<interpret::AllocId>,
6665

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

200199
impl<'a, 'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'a, 'tcx> {
201200
fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
202-
let index = if self.interpret_alloc_ids.insert(*alloc_id) {
203-
let idx = self.interpret_alloc_ids.len() - 1;
204-
assert_eq!(idx, self.interpret_allocs_inverse.len());
205-
self.interpret_allocs_inverse.push(*alloc_id);
206-
assert!(self.interpret_allocs.insert(*alloc_id, idx).is_none());
207-
idx
208-
} else {
209-
self.interpret_allocs[alloc_id]
201+
use std::collections::hash_map::Entry;
202+
let index = match self.interpret_allocs.entry(*alloc_id) {
203+
Entry::Occupied(e) => *e.get(),
204+
Entry::Vacant(e) => {
205+
let idx = self.interpret_allocs_inverse.len();
206+
self.interpret_allocs_inverse.push(*alloc_id);
207+
e.insert(idx);
208+
idx
209+
},
210210
};
211211

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

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

17981797
// Encode the rustc version string in a predictable location.

0 commit comments

Comments
 (0)