Skip to content

Simplify and optimize VecCache's SlotIndex::from_index #142095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions compiler/rustc_data_structures/src/vec_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ impl SlotIndex {
// slots (see `slot_index_exhaustive` in tests).
#[inline]
const fn from_index(idx: u32) -> Self {
let mut bucket = match idx.checked_ilog2() {
Some(x) => x as usize,
None => 0,
};
let entries;
let running_sum;
if bucket <= 11 {
entries = 1 << 12;
running_sum = 0;
bucket = 0;
} else {
entries = 1 << bucket;
running_sum = entries;
bucket = bucket - 11;
const FIRST_BUCKET_SHIFT: usize = 12;
if idx < (1 << FIRST_BUCKET_SHIFT) {
return SlotIndex {
bucket_idx: 0,
entries: 1 << FIRST_BUCKET_SHIFT,
index_in_bucket: idx as usize,
};
}
// SAFETY: We already ruled out idx 0, so `checked_ilog2` can't return `None`.
let bucket = unsafe { idx.checked_ilog2().unwrap_unchecked() as usize };
let entries = 1 << bucket;
SlotIndex {
bucket_idx: bucket - FIRST_BUCKET_SHIFT + 1,
entries,
index_in_bucket: idx as usize - entries,
}
SlotIndex { bucket_idx: bucket, entries, index_in_bucket: idx as usize - running_sum }
}

// SAFETY: Buckets must be managed solely by functions here (i.e., get/put on SlotIndex) and
Expand Down
19 changes: 8 additions & 11 deletions compiler/rustc_data_structures/src/vec_cache/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,21 @@ fn slot_index_exhaustive() {
for idx in 0..=u32::MAX {
buckets[SlotIndex::from_index(idx).bucket_idx] += 1;
}
let mut prev = None::<SlotIndex>;
for idx in 0..=u32::MAX {
let slot_idx = SlotIndex::from_index(0);
assert_eq!(slot_idx.index_in_bucket, 0);
assert_eq!(slot_idx.bucket_idx, 0);
let mut prev = slot_idx;
for idx in 1..=u32::MAX {
let slot_idx = SlotIndex::from_index(idx);
if let Some(p) = prev {
if p.bucket_idx == slot_idx.bucket_idx {
assert_eq!(p.index_in_bucket + 1, slot_idx.index_in_bucket);
} else {
assert_eq!(slot_idx.index_in_bucket, 0);
}
if prev.bucket_idx == slot_idx.bucket_idx {
assert_eq!(prev.index_in_bucket + 1, slot_idx.index_in_bucket);
} else {
assert_eq!(idx, 0);
assert_eq!(slot_idx.index_in_bucket, 0);
assert_eq!(slot_idx.bucket_idx, 0);
}

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

prev = Some(slot_idx);
prev = slot_idx;
}
}
Loading