Skip to content

Commit 47a8eb7

Browse files
committed
Auto merge of #47373 - wesleywiser:incr_cache_hashing, r=michaelwoerister
[Incremental] Cache hashes for AdDef and ty::Slice<T> r? @michaelwoerister
2 parents ae920dc + 45bd091 commit 47a8eb7

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

src/librustc/ich/impls_ty.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
//! This module contains `HashStable` implementations for various data types
1212
//! from rustc::ty in no particular order.
1313
14-
use ich::{StableHashingContext, NodeIdHashingMode};
14+
use ich::{Fingerprint, StableHashingContext, NodeIdHashingMode};
15+
use rustc_data_structures::fx::FxHashMap;
1516
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
1617
StableHasher, StableHasherResult};
18+
use std::cell::RefCell;
1719
use std::hash as std_hash;
1820
use std::mem;
1921
use middle::region;
@@ -26,7 +28,26 @@ for &'gcx ty::Slice<T>
2628
fn hash_stable<W: StableHasherResult>(&self,
2729
hcx: &mut StableHashingContext<'gcx>,
2830
hasher: &mut StableHasher<W>) {
29-
(&self[..]).hash_stable(hcx, hasher);
31+
thread_local! {
32+
static CACHE: RefCell<FxHashMap<(usize, usize), Fingerprint>> =
33+
RefCell::new(FxHashMap());
34+
}
35+
36+
let hash = CACHE.with(|cache| {
37+
let key = (self.as_ptr() as usize, self.len());
38+
if let Some(&hash) = cache.borrow().get(&key) {
39+
return hash;
40+
}
41+
42+
let mut hasher = StableHasher::new();
43+
(&self[..]).hash_stable(hcx, &mut hasher);
44+
45+
let hash: Fingerprint = hasher.finish();
46+
cache.borrow_mut().insert(key, hash);
47+
hash
48+
});
49+
50+
hash.hash_stable(hcx, hasher);
3051
}
3152
}
3253

src/librustc/ty/mod.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use hir::def::{Def, CtorKind, ExportMap};
2020
use hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
2121
use hir::map::DefPathData;
2222
use hir::svh::Svh;
23+
use ich::Fingerprint;
2324
use ich::StableHashingContext;
2425
use middle::const_val::ConstVal;
2526
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
@@ -37,6 +38,7 @@ use util::common::ErrorReported;
3738
use util::nodemap::{NodeSet, DefIdMap, FxHashMap, FxHashSet};
3839

3940
use serialize::{self, Encodable, Encoder};
41+
use std::cell::RefCell;
4042
use std::collections::BTreeMap;
4143
use std::cmp;
4244
use std::fmt;
@@ -1476,17 +1478,32 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for AdtDef {
14761478
fn hash_stable<W: StableHasherResult>(&self,
14771479
hcx: &mut StableHashingContext<'gcx>,
14781480
hasher: &mut StableHasher<W>) {
1479-
let ty::AdtDef {
1480-
did,
1481-
ref variants,
1482-
ref flags,
1483-
ref repr,
1484-
} = *self;
1485-
1486-
did.hash_stable(hcx, hasher);
1487-
variants.hash_stable(hcx, hasher);
1488-
flags.hash_stable(hcx, hasher);
1489-
repr.hash_stable(hcx, hasher);
1481+
thread_local! {
1482+
static CACHE: RefCell<FxHashMap<usize, Fingerprint>> =
1483+
RefCell::new(FxHashMap());
1484+
}
1485+
1486+
let hash: Fingerprint = CACHE.with(|cache| {
1487+
let addr = self as *const AdtDef as usize;
1488+
*cache.borrow_mut().entry(addr).or_insert_with(|| {
1489+
let ty::AdtDef {
1490+
did,
1491+
ref variants,
1492+
ref flags,
1493+
ref repr,
1494+
} = *self;
1495+
1496+
let mut hasher = StableHasher::new();
1497+
did.hash_stable(hcx, &mut hasher);
1498+
variants.hash_stable(hcx, &mut hasher);
1499+
flags.hash_stable(hcx, &mut hasher);
1500+
repr.hash_stable(hcx, &mut hasher);
1501+
1502+
hasher.finish()
1503+
})
1504+
});
1505+
1506+
hash.hash_stable(hcx, hasher);
14901507
}
14911508
}
14921509

0 commit comments

Comments
 (0)