Skip to content

Conversation

@lolbinarycat
Copy link
Contributor

@lolbinarycat lolbinarycat commented Dec 5, 2025

Here's a small optimization via specialization that can potentially eliminate a fair few reallocations when building strings using specific patterns, unsure if this justifies the use of unsafe, but I had the code implemented from #148604, so I thought it was worth submitting to see.

@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 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 5, 2025

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

@Kobzol
Copy link
Member

Kobzol commented Dec 5, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Dec 5, 2025
…r=<try>

alloc: specialize String::extend for slices of str
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 5, 2025

#[inline]
fn push_str_slice(&mut self, slice: &[&str]) {
let additional = slice.iter().map(|x| x.len()).sum();
Copy link
Contributor

@hanna-kruppe hanna-kruppe Dec 5, 2025

Choose a reason for hiding this comment

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

This needs to check for overflow, otherwise it's unsound to do unchecked ptr.adds and copy_nonoverlappings below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I forgot about the case where multiple strs reuse the same memory by having their addresses overlap, and was operating under the assumption all arguments were disjoint, in which case overflow would be impossible.

What do you recommend doing in the overflow case? just panicing with a generic message? triggering the oom handler to preserve behavior (as that's what the generic implementation would do in the overflow case)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just saturate and let reserve panic from capacity overflow, just like the previous push_str calls would have? I don't know off-hand if there's any significant benefit to actual overflow check vs. saturation in this context.

@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Dec 5, 2025

☀️ Try build successful (CI)
Build commit: 281de2c (281de2c09f393bab648058208a34357b68b660d1, parent: 66428d92bec337ed4785d695d0127276a482278c)

@rust-timer

This comment has been minimized.

@lolbinarycat lolbinarycat force-pushed the alloc-extend-slice-of-str-spec branch 2 times, most recently from d2b3962 to 38ab135 Compare December 5, 2025 23:21
@lolbinarycat
Copy link
Contributor Author

Fixed CI failure and used saturating arithmetic

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (281de2c): 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 (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)
- - 0
Regressions ❌
(secondary)
1.7% [0.9%, 2.5%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.7% [-6.2%, -1.2%] 2
All ❌✅ (primary) - - 0

Cycles

Results (primary -6.4%, secondary 0.1%)

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.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
-6.4% [-6.4%, -6.4%] 1
Improvements ✅
(secondary)
-2.2% [-2.2%, -2.2%] 1
All ❌✅ (primary) -6.4% [-6.4%, -6.4%] 1

Binary size

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

Bootstrap: 470.249s -> 470.338s (0.02%)
Artifact size: 386.85 MiB -> 386.82 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 6, 2025
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.

7 participants