Alloc String::retain optimization - #150067
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use |
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Alloc `String::retain` optimization
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (2621d01): comparison URL. Overall result: no relevant changes - no action neededBenchmarking 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 Instruction countThis 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.
CyclesResults (primary 68.5%, secondary -2.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary 0.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 479.038s -> 478.434s (-0.13%) |
|
r? joboet Probably makes sense to have the same reviewer for both. |
|
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
bc23951 to
875c63d
Compare
|
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. |
66f0f66 to
2af59e0
Compare
|
Reminder, once the PR becomes ready for a review, use |
|
@bors try @rust-timer queue |
|
Awaiting bors try build completion. @rustbot label: +S-waiting-on-perf |
|
⌛ Trying commit 590ba93 with merge 886f85e… To cancel the try build, run the command Workflow: https://github.com/rust-lang/rust/actions/runs/20782421026 |
Alloc `String::retain` optimization
|
@bors try cancel |
|
Try build cancelled. Cancelled workflows: |
|
@fereidani Hi, ping from triage team. This PR has been inactive for a while. There are still some review comments that need to be addressed. Would you like to proceed with this PR? Thanks. |
|
@SpriteOvO @joboet Please excuse the delay. My country is currently at war, and our government has cut off internet access for our people for over two months now. Now that I have a somewhat workable connection, I’ll try to complete the PR within the next few days. Thank you for your understanding. |
|
@fereidani Oh, so sorry to hear that. No need to hurry at all here. Feel free to come back to this when you have time, staying safe first. |
|
|
Please excuse my delay. Let me know when to clean up the tree and make it a single commit. |
This comment has been minimized.
This comment has been minimized.
| let ch = unsafe { g.s.get_unchecked(read..len).chars().next().unwrap_unchecked() }; | ||
| let ch_len = ch.len_utf8(); |
There was a problem hiding this comment.
Calculating the length using char_indices tends to optimise a bit better. Try doing
| let ch = unsafe { g.s.get_unchecked(read..len).chars().next().unwrap_unchecked() }; | |
| let ch_len = ch.len_utf8(); | |
| let mut iter = unsafe { g.s.get_unchecked(read..len).char_indices() }; | |
| let ch = unsafe { iter.next().unwrap_unchecked() }; | |
| let ch_len = iter.offset(); |
|
Thank you my friend, I'm happy to be back, It's a privilege for me to have you as my reviewer. // SAFETY: `read..read + ch_len` is in bounds and `g.write < read`.
//
// The match makes every copy length constant so LLVM expands
// it inline; a dynamic-length `ptr::copy` lowers to a
// `memmove` call per retained character.
unsafe {
let src = ptr.add(read);
let dst = ptr.add(g.write);
match ch_len {
1 => ptr::copy(src, dst, 1),
2 => ptr::copy(src, dst, 2),
3 => ptr::copy(src, dst, 3),
_ => ptr::copy(src, dst, 4),
}
}It optimizes better and it is about 3.5x faster when match is optimizable. |
View all comments
Hi again!
This uses the exact same algorithm as my other PR: #149784
Technically it should improve performance for
String::retaintoo, But let's see what bors thinks.