Skip to content

Commit 3d1f645

Browse files
committed
Get rid of redundant HashSet
1 parent 84be1fd commit 3d1f645

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

src/librustc/mir/interpret/mod.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ impl<'sess> OnDiskCache<'sess> {
200200
predicate_shorthands: FxHashMap(),
201201
expn_info_shorthands: FxHashMap(),
202202
interpret_allocs: FxHashMap(),
203-
interpret_alloc_ids: FxHashSet(),
204203
interpret_allocs_inverse: Vec::new(),
205204
codemap: CachingCodemapView::new(tcx.sess.codemap()),
206205
file_to_file_index,
@@ -279,7 +278,12 @@ impl<'sess> OnDiskCache<'sess> {
279278
let mut interpret_alloc_index = Vec::new();
280279
let mut n = 0;
281280
loop {
282-
let new_n = encoder.interpret_alloc_ids.len();
281+
let new_n = encoder.interpret_allocs_inverse.len();
282+
// if we have found new ids, serialize those, too
283+
if n == new_n {
284+
// otherwise, abort
285+
break;
286+
}
283287
for idx in n..new_n {
284288
let id = encoder.interpret_allocs_inverse[idx];
285289
let pos = AbsoluteBytePos::new(encoder.position());
@@ -290,11 +294,6 @@ impl<'sess> OnDiskCache<'sess> {
290294
id,
291295
)?;
292296
}
293-
// if we have found new ids, serialize those, too
294-
if n == new_n {
295-
// otherwise, abort
296-
break;
297-
}
298297
n = new_n;
299298
}
300299
interpret_alloc_index
@@ -798,7 +797,6 @@ struct CacheEncoder<'enc, 'a, 'tcx, E>
798797
expn_info_shorthands: FxHashMap<Mark, AbsoluteBytePos>,
799798
interpret_allocs: FxHashMap<interpret::AllocId, usize>,
800799
interpret_allocs_inverse: Vec<interpret::AllocId>,
801-
interpret_alloc_ids: FxHashSet<interpret::AllocId>,
802800
codemap: CachingCodemapView<'tcx>,
803801
file_to_file_index: FxHashMap<*const FileMap, FileMapIndex>,
804802
}
@@ -835,14 +833,15 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<interpret::AllocId> for CacheEncoder<
835833
where E: 'enc + ty_codec::TyEncoder
836834
{
837835
fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
838-
let index = if self.interpret_alloc_ids.insert(*alloc_id) {
839-
let idx = self.interpret_alloc_ids.len() - 1;
840-
assert_eq!(idx, self.interpret_allocs_inverse.len());
841-
self.interpret_allocs_inverse.push(*alloc_id);
842-
assert!(self.interpret_allocs.insert(*alloc_id, idx).is_none());
843-
idx
844-
} else {
845-
self.interpret_allocs[alloc_id]
836+
use std::collections::hash_map::Entry;
837+
let index = match self.interpret_allocs.entry(*alloc_id) {
838+
Entry::Occupied(e) => *e.get(),
839+
Entry::Vacant(e) => {
840+
let idx = self.interpret_allocs_inverse.len();
841+
self.interpret_allocs_inverse.push(*alloc_id);
842+
e.insert(idx);
843+
idx
844+
},
846845
};
847846

848847
index.encode(self)

src/librustc_metadata/encoder.rs

Lines changed: 12 additions & 13 deletions
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),
@@ -1786,7 +1786,6 @@ pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
17861786
filemap_cache: tcx.sess.codemap().files()[0].clone(),
17871787
interpret_allocs: Default::default(),
17881788
interpret_allocs_inverse: Default::default(),
1789-
interpret_alloc_ids: Default::default(),
17901789
};
17911790

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

0 commit comments

Comments
 (0)