Use a safe BucketIndex abstraction in VecCache#153434
Use a safe BucketIndex abstraction in VecCache#153434rust-bors[bot] merged 1 commit intorust-lang:mainfrom
BucketIndex abstraction in VecCache#153434Conversation
|
r? @wesleywiser rustbot has assigned @wesleywiser. Use Why was this reviewer chosen?The reviewer was selected based on:
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Use a safe `BucketIndex` abstraction in `VecCache`
This comment has been minimized.
This comment has been minimized.
This comment was marked as outdated.
This comment was marked as outdated.
|
Hmm, did my harmless tweaks somehow trigger another regression? |
This comment has been minimized.
This comment has been minimized.
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Use a safe `BucketIndex` abstraction in `VecCache`
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (a6e26e0): comparison URL. Overall result: no relevant changes - no action neededBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. @bors rollup=never Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (primary 2.3%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -1.3%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 480.787s -> 481.672s (0.18%) |
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
No changes in the rebase, but let's do a more up-to-date perf run as a precaution. @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Use a safe `BucketIndex` abstraction in `VecCache`
| #[derive(Clone, Copy, PartialEq, Eq)] | ||
| #[repr(usize)] | ||
| enum BucketIndex { | ||
| // tidy-alphabetical-start |
There was a problem hiding this comment.
Not sure this tidy directive is doing anything useful? I don't imagine this list will be changing.
There was a problem hiding this comment.
It's mostly there to reassure readers that the list is indeed numerically sorted.
| // OK, now under the allocator lock, if we're still null then it's definitely us that will | ||
| // initialize this bucket. | ||
| if ptr.is_null() { | ||
| let bucket_len = SlotIndex { bucket_idx, index_in_bucket: 0 }.entries(); |
There was a problem hiding this comment.
I think bucket_len was a misleading name? I.e. this is really the capacity.
There was a problem hiding this comment.
It was arguably correct in the sense that it's the length of the bucket, but yeah I still found it misleading in practice.
| for i in BucketIndex::iter_all() { | ||
| total += u128::try_from(i.capacity()).unwrap(); | ||
| } | ||
| assert_eq!(total, (u32::MAX as u128) + 1); |
There was a problem hiding this comment.
The + 1 seems wrong? Every bucket has an even number of entries, and the sum should be u32::MAX.
Relatedly, there is an existing comment "The total number of entries if all buckets are initialized is u32::MAX-1." The "-1" is incorrect and should be removed.
The u128 and try_from also seems like overkill here given that we know the largest bucket is 2^31.
There was a problem hiding this comment.
+1 is correct, but I've replaced it with 1 << 32 instead.
(Remember that u32::MAX == (1 << 32) - 1, which is 1 less than the total we're trying to count to.)
u128 is overkill, so I've narrowed it to u64. Using usize would overflow on 32-bit hosts, though I don't know if we ever run unit tests on those.
This comment has been minimized.
This comment has been minimized.
The current code for indexing into bucket arrays is quite tricky and unsafe, partly because it has to keep manually assuring the compiler that a bucket index is always less than 21. By encapsulating that knowledge in a 21-value enum, we can make the code clearer and safer, without giving up performance. Having a dedicated `BucketIndex` type could also help with further cleanups of `VecCache` indexing.
|
Changes since review (diff):
|
|
Finished benchmarking commit (ac46537): comparison URL. Overall result: no relevant changes - no action neededBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. @bors rollup=never Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)This benchmark run did not return any relevant results for this metric. CyclesResults (primary -4.2%, secondary -0.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 485.277s -> 483.584s (-0.35%) |
|
Yes, I was mixing up @bors r+ rollup=maybe |
View all comments
The current code for indexing into bucket arrays is quite tricky and unsafe, partly because it has to keep manually assuring the compiler that a bucket index is always less than 21.
By encapsulating that knowledge in a 21-value enum, we can make the code clearer and safer, without giving up performance.
Having a dedicated
BucketIndextype could also help with further cleanups ofVecCacheindexing.