Skip to content

Commit

Permalink
completely outline DelayedMap cache accesses
Browse files Browse the repository at this point in the history
also increase the cutoff
  • Loading branch information
lcnr committed Sep 27, 2024
1 parent f550e0f commit 999add5
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions compiler/rustc_type_ir/src/data_structures/delayed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::hash::Hash;

use crate::data_structures::{HashMap, HashSet};

const CACHE_CUTOFF: u32 = 16;
const CACHE_CUTOFF: u32 = 32;

/// A hashmap which only starts hashing after ignoring the first few inputs.
///
Expand All @@ -22,18 +22,35 @@ impl<K, V> Default for DelayedMap<K, V> {
}

impl<K: Hash + Eq, V> DelayedMap<K, V> {
#[inline]
#[inline(always)]
pub fn insert(&mut self, key: K, value: V) -> bool {
if self.count >= CACHE_CUTOFF {
self.cache.insert(key, value).is_none()
self.cold_insert(key, value)
} else {
self.count += 1;
true
}
}

#[inline]
#[cold]
#[inline(never)]
fn cold_insert(&mut self, key: K, value: V) -> bool {
self.cache.insert(key, value).is_none()
}


#[inline(always)]
pub fn get(&self, key: &K) -> Option<&V> {
if self.cache.is_empty() {
None
} else {
self.cold_get(key)
}
}

#[cold]
#[inline(never)]
fn cold_get(&self, key: &K) -> Option<&V> {
self.cache.get(key)
}
}
Expand All @@ -51,18 +68,30 @@ impl<T> Default for DelayedSet<T> {
}

impl<T: Hash + Eq> DelayedSet<T> {
#[inline]
#[inline(always)]
pub fn insert(&mut self, value: T) -> bool {
if self.count >= CACHE_CUTOFF {
self.cache.insert(value)
self.cold_insert(value)
} else {
self.count += 1;
true
}
}

#[cold]
#[inline(never)]
fn cold_insert(&mut self, value: T) -> bool {
self.cache.insert(value)
}

#[inline]
pub fn contains(&self, value: &T) -> bool {
!self.cache.is_empty() && self.cold_contains(value)
}

#[cold]
#[inline(never)]
fn cold_contains(&self, value: &T) -> bool {
self.cache.contains(value)
}
}

0 comments on commit 999add5

Please sign in to comment.