Skip to content

std: implement clear via truncate#154095

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
joboet:vec_clear_truncate
May 4, 2026
Merged

std: implement clear via truncate#154095
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
joboet:vec_clear_truncate

Conversation

@joboet

@joboet joboet commented Mar 19, 2026

Copy link
Copy Markdown
Member

This gets rid of some unsafe. truncate(0) is even documented to be equivalent to clear, this makes that equivalence even more obvious.

@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 Mar 19, 2026
@rustbot

rustbot commented Mar 19, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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 7 candidates

@xtqqczze

xtqqczze commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

clear was intentionally implemented this way (rather than using truncate(0)) for performance reasons, as introduced in 9c59d04.

cc: @nnethercote

@Mark-Simulacrum

Copy link
Copy Markdown
Member

r? @nnethercote

@nnethercote

Copy link
Copy Markdown
Contributor

I just wrote a microbenchmark and the difference between the two is still measurable. So I'd prefer to keep the code as is. It would be useful to add a comment explaining why truncate(0) is not used, if you are happy to do that? (I should have done that in #96002.)

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 26, 2026
@joboet joboet force-pushed the vec_clear_truncate branch from 9aa5814 to f8b83b2 Compare May 1, 2026 14:22
@rustbot

rustbot commented May 1, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

@@ -2992,6 +2985,10 @@ impl<T, A: Allocator> Vec<T, A> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn clear(&mut self) {
// Though this is equivalent to `truncate(0)`, the manual version
// optimizes better, justifying the additional complexity
// (see #96002 and #154095 for context).

@xtqqczze xtqqczze May 1, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe use full URLs for the GitHub issue links so they are navigable from IDEs?

View changes since the review

@nnethercote

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors

rust-bors Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f8b83b2 has been approved by nnethercote

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 4, 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 May 4, 2026
@rust-bors

rust-bors Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: nnethercote
Duration: 3h 14m 22s
Pushing 783062d to main...

@rust-bors rust-bors Bot merged commit 783062d into rust-lang:main May 4, 2026
12 checks passed
@rustbot rustbot added this to the 1.97.0 milestone May 4, 2026
@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor
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 a51f3a8 (parent) -> 783062d (this PR)

Test differences

Show 156 test diffs

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

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 783062d3c612d223e6841db4bbbc9bac4ab31ff4 --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-various-2: 34m 44s -> 44m 12s (+27.2%)
  2. i686-msvc-2: 1h 44m -> 2h 10m (+24.5%)
  3. dist-loongarch64-linux: 1h 28m -> 1h 49m (+23.1%)
  4. x86_64-gnu: 2h 12m -> 1h 48m (-18.2%)
  5. dist-sparcv9-solaris: 1h 28m -> 1h 14m (-16.0%)
  6. dist-x86_64-apple: 1h 53m -> 2h 10m (+15.1%)
  7. dist-aarch64-linux: 1h 59m -> 1h 45m (-11.7%)
  8. x86_64-msvc-ext2: 2h 5m -> 1h 51m (-11.2%)
  9. x86_64-msvc-ext1: 2h 20m -> 2h 5m (-10.5%)
  10. x86_64-gnu-stable: 2h 10m -> 2h 23m (+10.2%)
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.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (783062d): comparison URL.

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

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.2% [0.0%, 0.6%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.0%] 5
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (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)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.0% [-5.2%, -0.6%] 5
All ❌✅ (primary) - - 0

Cycles

Results (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)
3.8% [1.7%, 6.0%] 11
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.9% [-3.1%, -0.4%] 7
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 501.019s -> 495.891s (-1.02%)
Artifact size: 396.46 MiB -> 394.42 MiB (-0.51%)

@rustbot rustbot added the perf-regression Performance regression. label May 4, 2026
@panstromek

Copy link
Copy Markdown
Contributor

perf triage:

These results are noise, this PR only adds comment.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label May 5, 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-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.

7 participants