about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src/vec_cache
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-06-08 15:26:49 +0000
committerbors <bors@rust-lang.org>2025-06-08 15:26:49 +0000
commitfb644e6a1a7d34c6bbb5ecfe5c185f8c977d6bb3 (patch)
tree8490a16eeb3a6fd12150195172a3b21e38cb0f0d /compiler/rustc_data_structures/src/vec_cache
parent244bbfc60ee8593db96892468eee876240cb7ba1 (diff)
parent8a56ed4c89d38618e065f7d9ec6be0390fdabe31 (diff)
downloadrust-fb644e6a1a7d34c6bbb5ecfe5c185f8c977d6bb3.tar.gz
rust-fb644e6a1a7d34c6bbb5ecfe5c185f8c977d6bb3.zip
Auto merge of #142095 - joshtriplett:optimize-veccache, r=SparrowLii
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.)

---

Noticed because `slot_index_exhaustive` stood out as taking unusually long compared to other tests, so I started investigating what it was doing.
Diffstat (limited to 'compiler/rustc_data_structures/src/vec_cache')
-rw-r--r--compiler/rustc_data_structures/src/vec_cache/tests.rs19
1 files changed, 8 insertions, 11 deletions
diff --git a/compiler/rustc_data_structures/src/vec_cache/tests.rs b/compiler/rustc_data_structures/src/vec_cache/tests.rs
index 3b65c14162e..9b60913ec92 100644
--- a/compiler/rustc_data_structures/src/vec_cache/tests.rs
+++ b/compiler/rustc_data_structures/src/vec_cache/tests.rs
@@ -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;
     }
 }