Skip to content

Commit 8a56ed4

Browse files
committed
SlotIndex::from_index: Factor out a constant for the first bucket size
1 parent 9837c3c commit 8a56ed4

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

compiler/rustc_data_structures/src/vec_cache.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,22 @@ impl SlotIndex {
6868
// slots (see `slot_index_exhaustive` in tests).
6969
#[inline]
7070
const fn from_index(idx: u32) -> Self {
71-
if idx < 4096 {
72-
return SlotIndex { bucket_idx: 0, entries: 4096, index_in_bucket: idx as usize };
71+
const FIRST_BUCKET_SHIFT: usize = 12;
72+
if idx < (1 << FIRST_BUCKET_SHIFT) {
73+
return SlotIndex {
74+
bucket_idx: 0,
75+
entries: 1 << FIRST_BUCKET_SHIFT,
76+
index_in_bucket: idx as usize,
77+
};
7378
}
7479
// SAFETY: We already ruled out idx 0, so `checked_ilog2` can't return `None`.
7580
let bucket = unsafe { idx.checked_ilog2().unwrap_unchecked() as usize };
7681
let entries = 1 << bucket;
77-
SlotIndex { bucket_idx: bucket - 11, entries, index_in_bucket: idx as usize - entries }
82+
SlotIndex {
83+
bucket_idx: bucket - FIRST_BUCKET_SHIFT + 1,
84+
entries,
85+
index_in_bucket: idx as usize - entries,
86+
}
7887
}
7988

8089
// SAFETY: Buckets must be managed solely by functions here (i.e., get/put on SlotIndex) and

0 commit comments

Comments
 (0)