Skip to content

Make performance description of String::{insert,insert_str,remove} more precise #138538

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

Merged
merged 1 commit into from
Jun 17, 2025

Conversation

hkBst
Copy link
Member

@hkBst hkBst commented Mar 15, 2025

Fixes #46650

@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2025

r? @joboet

rustbot has assigned @joboet.
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

@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 Mar 15, 2025
@hkBst hkBst changed the title Make performance of String::insert_str more precise Make performance description of String::insert_str more precise Mar 17, 2025
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

I don't really see why anyone would need such precise numbers, it's not like you could correlate that with an exact cycle number or something.

Still, the old note is not correct. I recommend you adapt the old note to say something about the tail of the string needing to be shifted – that is probably the most helpful information to anyone investigating a performance issue in their code.

/// This is an *O*(*n*) operation as it requires copying every element in the
/// buffer.
/// If there is space in `self` this will copy `self.len() - idx + string.len()` bytes,
/// otherwise will reallocate and copy `self.len() + string.len()` bytes.
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect – the reallocation case potentially copies self.capacity() bytes and then additionally self.len() - idx + string.len() bytes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I was a bit fuzzy about how the reallocation worked. Thanks for clearing that up! :D

Copy link
Member Author

Choose a reason for hiding this comment

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

I've reformulated this. Let me know what you think of it.

@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 Mar 17, 2025
@the8472
Copy link
Member

the8472 commented Mar 17, 2025

Being too specific also has the downside that it's essentially specifying the implementation, thus constraining future implementation changes. What is documented generally is a guarantee.

@hkBst
Copy link
Member Author

hkBst commented Apr 4, 2025

Being too specific also has the downside that it's essentially specifying the implementation, thus constraining future implementation changes. What is documented generally is a guarantee.

This is true. Would you prefer only documenting linear complexity in the total number of bytes (both the receiver and the insertion)?

@hkBst
Copy link
Member Author

hkBst commented May 6, 2025

@rustbot ready

@rustbot rustbot 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. labels May 6, 2025
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

r=me with the commas removed

/// Reallocates if `self.capacity()` is insufficient,
/// (which may involve copying all `self.capacity()` bytes).
/// Makes space at the given position for the insertion,
/// by moving the bytes, from the given position until the end, to new positions.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// by moving the bytes, from the given position until the end, to new positions.
/// by moving the bytes from the given position until the end to new positions.

Copy link
Member Author

Choose a reason for hiding this comment

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

perhaps we can keep the last comma and remove only the first?

Copy link
Member Author

Choose a reason for hiding this comment

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

I rephrased this, so perhaps you'd like to take another look.

Copy link
Member Author

Choose a reason for hiding this comment

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

@joboet: ^^^

@rustbot

This comment has been minimized.

@hkBst hkBst changed the title Make performance description of String::insert_str more precise Make performance description of String::{insert,insert_str,remove} more precise May 21, 2025
@hkBst hkBst requested a review from joboet May 21, 2025 08:56
Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Couple of small requests then I'd be happy to merge this. Please also rewrap doc comments to 100 chars.

/// buffer.
/// Copies all bytes after the removed char to new positions.
///
/// NB Calling this in a loop can result in quadratic behavior.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you replace "NB" with "Note that"? We've had some people be confused with "NB" before, and it reads a bit unusual in prose ("N.B." may be better, but not much reason not to just spell it out)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure

Comment on lines 1686 to 1687
/// Makes space for the insertion,
/// by copying all bytes of `&self[idx..]` to new positions.
Copy link
Contributor

Choose a reason for hiding this comment

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

A comma isn't needed before "by" - it's not a comma splice per se, but it's a short sentence and not really someplace you would pause speaking it out loud.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed

@rustbot

This comment has been minimized.

@hkBst
Copy link
Member Author

hkBst commented Jun 16, 2025

Couple of small requests then I'd be happy to merge this. Please also rewrap doc comments to 100 chars.

I made the few improvements you requested, and removed an unnecessary line break.

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

One small nit I missed then r=me

Comment on lines 1684 to 1686
/// Reallocates if `self.capacity()` is insufficient,
/// which may involve copying all `self.capacity()` bytes.
/// Makes space for the insertion by copying all bytes of `&self[idx..]` to new positions.
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, one other thing - could you rewrap docs to 100 chars?

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, I've manually rewrapped this, but I'm not a fan.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have a tool that produces this unusual wrapping?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, looking closer I think this looks like these might be semantic line breaks - we've ~loosely talked about this before in a handful of scattered places. The general impression I've gotten is that SLBs make reading diffs slightly easier but make the resulting source harder/unnatural to read; since our source is read all the time, we prefer to cater to that and just wrap at 100. Also it's somewhat infrequent that a single clause is updated, usually changes are more substantial.

Hopefully one day rustfmt will just take care of this without intervention...

Copy link
Member Author

Choose a reason for hiding this comment

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

All good points, though I'm not sure I agree about which way is more readable.

Hopefully one day rustfmt will just take care of this without intervention...

That would be good.

Thanks for reviewing!

@rustbot
Copy link
Collaborator

rustbot commented Jun 17, 2025

⚠️ Warning ⚠️

@tgross35
Copy link
Contributor

Thank you!

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 17, 2025

📌 Commit 0348a4a has been approved by tgross35

It is now in the queue for this repository.

@bors bors 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 Jun 17, 2025
rust-bors bot added a commit that referenced this pull request Jun 17, 2025
Rollup of 13 pull requests

Successful merges:

 - #138538 (Make performance description of String::{insert,insert_str,remove} more precise)
 - #141946 (std: refactor explanation of `NonNull`)
 - #142216 (Miscellaneous RefCell cleanups)
 - #142542 (Manually invalidate caches in SimplifyCfg.)
 - #142563 (Refine run-make test ignores due to unpredictable `i686-pc-windows-gnu` unwind mechanism)
 - #142570 (Reject union default field values)
 - #142584 (Handle same-crate macro for borrowck semicolon suggestion)
 - #142585 (Update books)
 - #142586 (Fold unnecessary `visit_struct_field_def` in AstValidator)
 - #142587 (Make sure to propagate result from `visit_expr_fields`)
 - #142595 (Revert overeager warning for misuse of `--print native-static-libs`)
 - #142598 (Set elf e_flags on ppc64 targets according to abi)
 - #142601 (Add a comment to `FORMAT_VERSION`.)

r? `@ghost`
`@rustbot` modify labels: rollup
<!-- homu-ignore:start -->
[Create a similar rollup](https://bors.rust-lang.org/queue/rust?prs=138538,141946,142216,142542,142563,142570,142584,142585,142586,142587,142595,142598,142601)
<!-- homu-ignore:end -->
try-job: dist-apple-various
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 17, 2025
Make performance description of String::{insert,insert_str,remove} more precise
bors added a commit that referenced this pull request Jun 17, 2025
Rollup of 10 pull requests

Successful merges:

 - #138538 (Make performance description of String::{insert,insert_str,remove} more precise)
 - #141946 (std: refactor explanation of `NonNull`)
 - #142216 (Miscellaneous RefCell cleanups)
 - #142371 (avoid `&mut P<T>` in `visit_expr` etc methods)
 - #142377 (Try unremapping compiler sources)
 - #142517 (Windows: Use anonymous pipes in Command)
 - #142542 (Manually invalidate caches in SimplifyCfg.)
 - #142563 (Refine run-make test ignores due to unpredictable `i686-pc-windows-gnu` unwind mechanism)
 - #142570 (Reject union default field values)
 - #142584 (Handle same-crate macro for borrowck semicolon suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
rust-bors bot added a commit that referenced this pull request Jun 17, 2025
Rollup of 10 pull requests

Successful merges:

 - #138538 (Make performance description of String::{insert,insert_str,remove} more precise)
 - #141946 (std: refactor explanation of `NonNull`)
 - #142216 (Miscellaneous RefCell cleanups)
 - #142371 (avoid `&mut P<T>` in `visit_expr` etc methods)
 - #142377 (Try unremapping compiler sources)
 - #142517 (Windows: Use anonymous pipes in Command)
 - #142542 (Manually invalidate caches in SimplifyCfg.)
 - #142563 (Refine run-make test ignores due to unpredictable `i686-pc-windows-gnu` unwind mechanism)
 - #142570 (Reject union default field values)
 - #142584 (Handle same-crate macro for borrowck semicolon suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
<!-- homu-ignore:start -->
[Create a similar rollup](https://bors.rust-lang.org/queue/rust?prs=138538,141946,142216,142371,142377,142517,142542,142563,142570,142584)
<!-- homu-ignore:end -->
try-job: dist-aarch64-apple
bors added a commit that referenced this pull request Jun 17, 2025
Rollup of 13 pull requests

Successful merges:

 - #138538 (Make performance description of String::{insert,insert_str,remove} more precise)
 - #141946 (std: refactor explanation of `NonNull`)
 - #142216 (Miscellaneous RefCell cleanups)
 - #142542 (Manually invalidate caches in SimplifyCfg.)
 - #142563 (Refine run-make test ignores due to unpredictable `i686-pc-windows-gnu` unwind mechanism)
 - #142570 (Reject union default field values)
 - #142584 (Handle same-crate macro for borrowck semicolon suggestion)
 - #142585 (Update books)
 - #142586 (Fold unnecessary `visit_struct_field_def` in AstValidator)
 - #142587 (Make sure to propagate result from `visit_expr_fields`)
 - #142595 (Revert overeager warning for misuse of `--print native-static-libs`)
 - #142598 (Set elf e_flags on ppc64 targets according to abi)
 - #142601 (Add a comment to `FORMAT_VERSION`.)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 346adce into rust-lang:master Jun 17, 2025
10 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 17, 2025
rust-timer added a commit that referenced this pull request Jun 17, 2025
Rollup merge of #138538 - hkBst:patch-4, r=tgross35

Make performance description of String::{insert,insert_str,remove} more precise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

Avoid O-notation for String operations
6 participants