Skip to content

Tweak VecCache to improve performance#138405

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Zoxc:vec-cache-tweaks
Feb 1, 2026
Merged

Tweak VecCache to improve performance#138405
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Zoxc:vec-cache-tweaks

Conversation

@Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Mar 12, 2025

This has some tweaks to VecCache to improve performance.

  • It saves a compare_exchange in complete using the new put_unique function.
  • It removes bound checks on entries. These are instead checked in the slot_index_exhaustive test.
  • initialize_bucket is outlined and tuned for that.

cc @Mark-Simulacrum

@rustbot
Copy link
Collaborator

rustbot commented Mar 12, 2025

r? @Noratrieb

rustbot has assigned @Noratrieb.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 12, 2025
@Kobzol
Copy link
Member

Kobzol commented Mar 12, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 12, 2025
@bors
Copy link
Collaborator

bors commented Mar 12, 2025

⌛ Trying commit 00e7039 with merge 0b0612c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 12, 2025
Tweak `VecCache` to improve performance

This has some tweaks to `VecCache` to improve performance.

- It saves a `compare_exchange` in `complete` using the new `put_unique` function.
- It removes bound checks on entries. These are instead checked in the `slot_index_exhaustive` test.
- `initialize_bucket` is outlined and tuned for that.

cc `@Mark-Simulacrum`
@bors
Copy link
Collaborator

bors commented Mar 12, 2025

☀️ Try build successful - checks-actions
Build commit: 0b0612c (0b0612c92faadc7268901ca7f18b6859d25f4eba)

@rust-timer

This comment has been minimized.

#[cold]
fn initialize_bucket<V>(&self, bucket: &AtomicPtr<Slot<V>>) -> *mut Slot<V> {
#[inline(never)]
fn initialize_bucket<V>(bucket: &AtomicPtr<Slot<V>>, bucket_idx: usize) -> *mut Slot<V> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It avoids Self needing it exist when not inlined, as the needed information can be passed in registers instead.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0b0612c): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.9% [0.2%, 1.8%] 15
Improvements ✅
(primary)
-0.4% [-0.9%, -0.2%] 140
Improvements ✅
(secondary)
-0.6% [-1.2%, -0.2%] 42
All ❌✅ (primary) -0.4% [-0.9%, -0.2%] 140

Max RSS (memory usage)

Results (primary -2.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.0% [-2.8%, -1.2%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.0% [-2.8%, -1.2%] 2

Cycles

Results (primary 4.1%, secondary -3.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
6.8% [1.6%, 13.7%] 13
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.7% [-2.1%, -1.4%] 6
Improvements ✅
(secondary)
-3.1% [-3.5%, -2.5%] 3
All ❌✅ (primary) 4.1% [-2.1%, 13.7%] 19

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 779.667s -> 779.823s (0.02%)
Artifact size: 365.24 MiB -> 365.04 MiB (-0.06%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 12, 2025
@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 12, 2025

Local results:

BenchmarkBeforeAfterBeforeAfterBeforeAfter
TimeTime%Physical MemoryPhysical Memory%Committed MemoryCommitted Memory%
🟣 clap:check1.4028s1.3962s -0.47%147.60 MiB147.57 MiB -0.02%192.07 MiB192.06 MiB -0.01%
🟣 hyper:check0.2335s0.2321s -0.58%80.01 MiB79.95 MiB -0.07%121.83 MiB121.83 MiB -0.01%
🟣 regex:check0.7944s0.7918s -0.33%108.43 MiB108.50 MiB 0.07%146.66 MiB146.80 MiB 0.09%
🟣 syn:check1.3258s1.3183s -0.57%141.93 MiB141.92 MiB -0.01%180.31 MiB180.36 MiB 0.03%
Total3.7565s3.7385s -0.48%477.97 MiB477.95 MiB -0.00%640.87 MiB641.04 MiB 0.03%
Summary1.0000s0.9951s -0.49%1 byte1.00 bytes -0.01%1 byte1.00 bytes 0.03%

Looks like something might be up with html5ever.

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 12, 2025

I can't reproduce the html5ever regression locally:

BenchmarkBeforeAfterBeforeAfterBeforeAfter
TimeTime%Physical MemoryPhysical Memory%Committed MemoryCommitted Memory%
🟣 html5ever:check0.6299s0.6256s -0.67%104.10 MiB103.98 MiB -0.11%147.54 MiB147.58 MiB 0.02%
Total0.6299s0.6256s -0.67%104.10 MiB103.98 MiB -0.11%147.54 MiB147.58 MiB 0.02%
Summary1.0000s0.9933s -0.67%1 byte1.00 bytes -0.11%1 byte1.00 bytes 0.02%

@Kobzol
Copy link
Member

Kobzol commented Mar 12, 2025

Cycles are noisy, although this was high above the threshold. Let's try again, just in case it was a fluke.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 12, 2025
@bors
Copy link
Collaborator

bors commented Mar 12, 2025

⌛ Trying commit 00e7039 with merge d06bb14...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 12, 2025
Tweak `VecCache` to improve performance

This has some tweaks to `VecCache` to improve performance.

- It saves a `compare_exchange` in `complete` using the new `put_unique` function.
- It removes bound checks on entries. These are instead checked in the `slot_index_exhaustive` test.
- `initialize_bucket` is outlined and tuned for that.

cc `@Mark-Simulacrum`
@bors
Copy link
Collaborator

bors commented Mar 12, 2025

☀️ Try build successful - checks-actions
Build commit: d06bb14 (d06bb14bb9c4c2e005971e2f02d13bb79af6845e)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d06bb14): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.3% [0.6%, 1.8%] 10
Improvements ✅
(primary)
-0.4% [-0.9%, -0.2%] 143
Improvements ✅
(secondary)
-0.6% [-1.3%, -0.2%] 43
All ❌✅ (primary) -0.4% [-0.9%, -0.2%] 143

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

Results (primary -1.5%, secondary -2.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.5% [-1.5%, -1.5%] 1
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) -1.5% [-1.5%, -1.5%] 1

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 779.458s -> 779.323s (-0.02%)
Artifact size: 365.27 MiB -> 365.05 MiB (-0.06%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 13, 2025
@Kobzol
Copy link
Member

Kobzol commented Mar 13, 2025

Looks like it was a fluke after all.

@apiraino
Copy link
Contributor

So, is this patch actually ... needed? Thanks

cc @Zoxc @Kobzol

@lqd
Copy link
Member

lqd commented Apr 24, 2025

yes

@wesleywiser
Copy link
Member

r? rust-lang/compiler

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f020bce): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking 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.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 2
Improvements ✅
(primary)
-0.4% [-1.0%, -0.1%] 145
Improvements ✅
(secondary)
-0.5% [-1.3%, -0.0%] 98
All ❌✅ (primary) -0.4% [-1.0%, -0.1%] 145

Max RSS (memory usage)

Results (primary 1.7%, secondary 3.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.7% [1.7%, 1.7%] 1
Regressions ❌
(secondary)
5.2% [4.5%, 5.6%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-3.2%, -1.9%] 2
All ❌✅ (primary) 1.7% [1.7%, 1.7%] 1

Cycles

Results (secondary -0.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.4% [2.3%, 4.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.0% [-4.2%, -3.8%] 2
All ❌✅ (primary) - - 0

Binary size

Results (primary -0.0%, secondary -0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.0% [-0.0%, -0.0%] 4
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) -0.0% [-0.0%, -0.0%] 4

Bootstrap: 472.506s -> 470.841s (-0.35%)
Artifact size: 383.60 MiB -> 383.49 MiB (-0.03%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 27, 2026
@Zoxc
Copy link
Contributor Author

Zoxc commented Jan 27, 2026

Are we actually sure that just because the indices are unique they won't cause a race?

If the indices are unique, the location they operate on are also unique and won't cause a race.

I'm worried about multiple threads calling VecCache::complete at the same time (if that could ever happen), at which point acquiring the lock on the slot seems necessary.

It still uses a lock for the main slots, but this changes the secondary list of inserted keys to just insert without atomics as the length (used as the index) is unique for each VecCache::complete operation.

@Zoxc
Copy link
Contributor Author

Zoxc commented Jan 27, 2026

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 27, 2026
@fee1-dead
Copy link
Member

Not too comfortable reviewing this, so

@rustbot reroll

@rustbot rustbot assigned JonathanBrouwer and unassigned fee1-dead Jan 28, 2026
@JonathanBrouwer
Copy link
Contributor

Also not comfortable,
r? @Mark-Simulacrum as you seem to have made changes here before

@Mark-Simulacrum
Copy link
Member

Can you update the PR description to reflect the more limited change being made here? r=me with that done.

@Zoxc
Copy link
Contributor Author

Zoxc commented Feb 1, 2026

@Mark-Simulacrum Not sure what you mean, the description is still accurate?

@Mark-Simulacrum
Copy link
Member

@bors r+

Ah, I misread the line about complete. Sorry about that.

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 1, 2026

📌 Commit cd18a8d has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 1, 2026
@rust-bors

This comment has been minimized.

@rust-bors rust-bors bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 1, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 1, 2026

☀️ Test successful - CI
Approved by: Mark-Simulacrum
Duration: 3h 7m 38s
Pushing 57d2fb1 to main...

@rust-bors rust-bors bot merged commit 57d2fb1 into rust-lang:main Feb 1, 2026
13 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 1, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 1, 2026

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 8340622 (parent) -> 57d2fb1 (this PR)

Test differences

Show 5 test diffs

Stage 0

  • vec_cache::tests::bucket_entries_matches: [missing] -> pass (J0)

Stage 1

  • vec_cache::tests::bucket_entries_matches: [missing] -> pass (J1)

Additionally, 3 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 57d2fb136650d05efb3ed3ea33b330bfc85844d5 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-aarch64-apple: 6182.7s -> 7882.0s (+27.5%)
  2. i686-gnu-2: 5092.9s -> 6297.8s (+23.7%)
  3. aarch64-gnu-llvm-20-1: 3300.1s -> 3920.0s (+18.8%)
  4. dist-aarch64-msvc: 6052.5s -> 7180.5s (+18.6%)
  5. x86_64-rust-for-linux: 2573.5s -> 3044.8s (+18.3%)
  6. pr-check-1: 1645.8s -> 1928.9s (+17.2%)
  7. dist-aarch64-llvm-mingw: 5535.6s -> 6393.4s (+15.5%)
  8. i686-gnu-nopt-1: 7174.1s -> 8255.3s (+15.1%)
  9. aarch64-gnu-debug: 3869.3s -> 4448.6s (+15.0%)
  10. x86_64-gnu-stable: 7542.5s -> 8600.9s (+14.0%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@Zoxc Zoxc deleted the vec-cache-tweaks branch February 1, 2026 22:24
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (57d2fb1): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.1% [0.0%, 0.2%] 3
Improvements ✅
(primary)
-0.4% [-1.2%, -0.2%] 126
Improvements ✅
(secondary)
-0.5% [-1.3%, -0.0%] 93
All ❌✅ (primary) -0.4% [-1.2%, -0.2%] 126

Max RSS (memory usage)

Results (primary -1.1%, secondary -3.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.1% [-1.1%, -1.1%] 1
Improvements ✅
(secondary)
-3.6% [-3.6%, -3.6%] 1
All ❌✅ (primary) -1.1% [-1.1%, -1.1%] 1

Cycles

Results (secondary 0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
5.5% [2.0%, 14.8%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-6.7% [-8.1%, -5.2%] 4
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 472.577s -> 473.934s (0.29%)
Artifact size: 397.87 MiB -> 397.68 MiB (-0.05%)

@lqd
Copy link
Member

lqd commented Feb 2, 2026

Tiny regressions on a synthetic benchmark, and the overall benefits easily outweigh them.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.