Skip to content

Commit 43ee7cd

Browse files
committed
Simplify and optimize SlotIndex::from_index
Break out bucket 0 (containing `idx < 4096`) as an early return, which simplifies the remainder of the function, and allows optimizing the `checked_ilog2` since it can no longer return `None`. This reduces the runtime of `vec_cache::tests::slot_index_exhaustive` (which calls `SlotIndex::from_index` for every `u32`, twice) from ~15.5s to ~13.3s.
1 parent df8102f commit 43ee7cd

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

compiler/rustc_data_structures/src/vec_cache.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,13 @@ impl SlotIndex {
6868
// slots (see `slot_index_exhaustive` in tests).
6969
#[inline]
7070
const fn from_index(idx: u32) -> Self {
71-
let mut bucket = match idx.checked_ilog2() {
72-
Some(x) => x as usize,
73-
None => 0,
74-
};
75-
let entries;
76-
let running_sum;
77-
if bucket <= 11 {
78-
entries = 1 << 12;
79-
running_sum = 0;
80-
bucket = 0;
81-
} else {
82-
entries = 1 << bucket;
83-
running_sum = entries;
84-
bucket = bucket - 11;
71+
if idx < 4096 {
72+
return SlotIndex { bucket_idx: 0, entries: 4096, index_in_bucket: idx as usize };
8573
}
86-
SlotIndex { bucket_idx: bucket, entries, index_in_bucket: idx as usize - running_sum }
74+
// SAFETY: We already ruled out idx 0, so `checked_ilog2` can't return `None`.
75+
let bucket = unsafe { idx.checked_ilog2().unwrap_unchecked() as usize };
76+
let entries = 1 << bucket;
77+
SlotIndex { bucket_idx: bucket - 11, entries, index_in_bucket: idx as usize - entries }
8778
}
8879

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

0 commit comments

Comments
 (0)