Skip to content

Commit 18aa6ca

Browse files
committed
Auto merge of #142095 - joshtriplett:optimize-veccache, r=<try>
Simplify and optimize `VecCache`'s `SlotIndex::from_index` 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. Separately, simplify the test case as well. (The old and new code passes with the old and new test case.)
2 parents 0b20963 + 9837c3c commit 18aa6ca

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
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

compiler/rustc_data_structures/src/vec_cache/tests.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,21 @@ fn slot_index_exhaustive() {
7575
for idx in 0..=u32::MAX {
7676
buckets[SlotIndex::from_index(idx).bucket_idx] += 1;
7777
}
78-
let mut prev = None::<SlotIndex>;
79-
for idx in 0..=u32::MAX {
78+
let slot_idx = SlotIndex::from_index(0);
79+
assert_eq!(slot_idx.index_in_bucket, 0);
80+
assert_eq!(slot_idx.bucket_idx, 0);
81+
let mut prev = slot_idx;
82+
for idx in 1..=u32::MAX {
8083
let slot_idx = SlotIndex::from_index(idx);
81-
if let Some(p) = prev {
82-
if p.bucket_idx == slot_idx.bucket_idx {
83-
assert_eq!(p.index_in_bucket + 1, slot_idx.index_in_bucket);
84-
} else {
85-
assert_eq!(slot_idx.index_in_bucket, 0);
86-
}
84+
if prev.bucket_idx == slot_idx.bucket_idx {
85+
assert_eq!(prev.index_in_bucket + 1, slot_idx.index_in_bucket);
8786
} else {
88-
assert_eq!(idx, 0);
8987
assert_eq!(slot_idx.index_in_bucket, 0);
90-
assert_eq!(slot_idx.bucket_idx, 0);
9188
}
9289

9390
assert_eq!(buckets[slot_idx.bucket_idx], slot_idx.entries as u32);
9491
assert_eq!(ENTRIES_BY_BUCKET[slot_idx.bucket_idx], slot_idx.entries, "{}", idx);
9592

96-
prev = Some(slot_idx);
93+
prev = slot_idx;
9794
}
9895
}

0 commit comments

Comments
 (0)