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

panic early when TrustedLen indicates a length > usize::MAX #83726

Merged
merged 1 commit into from
Apr 1, 2021

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Mar 31, 2021

Changes TrustedLen specializations to immediately panic when size_hint().1 == None.

As far as I can tell this is not a change a minimal change in observable behavior for anything except ZSTs because the fallback path would go through extend_desugared() which tries to reserve(lower_bound) which already is usize::MAX and that would also lead to a panic. Before it might have popped somewhere between zero and a few elements from the iterator before panicking while it now panics immediately.

Overall this should reduce codegen by eliminating the fallback paths.

While looking into the with_capacity() behavior I also noticed that its documentation didn't have a Panics section, so I added that.

@rust-highfive
Copy link
Collaborator

r? @kennytm

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 31, 2021
@the8472
Copy link
Member Author

the8472 commented Mar 31, 2021

@rustbot label T-libs

since it also touches visible docs, not just implementation

@rustbot
Copy link
Collaborator

rustbot commented Mar 31, 2021

Error: Label since can only be set by Rust team members

Please let @rust-lang/release know if you're having trouble with this bot.

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 31, 2021
@JohnTitor
Copy link
Member

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 31, 2021
@bors
Copy link
Contributor

bors commented Mar 31, 2021

⌛ Trying commit ad3a791 with merge fd6d76150a587f8fbfa740e956a61774c1e85edc...

@bors
Copy link
Contributor

bors commented Apr 1, 2021

☀️ Try build successful - checks-actions
Build commit: fd6d76150a587f8fbfa740e956a61774c1e85edc (fd6d76150a587f8fbfa740e956a61774c1e85edc)

@rust-timer
Copy link
Collaborator

Queued fd6d76150a587f8fbfa740e956a61774c1e85edc with parent 4fdac23, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (fd6d76150a587f8fbfa740e956a61774c1e85edc): comparison url.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying rollup- to bors.

Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 1, 2021
@kennytm
Copy link
Member

kennytm commented Apr 1, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Apr 1, 2021

📌 Commit ad3a791 has been approved by kennytm

@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 Apr 1, 2021
@bors
Copy link
Contributor

bors commented Apr 1, 2021

⌛ Testing commit ad3a791 with merge 803ddb8...

@bors
Copy link
Contributor

bors commented Apr 1, 2021

☀️ Test successful - checks-actions
Approved by: kennytm
Pushing 803ddb8 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 1, 2021
@bors bors merged commit 803ddb8 into rust-lang:master Apr 1, 2021
@rustbot rustbot added this to the 1.53.0 milestone Apr 1, 2021
@Mark-Simulacrum
Copy link
Member

This.. surprisingly to me, at least, ended up being a pretty nice perf win, of up to 8%: https://perf.rust-lang.org/compare.html?start=49e1ec09952c5ab7798addd29532d44dc020283f&end=803ddb83598838fb9de308d283b759ba463e5e80&stat=instructions:u. Largely this looks to be driven by incremental benchmark improvements, but not completely.

@the8472
Copy link
Member Author

the8472 commented Apr 6, 2021

Some iter.collect<{Arc, Rc}>() may have resulted in up to 3 separate monomorphizations of each iterator call chain. One for Rc::from_iter_exact, one for spec_extend and one for the fallback. For iter.collect<Vec>() there would have been two. Now there should only be one.

I haven't actually looked at the generated IR, but that's the theory that motivated this change.

@@ -47,7 +47,12 @@ where
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does the above code seemed to have the same condition written twice? See 10 lines above.

Copy link
Member Author

Choose a reason for hiding this comment

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

One checks that high == low. the other checks that it's finite.

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I mean

        if let Some(high_value) = high {
            debug_assert_eq!(
                low,
                high_value,
                "TrustedLen iterator's size hint is not exact: {:?}",
                (low, high)
            );
        }
        if let Some(additional) = high {
            self.reserve(additional);
...

Copy link
Member Author

Choose a reason for hiding this comment

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

idk, probably just an historic artifact of code being moved around and redundant conditions not being merged. Someone just has to clean it up.

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 15, 2021
Merge same condition branch in vec spec_extend

Follow up of rust-lang#83726 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants