Skip to content

library: optimize truncate method#157123

Open
xtqqczze wants to merge 1 commit into
rust-lang:mainfrom
xtqqczze:truncate
Open

library: optimize truncate method#157123
xtqqczze wants to merge 1 commit into
rust-lang:mainfrom
xtqqczze:truncate

Conversation

@xtqqczze
Copy link
Copy Markdown
Contributor

@xtqqczze xtqqczze commented May 29, 2026

Closes #76089

Slightly better code when the elements have drop glue, see #74172 (comment).

Slightly worse code for truncate(0), see #78884 (comment). However, this is mitigated by the new clippy::manual_clear lint which is warn-by-default.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 29, 2026
@xtqqczze xtqqczze marked this pull request as ready for review May 29, 2026 21:43
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 29, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 29, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 29, 2026

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
  • Random selection from Mark-Simulacrum, joboet

@ZuseZ4
Copy link
Copy Markdown
Member

ZuseZ4 commented May 29, 2026

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 29, 2026
rust-bors Bot pushed a commit that referenced this pull request May 29, 2026
library: optimize truncate method
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 29, 2026

☀️ Try build successful (CI)
Build commit: 0b2c3b8 (0b2c3b8072bcdf1845525eef84dd5e719d76118f, parent: 6368fd52cb9f230dfb156097625993e7a8891800)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (0b2c3b8): comparison URL.

Overall result: no relevant changes - no action needed

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

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

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

Max RSS (memory usage)

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

Cycles

Results (primary 2.3%, secondary 2.4%)

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

mean range count
Regressions ❌
(primary)
2.3% [2.3%, 2.3%] 1
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.3% [2.3%, 2.3%] 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
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.0% [-0.0%, -0.0%] 4
Improvements ✅
(secondary)
-0.0% [-0.1%, -0.0%] 14
All ❌✅ (primary) -0.0% [-0.0%, -0.0%] 4

Bootstrap: 511.654s -> 511.381s (-0.05%)
Artifact size: 400.70 MiB -> 400.67 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 30, 2026
@mati865
Copy link
Copy Markdown
Member

mati865 commented May 30, 2026

The same, slightly improved assembly is generated if you remove or equal from the original condition. I.e. turnif new_len <= self.len() { into if new_len < self.len() { which is equiva to your change.

@xtqqczze
Copy link
Copy Markdown
Contributor Author

The same, slightly improved assembly is generated if you remove or equal from the original condition. I.e. turnif new_len <= self.len() { into if new_len < self.len() { which is equiva to your change.

You’re correct that changing <= to < in the original condition yields the same improvement. I opted for the early-return style instead, for consistency and to better align with the intent expressed in the comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible Vec::truncate improvement

6 participants