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

Avoid no-op when truncating a vector to same size. #78884

Closed
wants to merge 1 commit into from
Closed

Avoid no-op when truncating a vector to same size. #78884

wants to merge 1 commit into from

Conversation

feralfluid
Copy link

I noticed that Vec::truncate() will actually execute unnecessary operations if the target len is the same as the current len, even though it should do nothing. The method already checks whether the target len is greater than the current len, so I simply changed this check to a >= and updated the wording of the associated doc comment to reflect this.

While the lack of the = incurs a nearly negligible amount of unnecessary overhead, this does happen when clear()ing an empty vector or string, which happens fairly commonly.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cramertj (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 8, 2020
@scottmcm
Copy link
Member

scottmcm commented Nov 8, 2020

Good catch! I was worried it'd be an additional check, but 👍 for tweaking the existing one.

r? @scottmcm
@bors r+ rollup

@bors
Copy link
Contributor

bors commented Nov 8, 2020

📌 Commit 181d811 has been approved by scottmcm

@rust-highfive rust-highfive assigned scottmcm and unassigned cramertj Nov 8, 2020
@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 Nov 8, 2020
@scottmcm
Copy link
Member

scottmcm commented Nov 8, 2020

@bors r-

Oops, didn't notice that CI is failing.

It looks like this is adding a new branch in https://github.com/rust-lang/rust/blob/master/src/test/codegen/vec-clear.rs -- probably it was previously inlining the .truncate(0) and 0 > self.len used to be impossible. You'll need to take a look at the history of that test and see what it's trying to ensure.

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 8, 2020
@feralfluid
Copy link
Author

@bors r-

Oops, didn't notice that CI is failing.

It looks like this is adding a new branch in https://github.com/rust-lang/rust/blob/master/src/test/codegen/vec-clear.rs -- probably it was previously inlining the .truncate(0) and 0 > self.len used to be impossible. You'll need to take a look at the history of that test and see what it's trying to ensure.

The linked test(?) doesn't look like it has any kind of assertion and I don't know what //CHECK-NOT load means, I'm completely lost; I have no idea how to fix this.

@scottmcm
Copy link
Member

scottmcm commented Nov 8, 2020

That's a codegen test using FileCheck, https://llvm.org/docs/CommandGuide/FileCheck.html#tutorial

It'll need either changing the test or changing the code so that .clear() doesn't branch on the current length. The OP here sounds like you were expecting the addition of the branch in clear, so maybe look at the test history to see why there's a test saying that there shouldn't be one.

@erikdesjardins
Copy link
Contributor

FWIW this was previously attempted in #74172

@mbrubeck
Copy link
Contributor

mbrubeck commented Nov 9, 2020

Fixes #76089.

@crlf0710
Copy link
Member

@flotts Ping from triage, would you mind addressing the comments above? If you need some help, you can visit the rust-lang.zulipchat.com and seek for some help there. Thanks!

@feralfluid
Copy link
Author

feralfluid commented Nov 28, 2020

@flotts Ping from triage, would you mind addressing the comments above? If you need some help, you can visit the rust-lang.zulipchat.com and seek for some help there. Thanks!

I've been pretty swamped between school and work, I didn't expect this problem to be so complicated to solve, and I don't want to make changes to tests without sufficient understanding of the implications of those changes. Frankly, it makes no sense to me that there's a new branch as a result of this code.

I would recommend someone simply close this and give up for now or something. Maybe another time, if someone else doesn't fix it first. It clearly isn't a proper solution to this issue :P

@scottmcm
Copy link
Member

Frankly, it makes no sense to me that there's a new branch as a result of this code.

For anyone else who arrives and looks at this: this can happen because the old code, after inlining, resulted in if 0 > some_unsigned_number, which is of course never possible and thus gets optimized away. Changing the code so that the check is >= makes it possible again, and thus it is no longer optimized out.

@camelid camelid added A-collections Area: std::collections. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 18, 2020
@jyn514 jyn514 closed this Dec 18, 2020
@Dylan-DPC-zz Dylan-DPC-zz added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: std::collections. S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. 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.