Skip to content

Share retain implementation between Vec and VecDeque - #164252

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ywxt:vec_deque_retain
Nov 26, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ywxt:vec_deque_retain

Conversation

@ywxt

@ywxt ywxt commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

It treats Vec and VecDeque as two seperate regions, allowing them to share the same retain implementation.

When processing the buffer, retain_raw splits the logical ring buffer into up to two contiguous regions. Each region is processed independently: elements rejected by f are dropped, while retained elements are moved to fill holes.

Compared to the regional code, due to wrapping, there are some regressions on the small buffers for Vec.

Type Group Small (4–32 elems) Medium (64–512 elems) Large (1k–100k elems)
Vec keep even +1% to 0% 0% to -1% +1% to -1%
Vec keep all 0% 0% to -4% 0%
Vec iter + collect reference 0% to -1% 0% 0%
VecDeque keep all / contiguous +19% to -30% -43% to -48% -49% to -50%
VecDeque keep all / wrapped +6% to -23% -34% to -49% -49% to -51%
VecDeque keep even / contiguous -5% to -26% -28% to -31% about -30%
VecDeque keep even / wrapped +12% to -15% -30% to -32% -33% to -36%
VecDeque keep second half / contiguous +4% to -43% -52% to -56% -54% to -56%
VecDeque keep second half / wrapped +9% to -35% -49% to -57% -58% to -59%
VecDeque random 50% / contiguous +23% to -14% -23% to -29% -27% to -73%
VecDeque random 50% / wrapped +6% to -1% -15% to -30% -26% to -70%
Detailed Vec benchmark results

Vec (vec::bench_retain_*), ns/iter: before → after (change)

Keep even values

4 8 16 32 64 128 256 512 1000 10000 100000
u8 6.9 → 6.9 (-0%) 9.1 → 9.1 (+0%) 14.9 → 14.9 (-0%) 25.9 → 25.9 (+0%) 53.8 → 53.8 (+0%) 98.7 → 98.7 (+0%) 187.7 → 187.7 (+0%) 367.8 → 367.8 (-0%) 718.2 → 718.1 (-0%) 7.2k → 7.2k (+0%) 71.8k → 71.8k (-0%)
u16 6.2 → 6.2 (+0%) 9.4 → 9.5 (+1%) 14.6 → 14.6 (+0%) 27.1 → 27.1 (+0%) 54.5 → 54.5 (+0%) 99.5 → 99.5 (+0%) 190.5 → 190.5 (+0%) 373.4 → 373.5 (+0%) 737.8 → 737.8 (+0%) 7.4k → 7.4k (+0%) 74.3k → 74.4k (+0%)
u32 6.6 → 6.5 (-0%) 8.4 → 8.4 (+0%) 15.0 → 15.0 (-0%) 27.1 → 27.1 (-0%) 54.5 → 54.5 (-0%) 102.1 → 102.1 (+0%) 195.8 → 195.8 (-0%) 397.0 → 392.2 (-1%) 780.6 → 788.1 (+1%) 7.8k → 7.9k (+0%) 79.1k → 78.3k (-1%)
u64 5.5 → 5.5 (+0%) 9.4 → 9.4 (+0%) 15.8 → 15.8 (+0%) 28.3 → 28.3 (+0%) 57.9 → 58.0 (+0%) 109.5 → 109.5 (+0%) 228.3 → 228.4 (+0%) 461.2 → 457.7 (-1%) 904.1 → 904.2 (+0%) 9.1k → 9.1k (-0%) 91.6k → 91.5k (-0%)

Keep all (whole)

4 8 16 32 64 128 256 512 1000 10000 100000
u8 3.0 → 3.0 (+0%) 5.1 → 5.1 (+0%) 9.4 → 9.4 (+0%) 18.6 → 18.6 (+0%) 37.0 → 37.0 (+0%) 74.4 → 74.4 (+0%) 152.8 → 152.8 (-0%) 359.9 → 359.9 (-0%) 697.1 → 697.1 (-0%) 6.9k → 6.9k (-0%) 68.8k → 68.7k (-0%)
u16 3.2 → 3.2 (+0%) 5.4 → 5.4 (+0%) 9.9 → 9.9 (+0%) 19.4 → 19.4 (+0%) 38.8 → 38.8 (+0%) 77.3 → 77.6 (+0%) 155.2 → 154.6 (-0%) 313.3 → 313.3 (+0%) 604.5 → 605.5 (+0%) 6.0k → 6.0k (+0%) 60.2k → 60.2k (+0%)
u32 3.0 → 3.0 (+0%) 5.1 → 5.1 (+0%) 9.4 → 9.4 (+0%) 18.6 → 18.6 (+0%) 37.0 → 37.0 (+0%) 74.4 → 74.5 (+0%) 157.7 → 152.8 (-3%) 359.9 → 359.9 (-0%) 697.2 → 697.1 (-0%) 6.9k → 6.9k (-0%) 69.0k → 69.0k (-0%)
u64 3.0 → 3.0 (+0%) 5.1 → 5.1 (+0%) 9.4 → 9.4 (+0%) 18.6 → 18.6 (+0%) 37.0 → 36.9 (-0%) 74.4 → 74.4 (-0%) 164.2 → 157.7 (-4%) 359.9 → 359.9 (-0%) 698.5 → 697.8 (-0%) 6.9k → 6.9k (-0%) 68.9k → 68.8k (-0%)

Reference: into_iter().filter().collect()

4 8 16 32 64 128 256 512 1000 10000 100000
u32 6.7 → 6.7 (+0%) 10.9 → 10.9 (+0%) 17.3 → 17.4 (+0%) 30.1 → 29.9 (-1%) 57.6 → 57.6 (+0%)
Detailed VecDeque benchmark results

VecDeque (vec_deque::bench_retain_*, u32), ns/iter: before → after (change)

Group 4 8 16 32 64 128 256 512 1000 10000 100000
keep all, contiguous 8.4 → 10.0 (+19%) 11.0 → 11.0 (-0%) 17.8 → 14.8 (-17%) 30.2 → 21.1 (-30%) 56.7 → 32.3 (-43%) 110.7 → 60.5 (-45%) 215.2 → 113.6 (-47%) 425.8 → 222.6 (-48%) 823.6 → 420.1 (-49%) 8.2k → 4.1k (-50%) 81.1k → 40.6k (-50%)
keep all, wrapped 11.2 → 11.8 (+6%) 14.6 → 12.9 (-11%) 20.6 → 17.4 (-15%) 33.4 → 25.8 (-23%) 58.6 → 38.4 (-34%) 112.8 → 65.1 (-42%) 221.7 → 115.8 (-48%) 436.2 → 222.1 (-49%) 837.8 → 430.1 (-49%) 8.3k → 4.1k (-51%) 81.6k → 40.7k (-50%)
keep even, contiguous 10.1 → 9.6 (-5%) 14.2 → 12.1 (-15%) 22.5 → 17.9 (-20%) 39.7 → 29.3 (-26%) 80.6 → 58.0 (-28%) 148.0 → 103.5 (-30%) 283.6 → 197.7 (-30%) 558.1 → 387.8 (-31%) 1.1k → 762.5 (-30%) 10.7k → 7.5k (-30%) 106.6k → 75.0k (-30%)
keep even, wrapped 13.4 → 14.2 (+6%) 17.6 → 19.8 (+12%) 26.6 → 25.9 (-3%) 45.6 → 38.6 (-15%) 88.7 → 62.5 (-30%) 163.2 → 114.9 (-30%) 316.9 → 221.1 (-30%) 618.5 → 423.1 (-32%) 1.2k → 801.9 (-33%) 11.9k → 7.6k (-36%) 118.2k → 75.2k (-36%)
keep second half, contiguous 10.4 → 10.8 (+4%) 14.4 → 12.7 (-12%) 23.2 → 17.9 (-23%) 41.9 → 23.8 (-43%) 91.8 → 40.8 (-56%) 158.3 → 76.6 (-52%) 306.6 → 141.7 (-54%) 610.3 → 285.6 (-53%) 1.2k → 537.6 (-54%) 11.6k → 5.1k (-56%) 115.2k → 50.6k (-56%)
keep second half, wrapped 13.5 → 14.6 (+9%) 17.6 → 17.1 (-3%) 26.8 → 22.7 (-15%) 48.1 → 31.4 (-35%) 94.5 → 48.3 (-49%) 172.1 → 79.2 (-54%) 337.8 → 145.8 (-57%) 671.6 → 289.3 (-57%) 1.3k → 538.8 (-58%) 12.5k → 5.1k (-59%) 119.9k → 50.6k (-58%)
keep random 50%, contiguous 8.1 → 10.0 (+23%) 12.7 → 12.0 (-5%) 20.4 → 18.6 (-9%) 36.4 → 31.4 (-14%) 72.1 → 55.6 (-23%) 142.7 → 103.2 (-28%) 282.5 → 199.2 (-29%) 554.9 → 399.3 (-28%) 1.1k → 776.9 (-27%) 29.1k → 7.9k (-73%) 485.5k → 353.4k (-27%)
keep random 50%, wrapped 11.1 → 11.8 (+6%) 15.8 → 15.7 (-1%) 23.4 → 24.5 (+5%) 40.9 → 40.4 (-1%) 78.7 → 66.6 (-15%) 155.2 → 115.6 (-25%) 311.4 → 218.2 (-30%) 603.4 → 421.3 (-30%) 1.1k → 789.8 (-31%) 26.1k → 7.8k (-70%) 480.3k → 353.1k (-26%)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 13, 2026
@rustbot

rustbot commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from JohnTitor, Mark-Simulacrum

@ywxt

ywxt commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

r? @Amanieu

@rustbot rustbot assigned Amanieu and unassigned JohnTitor Oct 13, 2026
@ywxt
ywxt force-pushed the vec_deque_retain branch from 20024ce to ef0372a Compare October 14, 2026 07:30
@ywxt

ywxt commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

A simpler way is to invoke make_contiguous and then reuse the original Vec::retain_mut. I'm not sure which one is better.

@Amanieu

Amanieu commented Oct 19, 2026

Copy link
Copy Markdown
Member

@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 Oct 19, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 19, 2026
Share retain implementation between Vec and VecDeque
@rust-bors

rust-bors Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 702c61a (702c61ac118282c390460c97c2db396166c20084)
Base parent: bd4c6cc (bd4c6cc4aeef0eb9919c6f73c1e690677e9c6b38)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (702c61a): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@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.2% [0.1%, 0.9%] 44
Regressions ❌
(secondary)
0.3% [0.1%, 0.4%] 23
Improvements ✅
(primary)
-0.6% [-0.8%, -0.4%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-0.8%, 0.9%] 46

Max RSS (memory usage)

Results (primary -1.9%, secondary 0.8%)

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

mean range count
Regressions ❌
(primary)
1.3% [1.3%, 1.3%] 1
Regressions ❌
(secondary)
3.0% [2.2%, 3.8%] 2
Improvements ✅
(primary)
-5.0% [-5.0%, -5.0%] 1
Improvements ✅
(secondary)
-0.7% [-0.9%, -0.5%] 3
All ❌✅ (primary) -1.9% [-5.0%, 1.3%] 2

Cycles

Results (secondary 0.8%)

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.8% [0.5%, 1.4%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.3%, secondary 0.5%)

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

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

Bootstrap: 488.943s -> 491.657s (0.56%)
Artifact size: 409.03 MiB -> 409.15 MiB (0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 19, 2026
@Amanieu

Amanieu commented Oct 19, 2026

Copy link
Copy Markdown
Member

I haven't read through the code yet, but the perf regressions are very concerning, could you look into them?

@ywxt

ywxt commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Sure, It looks like a huge regression. 💔

@ywxt
ywxt force-pushed the vec_deque_retain branch from ef0372a to 7a6fe94 Compare October 21, 2026 08:57
@rustbot

This comment has been minimized.

@ywxt

ywxt commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

I rewrite it. It has the approximate instruction count on local. Could you rerun rustc-perf again?

@Amanieu

Amanieu commented Oct 21, 2026

Copy link
Copy Markdown
Member

@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 Oct 21, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 21, 2026
Share retain implementation between Vec and VecDeque
@rust-bors

rust-bors Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6f74f12 (6f74f12f747d78a73f86f31f72f66bc2e27b94c8)
Base parent: 1731fae (1731faea0da5b4ebf881d8e53f98066c17a74711)

@rust-bors

rust-bors Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 160d4ee (160d4eea122d476126274a57935dcd5383c7c9da)
Base parent: 560abf7 (560abf7946ae41fd210edac5dfe829d7eff86eec)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (160d4ee): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=iffy rustc-perf
@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)
3.6% [0.3%, 15.6%] 5
Regressions ❌
(secondary)
0.2% [0.2%, 0.3%] 3
Improvements ✅
(primary)
-0.3% [-0.3%, -0.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.0% [-0.3%, 15.6%] 6

Max RSS (memory usage)

Results (primary 0.9%, secondary -0.9%)

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

mean range count
Regressions ❌
(primary)
4.1% [3.4%, 4.8%] 2
Regressions ❌
(secondary)
1.2% [0.4%, 2.1%] 2
Improvements ✅
(primary)
-2.4% [-2.4%, -2.3%] 2
Improvements ✅
(secondary)
-2.4% [-3.5%, -0.5%] 3
All ❌✅ (primary) 0.9% [-2.4%, 4.8%] 4

Cycles

Results (primary 14.2%, secondary 1.0%)

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

mean range count
Regressions ❌
(primary)
14.2% [14.2%, 14.2%] 1
Regressions ❌
(secondary)
1.6% [0.4%, 2.9%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.4% [-0.4%, -0.4%] 1
All ❌✅ (primary) 14.2% [14.2%, 14.2%] 1

Binary size

Results (primary 0.1%, 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.1% [0.0%, 0.2%] 23
Regressions ❌
(secondary)
0.0% [0.0%, 0.2%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [0.0%, 0.2%] 23

Bootstrap: 460.27s -> 462.422s (0.47%)
Artifact size: 405.34 MiB -> 405.42 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 20, 2026
Comment thread library/alloc/src/collections/retain.rs Resolved
Comment thread library/alloc/src/collections/retain.rs Resolved
Comment thread library/alloc/src/collections/retain.rs Resolved
@Amanieu

Amanieu commented Nov 23, 2026

Copy link
Copy Markdown
Member

@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 Nov 23, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 23, 2026
Share retain implementation between Vec and VecDeque
@rust-bors

rust-bors Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: ee514f7 (ee514f792f35eb7662146e2d0445f481121192fa)
Base parent: 0259d5e (0259d5e9a81c6af08e40f8c204f2c0c41dca1647)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (ee514f7): comparison URL.

Overall result: ❌ regressions - please read:

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=iffy rustc-perf
@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.3% [0.3%, 0.4%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

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

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

mean range count
Regressions ❌
(primary)
2.9% [2.9%, 2.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.9% [-2.9%, -2.9%] 1
Improvements ✅
(secondary)
-1.6% [-2.8%, -0.4%] 2
All ❌✅ (primary) -0.0% [-2.9%, 2.9%] 2

Cycles

Results (primary -5.6%, secondary 2.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)
2.7% [0.7%, 5.2%] 4
Improvements ✅
(primary)
-5.6% [-5.6%, -5.6%] 1
Improvements ✅
(secondary)
-0.7% [-0.7%, -0.7%] 1
All ❌✅ (primary) -5.6% [-5.6%, -5.6%] 1

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.0% [0.0%, 0.2%] 19
Regressions ❌
(secondary)
0.0% [0.0%, 0.2%] 9
Improvements ✅
(primary)
-0.3% [-0.3%, -0.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [-0.3%, 0.2%] 20

Bootstrap: 460.582s -> 462.679s (0.46%)
Artifact size: 405.69 MiB -> 405.82 MiB (0.03%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 23, 2026
@Amanieu

Amanieu commented Nov 26, 2026

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f72705c has been approved by Amanieu

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 Nov 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 26, 2026
Share retain implementation between Vec and VecDeque

It treats Vec and VecDeque as two seperate regions, allowing them to share the same retain implementation.

When processing the buffer, `retain_raw` splits the logical ring buffer into up to two contiguous regions. Each region is processed independently: elements rejected by f are dropped, while retained elements are moved to fill holes.

Compared to the regional code, due to wrapping, there are some regressions on the small buffers for `Vec`.

Type | Group | Small (4–32 elems) | Medium (64–512 elems) | Large (1k–100k elems)
-- | -- | -- | -- | --
Vec | keep even | +1% to 0% | 0% to -1% | +1% to -1%
Vec | keep all | 0% | 0% to -4% | 0%
Vec | iter + collect reference | 0% to -1% | 0% | 0%
VecDeque | keep all / contiguous | +19% to -30% | -43% to -48% | -49% to -50%
VecDeque | keep all / wrapped | +6% to -23% | -34% to -49% | -49% to -51%
VecDeque | keep even / contiguous | -5% to -26% | -28% to -31% | about -30%
VecDeque | keep even / wrapped | +12% to -15% | -30% to -32% | -33% to -36%
VecDeque | keep second half / contiguous | +4% to -43% | -52% to -56% | -54% to -56%
VecDeque | keep second half / wrapped | +9% to -35% | -49% to -57% | -58% to -59%
VecDeque | random 50% / contiguous | +23% to -14% | -23% to -29% | -27% to -73%
VecDeque | random 50% / wrapped | +6% to -1% | -15% to -30% | -26% to -70%

<details>
<summary>Detailed Vec benchmark results</summary>

### Vec (`vec::bench_retain_*`), ns/iter: before → after (change)

#### Keep even values

|     | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1000 | 10000 | 100000 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| u8 | 6.9 → 6.9 (-0%) | 9.1 → 9.1 (+0%) | 14.9 → 14.9 (-0%) | 25.9 → 25.9 (+0%) | 53.8 → 53.8 (+0%) | 98.7 → 98.7 (+0%) | 187.7 → 187.7 (+0%) | 367.8 → 367.8 (-0%) | 718.2 → 718.1 (-0%) | 7.2k → 7.2k (+0%) | 71.8k → 71.8k (-0%) |
| u16 | 6.2 → 6.2 (+0%) | 9.4 → 9.5 (+1%) | 14.6 → 14.6 (+0%) | 27.1 → 27.1 (+0%) | 54.5 → 54.5 (+0%) | 99.5 → 99.5 (+0%) | 190.5 → 190.5 (+0%) | 373.4 → 373.5 (+0%) | 737.8 → 737.8 (+0%) | 7.4k → 7.4k (+0%) | 74.3k → 74.4k (+0%) |
| u32 | 6.6 → 6.5 (-0%) | 8.4 → 8.4 (+0%) | 15.0 → 15.0 (-0%) | 27.1 → 27.1 (-0%) | 54.5 → 54.5 (-0%) | 102.1 → 102.1 (+0%) | 195.8 → 195.8 (-0%) | 397.0 → 392.2 (-1%) | 780.6 → 788.1 (+1%) | 7.8k → 7.9k (+0%) | 79.1k → 78.3k (-1%) |
| u64 | 5.5 → 5.5 (+0%) | 9.4 → 9.4 (+0%) | 15.8 → 15.8 (+0%) | 28.3 → 28.3 (+0%) | 57.9 → 58.0 (+0%) | 109.5 → 109.5 (+0%) | 228.3 → 228.4 (+0%) | 461.2 → 457.7 (-1%) | 904.1 → 904.2 (+0%) | 9.1k → 9.1k (-0%) | 91.6k → 91.5k (-0%) |

#### Keep all (`whole`)

|     | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1000 | 10000 | 100000 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| u8 | 3.0 → 3.0 (+0%) | 5.1 → 5.1 (+0%) | 9.4 → 9.4 (+0%) | 18.6 → 18.6 (+0%) | 37.0 → 37.0 (+0%) | 74.4 → 74.4 (+0%) | 152.8 → 152.8 (-0%) | 359.9 → 359.9 (-0%) | 697.1 → 697.1 (-0%) | 6.9k → 6.9k (-0%) | 68.8k → 68.7k (-0%) |
| u16 | 3.2 → 3.2 (+0%) | 5.4 → 5.4 (+0%) | 9.9 → 9.9 (+0%) | 19.4 → 19.4 (+0%) | 38.8 → 38.8 (+0%) | 77.3 → 77.6 (+0%) | 155.2 → 154.6 (-0%) | 313.3 → 313.3 (+0%) | 604.5 → 605.5 (+0%) | 6.0k → 6.0k (+0%) | 60.2k → 60.2k (+0%) |
| u32 | 3.0 → 3.0 (+0%) | 5.1 → 5.1 (+0%) | 9.4 → 9.4 (+0%) | 18.6 → 18.6 (+0%) | 37.0 → 37.0 (+0%) | 74.4 → 74.5 (+0%) | 157.7 → 152.8 (-3%) | 359.9 → 359.9 (-0%) | 697.2 → 697.1 (-0%) | 6.9k → 6.9k (-0%) | 69.0k → 69.0k (-0%) |
| u64 | 3.0 → 3.0 (+0%) | 5.1 → 5.1 (+0%) | 9.4 → 9.4 (+0%) | 18.6 → 18.6 (+0%) | 37.0 → 36.9 (-0%) | 74.4 → 74.4 (-0%) | 164.2 → 157.7 (-4%) | 359.9 → 359.9 (-0%) | 698.5 → 697.8 (-0%) | 6.9k → 6.9k (-0%) | 68.9k → 68.8k (-0%) |

#### Reference: `into_iter().filter().collect()`

|     | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1000 | 10000 | 100000 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| u32 | 6.7 → 6.7 (+0%) | 10.9 → 10.9 (+0%) | 17.3 → 17.4 (+0%) | 30.1 → 29.9 (-1%) | 57.6 → 57.6 (+0%)

</details>

<details>
<summary>Detailed VecDeque benchmark results</summary>

### VecDeque (`vec_deque::bench_retain_*`, u32), ns/iter: before → after (change)

| Group | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1000 | 10000 | 100000 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| keep all, contiguous | 8.4 → 10.0 (+19%) | 11.0 → 11.0 (-0%) | 17.8 → 14.8 (-17%) | 30.2 → 21.1 (-30%) | 56.7 → 32.3 (-43%) | 110.7 → 60.5 (-45%) | 215.2 → 113.6 (-47%) | 425.8 → 222.6 (-48%) | 823.6 → 420.1 (-49%) | 8.2k → 4.1k (-50%) | 81.1k → 40.6k (-50%) |
| keep all, wrapped | 11.2 → 11.8 (+6%) | 14.6 → 12.9 (-11%) | 20.6 → 17.4 (-15%) | 33.4 → 25.8 (-23%) | 58.6 → 38.4 (-34%) | 112.8 → 65.1 (-42%) | 221.7 → 115.8 (-48%) | 436.2 → 222.1 (-49%) | 837.8 → 430.1 (-49%) | 8.3k → 4.1k (-51%) | 81.6k → 40.7k (-50%) |
| keep even, contiguous | 10.1 → 9.6 (-5%) | 14.2 → 12.1 (-15%) | 22.5 → 17.9 (-20%) | 39.7 → 29.3 (-26%) | 80.6 → 58.0 (-28%) | 148.0 → 103.5 (-30%) | 283.6 → 197.7 (-30%) | 558.1 → 387.8 (-31%) | 1.1k → 762.5 (-30%) | 10.7k → 7.5k (-30%) | 106.6k → 75.0k (-30%) |
| keep even, wrapped | 13.4 → 14.2 (+6%) | 17.6 → 19.8 (+12%) | 26.6 → 25.9 (-3%) | 45.6 → 38.6 (-15%) | 88.7 → 62.5 (-30%) | 163.2 → 114.9 (-30%) | 316.9 → 221.1 (-30%) | 618.5 → 423.1 (-32%) | 1.2k → 801.9 (-33%) | 11.9k → 7.6k (-36%) | 118.2k → 75.2k (-36%) |
| keep second half, contiguous | 10.4 → 10.8 (+4%) | 14.4 → 12.7 (-12%) | 23.2 → 17.9 (-23%) | 41.9 → 23.8 (-43%) | 91.8 → 40.8 (-56%) | 158.3 → 76.6 (-52%) | 306.6 → 141.7 (-54%) | 610.3 → 285.6 (-53%) | 1.2k → 537.6 (-54%) | 11.6k → 5.1k (-56%) | 115.2k → 50.6k (-56%) |
| keep second half, wrapped | 13.5 → 14.6 (+9%) | 17.6 → 17.1 (-3%) | 26.8 → 22.7 (-15%) | 48.1 → 31.4 (-35%) | 94.5 → 48.3 (-49%) | 172.1 → 79.2 (-54%) | 337.8 → 145.8 (-57%) | 671.6 → 289.3 (-57%) | 1.3k → 538.8 (-58%) | 12.5k → 5.1k (-59%) | 119.9k → 50.6k (-58%) |
| keep random 50%, contiguous | 8.1 → 10.0 (+23%) | 12.7 → 12.0 (-5%) | 20.4 → 18.6 (-9%) | 36.4 → 31.4 (-14%) | 72.1 → 55.6 (-23%) | 142.7 → 103.2 (-28%) | 282.5 → 199.2 (-29%) | 554.9 → 399.3 (-28%) | 1.1k → 776.9 (-27%) | 29.1k → 7.9k (-73%) | 485.5k → 353.4k (-27%) |
| keep random 50%, wrapped | 11.1 → 11.8 (+6%) | 15.8 → 15.7 (-1%) | 23.4 → 24.5 (+5%) | 40.9 → 40.4 (-1%) | 78.7 → 66.6 (-15%) | 155.2 → 115.6 (-25%) | 311.4 → 218.2 (-30%) | 603.4 → 421.3 (-30%) | 1.1k → 789.8 (-31%) | 26.1k → 7.8k (-70%) | 480.3k → 353.1k (-26%) |

</details>
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - #164252 (Share retain implementation between Vec and VecDeque)
 - #166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - #166506 (traits: Fix dead match arm for String type suggestions.)
 - #164542 (Add regression test for #151312)
 - #165651 (compiletest: Further clean up directive parsing)
 - #166196 (autodiff: don't ICE on invalid macro arguments)
 - #166556 (remove `Box::into_boxed_slice`)
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #164252 (Share retain implementation between Vec and VecDeque)
 - #166008 (fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature)
 - #166156 (tests: cover proc-macro metadata path remapping)
 - #166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - #166506 (traits: Fix dead match arm for String type suggestions.)
 - #164542 (Add regression test for #151312)
 - #165651 (compiletest: Further clean up directive parsing)
 - #166196 (autodiff: don't ICE on invalid macro arguments)
 - #166556 (remove `Box::into_boxed_slice`)
 - #166562 (docs: add backticks around `!` in primitive docs )
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #164252 (Share retain implementation between Vec and VecDeque)
 - #166008 (fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature)
 - #166156 (tests: cover proc-macro metadata path remapping)
 - #166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - #166506 (traits: Fix dead match arm for String type suggestions.)
 - #164542 (Add regression test for #151312)
 - #165651 (compiletest: Further clean up directive parsing)
 - #166196 (autodiff: don't ICE on invalid macro arguments)
 - #166556 (remove `Box::into_boxed_slice`)
 - #166562 (docs: add backticks around `!` in primitive docs )
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
Rollup merge of #164252 - ywxt:vec_deque_retain, r=Amanieu

Share retain implementation between Vec and VecDeque

It treats Vec and VecDeque as two seperate regions, allowing them to share the same retain implementation.

When processing the buffer, `retain_raw` splits the logical ring buffer into up to two contiguous regions. Each region is processed independently: elements rejected by f are dropped, while retained elements are moved to fill holes.

Compared to the regional code, due to wrapping, there are some regressions on the small buffers for `Vec`.

Type | Group | Small (4–32 elems) | Medium (64–512 elems) | Large (1k–100k elems)
-- | -- | -- | -- | --
Vec | keep even | +1% to 0% | 0% to -1% | +1% to -1%
Vec | keep all | 0% | 0% to -4% | 0%
Vec | iter + collect reference | 0% to -1% | 0% | 0%
VecDeque | keep all / contiguous | +19% to -30% | -43% to -48% | -49% to -50%
VecDeque | keep all / wrapped | +6% to -23% | -34% to -49% | -49% to -51%
VecDeque | keep even / contiguous | -5% to -26% | -28% to -31% | about -30%
VecDeque | keep even / wrapped | +12% to -15% | -30% to -32% | -33% to -36%
VecDeque | keep second half / contiguous | +4% to -43% | -52% to -56% | -54% to -56%
VecDeque | keep second half / wrapped | +9% to -35% | -49% to -57% | -58% to -59%
VecDeque | random 50% / contiguous | +23% to -14% | -23% to -29% | -27% to -73%
VecDeque | random 50% / wrapped | +6% to -1% | -15% to -30% | -26% to -70%

<details>
<summary>Detailed Vec benchmark results</summary>

### Vec (`vec::bench_retain_*`), ns/iter: before → after (change)

#### Keep even values

|     | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1000 | 10000 | 100000 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| u8 | 6.9 → 6.9 (-0%) | 9.1 → 9.1 (+0%) | 14.9 → 14.9 (-0%) | 25.9 → 25.9 (+0%) | 53.8 → 53.8 (+0%) | 98.7 → 98.7 (+0%) | 187.7 → 187.7 (+0%) | 367.8 → 367.8 (-0%) | 718.2 → 718.1 (-0%) | 7.2k → 7.2k (+0%) | 71.8k → 71.8k (-0%) |
| u16 | 6.2 → 6.2 (+0%) | 9.4 → 9.5 (+1%) | 14.6 → 14.6 (+0%) | 27.1 → 27.1 (+0%) | 54.5 → 54.5 (+0%) | 99.5 → 99.5 (+0%) | 190.5 → 190.5 (+0%) | 373.4 → 373.5 (+0%) | 737.8 → 737.8 (+0%) | 7.4k → 7.4k (+0%) | 74.3k → 74.4k (+0%) |
| u32 | 6.6 → 6.5 (-0%) | 8.4 → 8.4 (+0%) | 15.0 → 15.0 (-0%) | 27.1 → 27.1 (-0%) | 54.5 → 54.5 (-0%) | 102.1 → 102.1 (+0%) | 195.8 → 195.8 (-0%) | 397.0 → 392.2 (-1%) | 780.6 → 788.1 (+1%) | 7.8k → 7.9k (+0%) | 79.1k → 78.3k (-1%) |
| u64 | 5.5 → 5.5 (+0%) | 9.4 → 9.4 (+0%) | 15.8 → 15.8 (+0%) | 28.3 → 28.3 (+0%) | 57.9 → 58.0 (+0%) | 109.5 → 109.5 (+0%) | 228.3 → 228.4 (+0%) | 461.2 → 457.7 (-1%) | 904.1 → 904.2 (+0%) | 9.1k → 9.1k (-0%) | 91.6k → 91.5k (-0%) |

#### Keep all (`whole`)

|     | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1000 | 10000 | 100000 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| u8 | 3.0 → 3.0 (+0%) | 5.1 → 5.1 (+0%) | 9.4 → 9.4 (+0%) | 18.6 → 18.6 (+0%) | 37.0 → 37.0 (+0%) | 74.4 → 74.4 (+0%) | 152.8 → 152.8 (-0%) | 359.9 → 359.9 (-0%) | 697.1 → 697.1 (-0%) | 6.9k → 6.9k (-0%) | 68.8k → 68.7k (-0%) |
| u16 | 3.2 → 3.2 (+0%) | 5.4 → 5.4 (+0%) | 9.9 → 9.9 (+0%) | 19.4 → 19.4 (+0%) | 38.8 → 38.8 (+0%) | 77.3 → 77.6 (+0%) | 155.2 → 154.6 (-0%) | 313.3 → 313.3 (+0%) | 604.5 → 605.5 (+0%) | 6.0k → 6.0k (+0%) | 60.2k → 60.2k (+0%) |
| u32 | 3.0 → 3.0 (+0%) | 5.1 → 5.1 (+0%) | 9.4 → 9.4 (+0%) | 18.6 → 18.6 (+0%) | 37.0 → 37.0 (+0%) | 74.4 → 74.5 (+0%) | 157.7 → 152.8 (-3%) | 359.9 → 359.9 (-0%) | 697.2 → 697.1 (-0%) | 6.9k → 6.9k (-0%) | 69.0k → 69.0k (-0%) |
| u64 | 3.0 → 3.0 (+0%) | 5.1 → 5.1 (+0%) | 9.4 → 9.4 (+0%) | 18.6 → 18.6 (+0%) | 37.0 → 36.9 (-0%) | 74.4 → 74.4 (-0%) | 164.2 → 157.7 (-4%) | 359.9 → 359.9 (-0%) | 698.5 → 697.8 (-0%) | 6.9k → 6.9k (-0%) | 68.9k → 68.8k (-0%) |

#### Reference: `into_iter().filter().collect()`

|     | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1000 | 10000 | 100000 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| u32 | 6.7 → 6.7 (+0%) | 10.9 → 10.9 (+0%) | 17.3 → 17.4 (+0%) | 30.1 → 29.9 (-1%) | 57.6 → 57.6 (+0%)

</details>

<details>
<summary>Detailed VecDeque benchmark results</summary>

### VecDeque (`vec_deque::bench_retain_*`, u32), ns/iter: before → after (change)

| Group | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1000 | 10000 | 100000 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| keep all, contiguous | 8.4 → 10.0 (+19%) | 11.0 → 11.0 (-0%) | 17.8 → 14.8 (-17%) | 30.2 → 21.1 (-30%) | 56.7 → 32.3 (-43%) | 110.7 → 60.5 (-45%) | 215.2 → 113.6 (-47%) | 425.8 → 222.6 (-48%) | 823.6 → 420.1 (-49%) | 8.2k → 4.1k (-50%) | 81.1k → 40.6k (-50%) |
| keep all, wrapped | 11.2 → 11.8 (+6%) | 14.6 → 12.9 (-11%) | 20.6 → 17.4 (-15%) | 33.4 → 25.8 (-23%) | 58.6 → 38.4 (-34%) | 112.8 → 65.1 (-42%) | 221.7 → 115.8 (-48%) | 436.2 → 222.1 (-49%) | 837.8 → 430.1 (-49%) | 8.3k → 4.1k (-51%) | 81.6k → 40.7k (-50%) |
| keep even, contiguous | 10.1 → 9.6 (-5%) | 14.2 → 12.1 (-15%) | 22.5 → 17.9 (-20%) | 39.7 → 29.3 (-26%) | 80.6 → 58.0 (-28%) | 148.0 → 103.5 (-30%) | 283.6 → 197.7 (-30%) | 558.1 → 387.8 (-31%) | 1.1k → 762.5 (-30%) | 10.7k → 7.5k (-30%) | 106.6k → 75.0k (-30%) |
| keep even, wrapped | 13.4 → 14.2 (+6%) | 17.6 → 19.8 (+12%) | 26.6 → 25.9 (-3%) | 45.6 → 38.6 (-15%) | 88.7 → 62.5 (-30%) | 163.2 → 114.9 (-30%) | 316.9 → 221.1 (-30%) | 618.5 → 423.1 (-32%) | 1.2k → 801.9 (-33%) | 11.9k → 7.6k (-36%) | 118.2k → 75.2k (-36%) |
| keep second half, contiguous | 10.4 → 10.8 (+4%) | 14.4 → 12.7 (-12%) | 23.2 → 17.9 (-23%) | 41.9 → 23.8 (-43%) | 91.8 → 40.8 (-56%) | 158.3 → 76.6 (-52%) | 306.6 → 141.7 (-54%) | 610.3 → 285.6 (-53%) | 1.2k → 537.6 (-54%) | 11.6k → 5.1k (-56%) | 115.2k → 50.6k (-56%) |
| keep second half, wrapped | 13.5 → 14.6 (+9%) | 17.6 → 17.1 (-3%) | 26.8 → 22.7 (-15%) | 48.1 → 31.4 (-35%) | 94.5 → 48.3 (-49%) | 172.1 → 79.2 (-54%) | 337.8 → 145.8 (-57%) | 671.6 → 289.3 (-57%) | 1.3k → 538.8 (-58%) | 12.5k → 5.1k (-59%) | 119.9k → 50.6k (-58%) |
| keep random 50%, contiguous | 8.1 → 10.0 (+23%) | 12.7 → 12.0 (-5%) | 20.4 → 18.6 (-9%) | 36.4 → 31.4 (-14%) | 72.1 → 55.6 (-23%) | 142.7 → 103.2 (-28%) | 282.5 → 199.2 (-29%) | 554.9 → 399.3 (-28%) | 1.1k → 776.9 (-27%) | 29.1k → 7.9k (-73%) | 485.5k → 353.4k (-27%) |
| keep random 50%, wrapped | 11.1 → 11.8 (+6%) | 15.8 → 15.7 (-1%) | 23.4 → 24.5 (+5%) | 40.9 → 40.4 (-1%) | 78.7 → 66.6 (-15%) | 155.2 → 115.6 (-25%) | 311.4 → 218.2 (-30%) | 603.4 → 421.3 (-30%) | 1.1k → 789.8 (-31%) | 26.1k → 7.8k (-70%) | 480.3k → 353.1k (-26%) |

</details>
@rust-bors
rust-bors Bot merged commit e231e21 into rust-lang:main Nov 26, 2026
15 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 26, 2026
mehmettoplu-rs pushed a commit to mehmettoplu-rs/miri that referenced this pull request Nov 27, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#164252 (Share retain implementation between Vec and VecDeque)
 - rust-lang/rust#166008 (fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature)
 - rust-lang/rust#166156 (tests: cover proc-macro metadata path remapping)
 - rust-lang/rust#166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - rust-lang/rust#166506 (traits: Fix dead match arm for String type suggestions.)
 - rust-lang/rust#164542 (Add regression test for rust-lang/rust#151312)
 - rust-lang/rust#165651 (compiletest: Further clean up directive parsing)
 - rust-lang/rust#166196 (autodiff: don't ICE on invalid macro arguments)
 - rust-lang/rust#166556 (remove `Box::into_boxed_slice`)
 - rust-lang/rust#166562 (docs: add backticks around `!` in primitive docs )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-collections Area: `std::collections` (prefer A-binaryheap, A-btree, A-hashtable, A-linkedlist, A-vecdeque) A-slice-vec Area: `[T]` and `Vec<T>` A-vecdeque Area: `std::collections::VecDeque` perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants