-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Conversation
There was a problem hiding this 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.
library/alloc/src/string.rs
Outdated
/// 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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)? |
@rustbot ready |
There was a problem hiding this 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
library/alloc/src/string.rs
Outdated
/// 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joboet: ^^^
aa594fe
to
cea703d
Compare
This comment has been minimized.
This comment has been minimized.
cea703d
to
1172074
Compare
There was a problem hiding this 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.
library/alloc/src/string.rs
Outdated
/// buffer. | ||
/// Copies all bytes after the removed char to new positions. | ||
/// | ||
/// NB Calling this in a loop can result in quadratic behavior. |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure
library/alloc/src/string.rs
Outdated
/// Makes space for the insertion, | ||
/// by copying all bytes of `&self[idx..]` to new positions. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed
1172074
to
9d98be8
Compare
This comment has been minimized.
This comment has been minimized.
I made the few improvements you requested, and removed an unnecessary line break. |
There was a problem hiding this 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
library/alloc/src/string.rs
Outdated
/// 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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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!
9d98be8
to
0348a4a
Compare
|
Thank you! @bors r+ rollup |
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
Make performance description of String::{insert,insert_str,remove} more precise
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
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
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
Rollup merge of #138538 - hkBst:patch-4, r=tgross35 Make performance description of String::{insert,insert_str,remove} more precise
Fixes #46650