Skip to content

Commit

Permalink
Use two caches on x64 for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Jan 28, 2020
1 parent 22231ea commit 65b65b8
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions crates/std_detect/src/detect/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ impl Initializer {
}

/// This global variable is a cache of the features supported by the CPU.
#[cfg(target_pointer_width = "64")]
static CACHE: [Cache; 1] = [Cache::uninitialized()];

/// This global variable is a cache of the features supported by the CPU.
#[cfg(target_pointer_width = "32")]
// Note: on x64, we only use the first slot
static CACHE: [Cache; 2] = [Cache::uninitialized(), Cache::uninitialized()];

/// Feature cache with capacity for `usize::max_value() - 1` features.
Expand Down Expand Up @@ -145,10 +141,7 @@ cfg_if::cfg_if! {
#[inline]
fn do_initialize(value: Initializer) {
CACHE[0].initialize((value.0) as usize & Cache::MASK);
#[cfg(target_pointer_width = "32")]
{
CACHE[1].initialize((value.0 >> Cache::CAPACITY) as usize & Cache::MASK);
}
CACHE[1].initialize((value.0 >> Cache::CAPACITY) as usize & Cache::MASK);
}

/// Tests the `bit` of the storage. If the storage has not been initialized,
Expand All @@ -168,18 +161,14 @@ pub(crate) fn test<F>(bit: u32, f: F) -> bool
where
F: FnOnce() -> Initializer,
{
#[cfg(target_pointer_width = "32")]
{
if bit >= Cache::CAPACITY {
if CACHE[1].is_uninitialized() {
initialize(f())
}
return CACHE[1].test(bit - Cache::CAPACITY);
}
}
let (bit, idx) = if bit < Cache::CAPACITY {
(bit, 0)
} else {
(bit - Cache::CAPACITY, 1)
};

if CACHE[0].is_uninitialized() {
if CACHE[idx].is_uninitialized() {
initialize(f())
}
CACHE[0].test(bit)
CACHE[idx].test(bit - Cache::CAPACITY)
}

0 comments on commit 65b65b8

Please sign in to comment.