Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

speed up String::push and String::insert #124810

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

lincot
Copy link

@lincot lincot commented May 6, 2024

Addresses the concerns described in #116235.

The performance gain comes mainly from avoiding temporary buffers.

Complex pattern matching in encode_utf8 (introduced in #67569) has been simplified to a comparison and an exhaustive match in the encode_utf8_raw_unchecked helper function. It takes a slice of MaybeUninit<u8> because otherwise we'd have to construct a normal slice to uninitialized data, which is not desirable, I guess.

Several functions still have that unneeded zeroing, but a single instruction is not that important, I guess.

@rustbot label T-libs C-optimization A-str

@rustbot
Copy link
Collaborator

rustbot commented May 6, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @scottmcm (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@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. A-str Area: str and String C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such labels May 6, 2024
#[unstable(feature = "char_internals", reason = "exposed only for libstd", issue = "none")]
#[doc(hidden)]
#[inline]
pub unsafe fn encode_utf8_raw_unchecked(code: u32, dst: &mut [MaybeUninit<u8>]) -> &mut [u8] {
Copy link
Member

Choose a reason for hiding this comment

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

Pondering: How useful is it to be dealing in slices for this? Could this return, say, (usize, [u8; 4]) and thus not ever need to worry about the indirections? That would presumably resolve the zeroing issue, since it'd just be shifting together a 32-bit number (since [u8; 4] is passes as i32 in our LLVM ABI).

Copy link
Author

Choose a reason for hiding this comment

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

Currently, some functions use the encode_utf8 API, which requires a slice. Using a [u8; 4] makes it act as a number indeed, but it still needs to be xored with itself and is later moved to a buffer for bcmp in this case.

_ => self.vec.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes()),
let len = self.len();
let ch_len = ch.len_utf8();
self.reserve(ch_len);
Copy link
Member

Choose a reason for hiding this comment

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

Related to the previous, I wonder about making this .reserve(4), and just always copying the 4 bytes into the buffer, with only the set_len needing to use the actual length, so that it's always just one store rather than needing a variable number of stores depending on the data width.

Copy link
Author

@lincot lincot May 13, 2024

Choose a reason for hiding this comment

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

Reserving 4 bytes and doing a single store makes little difference other than getting rid of the unsafe, but reserving 4 bytes and doing the same writes makes the non-reallocating path 20% instructions shorter. However, it may cause the string to take up extra space: say, an ASCII char is pushed to a 63-byte string, making it allocate 128 bytes. Is this acceptable?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Worth noting here that this effectively just means that you'll have up to three extra bytes reserved always, since reserve takes into account existing capacity. It does slow down the case of, say, adding a newline to an existing string, but it would speed up repeated insertions, which are probably the bigger performance hit.

Copy link
Member

@scottmcm scottmcm left a comment

Choose a reason for hiding this comment

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

I had a variety of thoughts; let me know what you think.

Also, is there anything here for which it would make sense to have a codegen test to confirm what's happening? Or some other test to help confirm it's better?

@rustbot rustbot 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 May 13, 2024
@lincot
Copy link
Author

lincot commented May 13, 2024

A codegen check for the absence of memcpy would be nice, since the original String::push has one.

@rust-timer
Copy link
Collaborator

Insufficient permissions to issue commands to rust-timer.

@bors
Copy link
Contributor

bors commented Jun 12, 2024

@lincot: 🔑 Insufficient privileges: not in try users

@rust-timer
Copy link
Collaborator

Insufficient permissions to issue commands to rust-timer.

@bors
Copy link
Contributor

bors commented Jun 22, 2024

☔ The latest upstream changes (presumably #116113) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Jul 10, 2024
@rustbot
Copy link
Collaborator

rustbot commented Jul 10, 2024

There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

You can start a rebase with the following commands:

$ # rebase
$ git pull --rebase https://github.com/rust-lang/rust.git master
$ git push --force-with-lease

The following commits are merge commits:

@lincot lincot force-pushed the speed-up-string-push-and-string-insert branch from 9511918 to 89fa55e Compare July 10, 2024 19:08
@rustbot rustbot removed the has-merge-commits PR has merge commits, merge with caution. label Jul 10, 2024
@bors
Copy link
Contributor

bors commented Jul 17, 2024

☔ The latest upstream changes (presumably #127840) made this pull request unmergeable. Please resolve the merge conflicts.

@lincot lincot force-pushed the speed-up-string-push-and-string-insert branch from 89fa55e to 2cb20b3 Compare August 6, 2024 19:00
@Dylan-DPC Dylan-DPC added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 15, 2024
@Dylan-DPC Dylan-DPC added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 15, 2024
@bors
Copy link
Contributor

bors commented Sep 19, 2024

☔ The latest upstream changes (presumably #130511) made this pull request unmergeable. Please resolve the merge conflicts.

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 C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants