Skip to content

Alloc String::retain optimization - #150067

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
fereidani:string_retain
Aug 26, 2026
Merged

rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
fereidani:string_retain

Conversation

@fereidani

@fereidani fereidani commented Dec 16, 2025 •

Copy link
Copy Markdown
Contributor

View all comments

Hi again!
This uses the exact same algorithm as my other PR: #149784

Technically it should improve performance for String::retain too, But let's see what bors thinks.

@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 Dec 16, 2025
@rustbot

rustbot commented Dec 16, 2025

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

@ZuseZ4

ZuseZ4 commented Dec 16, 2025

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors Bot added a commit that referenced this pull request Dec 16, 2025
Alloc `String::retain` optimization
@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 Dec 16, 2025
@rust-bors

rust-bors Bot commented Dec 16, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 2621d01 (2621d0177c40d6c9165ad462e5826558b57ea69a, parent: 31010ca61c3ff019e1480dda0a7ef16bd2bd51c0)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (2621d01): comparison URL.

Overall result: no relevant changes - no action needed

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.

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

Instruction count

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

Max RSS (memory usage)

Results (primary -2.1%, 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
Regressions ❌
(secondary)
3.7% [3.7%, 3.7%] 1
Improvements ✅
(primary)
-2.1% [-2.1%, -2.1%] 1
Improvements ✅
(secondary)
-2.8% [-2.8%, -2.8%] 1
All ❌✅ (primary) -2.1% [-2.1%, -2.1%] 1

Cycles

Results (primary 68.5%, 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)
68.5% [68.5%, 68.5%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.4% [-2.4%, -2.4%] 1
All ❌✅ (primary) 68.5% [68.5%, 68.5%] 1

Binary size

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

Bootstrap: 479.038s -> 478.434s (-0.13%)
Artifact size: 390.32 MiB -> 390.32 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 16, 2025
@Mark-Simulacrum

Copy link
Copy Markdown
Member

r? joboet

Probably makes sense to have the same reviewer for both.

@rustbot rustbot assigned joboet and unassigned Mark-Simulacrum Dec 28, 2025
@rustbot

rustbot commented Dec 28, 2025

Copy link
Copy Markdown
Collaborator

joboet is not on the review rotation at the moment.
They may take a while to respond.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 3, 2026
@rustbot

rustbot commented Jan 4, 2026

Copy link
Copy Markdown
Collaborator

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.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 4, 2026

@joboet joboet left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this could be even faster by avoiding some UTF-8 reencoding...

View changes since this review

Comment thread library/alloc/src/string.rs Resolved
Comment thread library/alloc/src/string.rs Resolved
Comment thread library/alloc/src/string.rs Resolved
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 5, 2026
@joboet

joboet commented Aug 26, 2026

Copy link
Copy Markdown
Member

Thank you, looks great!
@bors r+

@rust-bors

rust-bors Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 6a24a6e has been approved by joboet

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 Aug 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Aug 26, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - #150067 (Alloc `String::retain` optimization)
 - #153973 (std::process: fix UEFI ExitStatus::code() silent truncation of error …)
 - #161690 (Deny #[inline] on EII declarations)
 - #161701 (Rename dlltool helper function)
rust-bors Bot pushed a commit that referenced this pull request Aug 26, 2026
Rollup merge of #150067 - fereidani:string_retain, r=joboet

Alloc `String::retain` optimization

Hi again!
This uses the exact same algorithm as my other PR: #149784

Technically it should improve performance for `String::retain` too, But let's see what bors thinks.
@rust-bors
rust-bors Bot merged commit 35df304 into rust-lang:main Aug 26, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Aug 26, 2026
pull Bot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Aug 27, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - rust-lang/rust#150067 (Alloc `String::retain` optimization)
 - rust-lang/rust#153973 (std::process: fix UEFI ExitStatus::code() silent truncation of error …)
 - rust-lang/rust#161690 (Deny #[inline] on EII declarations)
 - rust-lang/rust#161701 (Rename dlltool helper function)
@clarfonthey clarfonthey added the A-str Area: `str` and `String` label Nov 3, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 8, 2026
… r=Mark-Simulacrum

String::retain: convert only the untouched UTF-8 tail

During compaction, `String::retain` reads the next character with `g.s.get_unchecked(read..len)`. This invokes `String`’s `Deref`, constructing a `&str` over the entire `0..len` buffer before selecting the unread tail.

For example, removing `'é'` from `"éab"` and moving `'a'` left changes the buffer from `C3 A9 61 62` to `61 A9 61 62`. The next iteration reads the valid `'b'` tail, but first converts the entire buffer, including the orphaned `A9` continuation byte, to `&str`.

Constructing a non-UTF-8 `&str` is not immediate language-level UB under the [Reference’s validity requirements](https://doc.rust-lang.org/reference/behavior-considered-undefined.html). However, this conversion violates [`from_utf8_unchecked`’s documented safety precondition](https://doc.rust-lang.org/std/str/fn.from_utf8_unchecked.html#safety). The libs discussion in rust-lang#134598 is discussing which operations accept invalid UTF-8; `str::get_unchecked` is absent from the tentative list.

The fix slices `read..len` directly from the byte vector and converts only that untouched tail to `&str`. The original string was valid UTF-8, and `read` remains on a character boundary. rust-lang#154583 uses the same approach for `extract_if`.

Related: rust-lang#150067, rust-lang#134598, rust-lang#154583.
rust-bors Bot pushed a commit that referenced this pull request Nov 8, 2026
Rollup merge of #163624 - Talha-Dmr:string-retain-tail-utf8, r=Mark-Simulacrum

String::retain: convert only the untouched UTF-8 tail

During compaction, `String::retain` reads the next character with `g.s.get_unchecked(read..len)`. This invokes `String`’s `Deref`, constructing a `&str` over the entire `0..len` buffer before selecting the unread tail.

For example, removing `'é'` from `"éab"` and moving `'a'` left changes the buffer from `C3 A9 61 62` to `61 A9 61 62`. The next iteration reads the valid `'b'` tail, but first converts the entire buffer, including the orphaned `A9` continuation byte, to `&str`.

Constructing a non-UTF-8 `&str` is not immediate language-level UB under the [Reference’s validity requirements](https://doc.rust-lang.org/reference/behavior-considered-undefined.html). However, this conversion violates [`from_utf8_unchecked`’s documented safety precondition](https://doc.rust-lang.org/std/str/fn.from_utf8_unchecked.html#safety). The libs discussion in #134598 is discussing which operations accept invalid UTF-8; `str::get_unchecked` is absent from the tentative list.

The fix slices `read..len` directly from the byte vector and converts only that untouched tail to `&str`. The original string was valid UTF-8, and `read` remains on a character boundary. #154583 uses the same approach for `extract_if`.

Related: #150067, #134598, #154583.
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Nov 15, 2026
Reverts PR rust-lang#150067

This reverts commit 35df304, reversing
changes made to e081d66.
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 16, 2026
…-retain, r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in rust-lang#150067 by also rolling back rust-lang#163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in rust-lang#165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 16, 2026
…-retain, r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in rust-lang#150067 by also rolling back rust-lang#163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in rust-lang#165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 16, 2026
…-retain, r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in rust-lang#150067 by also rolling back rust-lang#163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in rust-lang#165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 16, 2026
…-retain, r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in rust-lang#150067 by also rolling back rust-lang#163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in rust-lang#165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 16, 2026
…-retain, r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in rust-lang#150067 by also rolling back rust-lang#163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in rust-lang#165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 16, 2026
…-retain, r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in rust-lang#150067 by also rolling back rust-lang#163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in rust-lang#165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 16, 2026
…-retain, r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in rust-lang#150067 by also rolling back rust-lang#163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in rust-lang#165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 16, 2026
…-retain, r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in rust-lang#150067 by also rolling back rust-lang#163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in rust-lang#165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
rust-bors Bot pushed a commit that referenced this pull request Nov 16, 2026
Rollup merge of #166011 - workingjubilee:clean-revert-string-retain, r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in #150067 by also rolling back #163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in #165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
sushink70 pushed a commit to sushink70/miri that referenced this pull request Nov 17, 2026
…r=hanna-kruppe

Clean revert of recent `String::retain` changes

Cleanly revert `String::retain` changes introduced in rust-lang/rust#150067 by also rolling back rust-lang/rust#163624's safety hygiene changes in order to address a performance regression in this function that was reported to us in rust-lang/rust#165988.

While we desire the safety-improving code, that code does not affect the risk of "actual" undefined behavior in the library code. It merely reduces the amount of implementation details we internally rely on and thus whether that code is resilient to changes around it.

r? @hanna-kruppe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-str Area: `str` and `String` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. S-waiting-on-perf Status: Waiting on a perf run to be completed. 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.

10 participants