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

implement TrustedLen for Flatten/FlatMap if the U: IntoIterator == [T; N] #87168

Merged
merged 4 commits into from
Jul 21, 2021

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Jul 15, 2021

This only works if arrays are passed directly instead of array iterators
because we need to be sure that they have not been advanced before
Flatten does its size calculation.

resolves #87094

…; N]

This only works if arrays are passed directly instead of array iterators
because we need to be sure that they have not been advanced before
Flatten does its size calculation.
@rust-highfive
Copy link
Collaborator

r? @scottmcm

(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 Jul 15, 2021
@the8472
Copy link
Member Author

the8472 commented Jul 16, 2021

The specialization now covers flattening over references to arrays in addition to owned arrays.

Due to rust-lang#20400 the corresponding TrustedLen impls need a helper trait
instead of directly adding `Item = &[T;N]` bounds.
Since TrustedLen is a public trait this in turn means
the helper trait needs to be public. Since it's just a workaround
for a compiler deficit it's marked hidden, unstable and unsafe.
@scottmcm
Copy link
Member

Thanks for this! I tried to do something similar back in #48544, but doing this with specialization avoids the complaints people had about that.

#[unstable(feature = "std_internals", issue = "none")]
// FIXME(#20400): Instead of this helper trait there should be multiple impl TrustedLen for Flatten<>
// blocks with different bounds on Iterator::Item but the compiler erroneously considers them overlapping
pub unsafe trait TrustedConstSize: IntoIterator {}
Copy link
Member

Choose a reason for hiding this comment

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

nit: I would have expected something like TrustedConstSizeIntoIterator : ConstSizeIntoIterator? Is there some reason this mentions only IntoIterator?

Copy link
Member Author

Choose a reason for hiding this comment

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

ConstSizeIntoIterator is implemented for all IntoIterator anyway but returns a None for the non-const ones. So adding that bound wouldn't make a difference.

@scottmcm scottmcm 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 Jul 19, 2021
@the8472 the8472 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 Jul 19, 2021
@scottmcm
Copy link
Member

Thanks! Let me just check perf just in case:

@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 Jul 20, 2021
@bors
Copy link
Contributor

bors commented Jul 20, 2021

⌛ Trying commit c3ac8d8 with merge e54a7e2fa88fac0e640991c5eae629c26bb69bf1...

@bors
Copy link
Contributor

bors commented Jul 20, 2021

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

@rust-timer
Copy link
Collaborator

Queued e54a7e2fa88fac0e640991c5eae629c26bb69bf1 with parent 6535449, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (e54a7e2fa88fac0e640991c5eae629c26bb69bf1): comparison url.

Summary: This benchmark run did not return any significant changes.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up.

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

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

@bors r+ rollup=iffy

@bors
Copy link
Contributor

bors commented Jul 20, 2021

📌 Commit c3ac8d8 has been approved by scottmcm

@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 Jul 20, 2021
@bors
Copy link
Contributor

bors commented Jul 20, 2021

⌛ Testing commit c3ac8d8 with merge fabf502...

@bors
Copy link
Contributor

bors commented Jul 21, 2021

☀️ Test successful - checks-actions
Approved by: scottmcm
Pushing fabf502 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 21, 2021
@bors bors merged commit fabf502 into rust-lang:master Jul 21, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jul 21, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 27, 2021
implement fold() on array::IntoIter to improve flatten().collect() perf

With rust-lang#87168 flattening `array::IntoIter`s is now `TrustedLen`, the `FromIterator` implementation for `Vec` has a specialization for `TrustedLen` iterators which uses internal iteration. This implements one of the main internal iteration methods on `array::Into` to optimize the combination of those two features.

This should address the main issue in rust-lang#87411

```
# old
test vec::bench_flat_map_collect                         ... bench:   2,244,024 ns/iter (+/- 18,903)

# new
test vec::bench_flat_map_collect                         ... bench:     172,863 ns/iter (+/- 2,141)
```
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

iter::Flatten, FlatMap should return more precise size_hint and be TrustedLen when item type is an array
7 participants