Skip to content

Commit 65b65b8

Browse files
committed
Use two caches on x64 for simplicity
1 parent 22231ea commit 65b65b8

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

crates/std_detect/src/detect/cache.rs

+9-20
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ impl Initializer {
7777
}
7878

7979
/// This global variable is a cache of the features supported by the CPU.
80-
#[cfg(target_pointer_width = "64")]
81-
static CACHE: [Cache; 1] = [Cache::uninitialized()];
82-
83-
/// This global variable is a cache of the features supported by the CPU.
84-
#[cfg(target_pointer_width = "32")]
80+
// Note: on x64, we only use the first slot
8581
static CACHE: [Cache; 2] = [Cache::uninitialized(), Cache::uninitialized()];
8682

8783
/// Feature cache with capacity for `usize::max_value() - 1` features.
@@ -145,10 +141,7 @@ cfg_if::cfg_if! {
145141
#[inline]
146142
fn do_initialize(value: Initializer) {
147143
CACHE[0].initialize((value.0) as usize & Cache::MASK);
148-
#[cfg(target_pointer_width = "32")]
149-
{
150-
CACHE[1].initialize((value.0 >> Cache::CAPACITY) as usize & Cache::MASK);
151-
}
144+
CACHE[1].initialize((value.0 >> Cache::CAPACITY) as usize & Cache::MASK);
152145
}
153146

154147
/// Tests the `bit` of the storage. If the storage has not been initialized,
@@ -168,18 +161,14 @@ pub(crate) fn test<F>(bit: u32, f: F) -> bool
168161
where
169162
F: FnOnce() -> Initializer,
170163
{
171-
#[cfg(target_pointer_width = "32")]
172-
{
173-
if bit >= Cache::CAPACITY {
174-
if CACHE[1].is_uninitialized() {
175-
initialize(f())
176-
}
177-
return CACHE[1].test(bit - Cache::CAPACITY);
178-
}
179-
}
164+
let (bit, idx) = if bit < Cache::CAPACITY {
165+
(bit, 0)
166+
} else {
167+
(bit - Cache::CAPACITY, 1)
168+
};
180169

181-
if CACHE[0].is_uninitialized() {
170+
if CACHE[idx].is_uninitialized() {
182171
initialize(f())
183172
}
184-
CACHE[0].test(bit)
173+
CACHE[idx].test(bit - Cache::CAPACITY)
185174
}

0 commit comments

Comments
 (0)