about summary refs log tree commit diff
path: root/compiler/rustc_data_structures
diff options
context:
space:
mode:
authorJosh Triplett <josh@joshtriplett.org>2025-06-05 12:02:04 -0700
committerJosh Triplett <josh@joshtriplett.org>2025-06-05 12:09:00 -0700
commit43ee7cd57c1706b949c92d6b29a7034b40dff533 (patch)
treeed26d3382bcccee7712b4deb6f0f9b5d77163133 /compiler/rustc_data_structures
parentdf8102fe5f24f28a918660b0cd918d7331c3896e (diff)
downloadrust-43ee7cd57c1706b949c92d6b29a7034b40dff533.tar.gz
rust-43ee7cd57c1706b949c92d6b29a7034b40dff533.zip
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.
Diffstat (limited to 'compiler/rustc_data_structures')
-rw-r--r--compiler/rustc_data_structures/src/vec_cache.rs21
1 files changed, 6 insertions, 15 deletions
diff --git a/compiler/rustc_data_structures/src/vec_cache.rs b/compiler/rustc_data_structures/src/vec_cache.rs
index 2ff60ab7f36..cc6973fb42e 100644
--- a/compiler/rustc_data_structures/src/vec_cache.rs
+++ b/compiler/rustc_data_structures/src/vec_cache.rs
@@ -68,22 +68,13 @@ 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;
+        if idx < 4096 {
+            return SlotIndex { bucket_idx: 0, entries: 4096, index_in_bucket: idx as usize };
         }
-        SlotIndex { bucket_idx: bucket, entries, index_in_bucket: idx as usize - running_sum }
+        // 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 - 11, entries, index_in_bucket: idx as usize - entries }
     }
 
     // SAFETY: Buckets must be managed solely by functions here (i.e., get/put on SlotIndex) and