Skip to content

Commit 7a5ecc8

Browse files
Merge pull request #22824 from ChayimFriedman2/interner-tls-cache
minor: Do not store `Option` in the interner cache's thread local
2 parents 8ff1f80 + e281b55 commit 7a5ecc8

4 files changed

Lines changed: 24 additions & 17 deletions

File tree

crates/base-db/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ impl Default for Nonce {
315315
}
316316

317317
impl Nonce {
318+
#[inline]
319+
pub const fn invalid() -> Nonce {
320+
Nonce(usize::MAX)
321+
}
322+
318323
#[inline]
319324
pub fn new() -> Nonce {
320325
Nonce(NEXT_NONCE.fetch_add(1, std::sync::atomic::Ordering::SeqCst))

crates/hir-def/src/test_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Clone for TestDB {
6868
files: self.files.clone(),
6969
crates_map: self.crates_map.clone(),
7070
events: self.events.clone(),
71-
nonce: Nonce::new(),
71+
nonce: self.nonce,
7272
}
7373
}
7474
}

crates/hir-ty/src/next_solver/interner.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,42 +2484,44 @@ mod tls_cache {
24842484
db_nonce: Nonce,
24852485
}
24862486

2487+
impl Cache {
2488+
const fn default() -> Cache {
2489+
Cache {
2490+
cache: GlobalCache::new(),
2491+
revision: Revision::max(),
2492+
db_nonce: Nonce::invalid(),
2493+
}
2494+
}
2495+
}
2496+
24872497
thread_local! {
2488-
static GLOBAL_CACHE: RefCell<Option<Cache>> = const { RefCell::new(None) };
2498+
static GLOBAL_CACHE: RefCell<Cache> = const { RefCell::new(Cache::default()) };
24892499
}
24902500

24912501
pub(super) fn reinit_cache(db: &dyn HirDatabase) {
24922502
GLOBAL_CACHE.with_borrow_mut(|handle| {
24932503
let (db_nonce, revision) = db.nonce_and_revision();
2494-
match handle {
2495-
Some(handle) => {
2496-
if handle.revision != revision || db_nonce != handle.db_nonce {
2497-
*handle = Cache { cache: GlobalCache::default(), revision, db_nonce };
2498-
}
2499-
}
2500-
None => *handle = Some(Cache { cache: GlobalCache::default(), revision, db_nonce }),
2504+
if handle.revision != revision || db_nonce != handle.db_nonce {
2505+
*handle = Cache { cache: GlobalCache::default(), revision, db_nonce };
25012506
}
25022507
})
25032508
}
25042509

2510+
#[inline]
25052511
pub(super) fn borrow_assume_valid<'db, T>(
25062512
db: &'db dyn HirDatabase,
25072513
f: impl FnOnce(&mut GlobalCache<DbInterner<'db>>) -> T,
25082514
) -> T {
25092515
if cfg!(debug_assertions) {
2510-
let get_state = || {
2511-
GLOBAL_CACHE.with_borrow(|handle| {
2512-
handle.as_ref().map(|handle| (handle.db_nonce, handle.revision))
2513-
})
2514-
};
2516+
let get_state =
2517+
|| GLOBAL_CACHE.with_borrow(|handle| (handle.db_nonce, handle.revision));
25152518
let old_state = get_state();
25162519
reinit_cache(db);
25172520
let new_state = get_state();
25182521
assert_eq!(old_state, new_state, "you assumed the cache is valid!");
25192522
}
25202523

25212524
GLOBAL_CACHE.with_borrow_mut(|handle| {
2522-
let handle = handle.as_mut().expect("you assumed the cache is valid!");
25232525
// SAFETY: No idea
25242526
f(unsafe {
25252527
std::mem::transmute::<
@@ -2535,7 +2537,7 @@ mod tls_cache {
25352537
/// Should be called before getting memory usage estimations, as the solver cache
25362538
/// is per-revision and usually should be excluded from estimations.
25372539
pub fn clear_tls_solver_cache() {
2538-
GLOBAL_CACHE.with_borrow_mut(|handle| *handle = None);
2540+
GLOBAL_CACHE.with_borrow_mut(|handle| *handle = Cache::default());
25392541
}
25402542
}
25412543

crates/hir-ty/src/test_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Clone for TestDB {
6464
files: self.files.clone(),
6565
crates_map: self.crates_map.clone(),
6666
events: self.events.clone(),
67-
nonce: Nonce::new(),
67+
nonce: self.nonce,
6868
}
6969
}
7070
}

0 commit comments

Comments
 (0)