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

Specialize flattening iterators with only one inner item #121204

Merged
merged 2 commits into from
Feb 17, 2024

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Feb 16, 2024

For iterators like Once and option::IntoIter that only ever have a
single item at most, the front and back iterator states in FlatMap and
Flatten are a waste, as they're always consumed already. We can use
specialization for these types to simplify the iterator methods.

It's a somewhat common pattern to use flatten() for options and
results, even recommended by multiple clippy lints. The
implementation is more efficient with filter_map, as mentioned in
clippy#9377, but this new specialization should close some of that
gap for existing code that flattens.

For iterators like `Once` and `option::IntoIter` that only ever have a
single item at most, the front and back iterator states in `FlatMap` and
`Flatten` are a waste, as they're always consumed already. We can use
specialization for these types to simplify the iterator methods.

It's a somewhat common pattern to use `flatten()` for options and
results, even recommended by [multiple][1] [clippy][2] [lints][3]. The
implementation is more efficient with `filter_map`, as mentioned in
[clippy#9377], but this new specialization should close some of that
gap for existing code that flattens.

[1]: https://rust-lang.github.io/rust-clippy/master/#filter_map_identity
[2]: https://rust-lang.github.io/rust-clippy/master/#option_filter_map
[3]: https://rust-lang.github.io/rust-clippy/master/#result_filter_map
[clippy#9377]: rust-lang/rust-clippy#9377
@rustbot
Copy link
Collaborator

rustbot commented Feb 16, 2024

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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 Feb 16, 2024
@cuviper
Copy link
Member Author

cuviper commented Feb 16, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 16, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 16, 2024
Specialize flattening iterators with only one inner item

For iterators like `Once` and `option::IntoIter` that only ever have a
single item at most, the front and back iterator states in `FlatMap` and
`Flatten` are a waste, as they're always consumed already. We can use
specialization for these types to simplify the iterator methods.

It's a somewhat common pattern to use `flatten()` for options and
results, even recommended by [multiple][1] [clippy][2] [lints][3]. The
implementation is more efficient with `filter_map`, as mentioned in
[clippy#9377], but this new specialization should close some of that
gap for existing code that flattens.

[1]: https://rust-lang.github.io/rust-clippy/master/#filter_map_identity
[2]: https://rust-lang.github.io/rust-clippy/master/#option_filter_map
[3]: https://rust-lang.github.io/rust-clippy/master/#result_filter_map
[clippy#9377]: rust-lang/rust-clippy#9377
@bors
Copy link
Contributor

bors commented Feb 16, 2024

⌛ Trying commit 974bc45 with merge 4b67ea2...

@cuviper
Copy link
Member Author

cuviper commented Feb 16, 2024

I have also been thinking about the opposite pattern with the "one-shot" on the outside, like an Option<I> where you call .into_iter().flatten(). I think this is also a candidate for streamlining, but it could be met with a new Option method for a simplified iterator wrapper.

@the8472 the8472 assigned the8472 and unassigned Mark-Simulacrum Feb 16, 2024
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@the8472 the8472 left a comment

Choose a reason for hiding this comment

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

Neat optimization.

I think next_chunk could also do what filter_map does here. But that optimization is a bit gnarly unsafe, so that can wait until we're closer to stabilizing the chunking iterators.

library/core/src/iter/adapters/flatten.rs Outdated Show resolved Hide resolved
Comment on lines +865 to +867
// These are always empty, which is fine to optimize too.
impl<T> OneShot for Empty<T> {}
impl<T> OneShot for array::IntoIter<T, 0> {}
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if something flattening over those exists in the wild, but sure, why not.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it would be weird, but maybe in some generic or macro code?
I wouldn't bother with this if it weren't trivial, for sure.

library/core/src/iter/adapters/flatten.rs Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Feb 16, 2024

☀️ Try build successful - checks-actions
Build commit: 4b67ea2 (4b67ea2e652830fe0a05868f9a698180a18295fa)

@cuviper
Copy link
Member Author

cuviper commented Feb 17, 2024

It doesn't appear to be in the queue? Let's try again:

@rust-timer queue

@rust-timer

This comment has been minimized.

@the8472
Copy link
Member

the8472 commented Feb 17, 2024

looks like it didn't pick up the try build.

@rust-timer build 4b67ea2

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4b67ea2): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

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

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.6% [-0.6%, -0.6%] 1

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
4.9% [4.9%, 4.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.6% [-1.6%, -1.6%] 1
All ❌✅ (primary) 4.9% [4.9%, 4.9%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.2%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.2%, -0.1%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.2%, 0.2%] 11

Bootstrap: 638.862s -> 638.174s (-0.11%)
Artifact size: 306.32 MiB -> 306.35 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 17, 2024
@cuviper
Copy link
Member Author

cuviper commented Feb 17, 2024

Of course I hoped for more positive results, but neutral is ok. Maybe the compiler is already sufficiently avoiding such flat-maps in its own hot loops. I can definitely show wins in microbenchmarks, so IMO it's still a potential boost for user code.

@Kobzol
Copy link
Contributor

Kobzol commented Feb 17, 2024

Usually no-op changes are half/half green/red when you click on the "Show non-relevant results", this one is mostly green. So there might be some tiny win. In any case, it doesn't seem to be a regression :)

@the8472
Copy link
Member

the8472 commented Feb 17, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Feb 17, 2024

📌 Commit c36ae93 has been approved by the8472

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 Feb 17, 2024
@bors
Copy link
Contributor

bors commented Feb 17, 2024

⌛ Testing commit c36ae93 with merge 6672c16...

@bors
Copy link
Contributor

bors commented Feb 17, 2024

☀️ Test successful - checks-actions
Approved by: the8472
Pushing 6672c16 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 17, 2024
@bors bors merged commit 6672c16 into rust-lang:master Feb 17, 2024
12 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Feb 17, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (6672c16): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.6% [0.6%, 0.6%] 1
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 1
Improvements ✅
(secondary)
-2.6% [-2.6%, -2.6%] 1
All ❌✅ (primary) -0.6% [-0.6%, -0.6%] 1

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.6% [1.6%, 1.6%] 1
Improvements ✅
(primary)
-2.9% [-3.7%, -2.2%] 5
Improvements ✅
(secondary)
-2.6% [-3.7%, -1.6%] 16
All ❌✅ (primary) -2.9% [-3.7%, -2.2%] 5

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.4% [-1.4%, -1.4%] 1
All ❌✅ (primary) - - 0

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.2%] 6
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.1%, -0.1%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-0.1%, 0.2%] 13

Bootstrap: 640.998s -> 641.933s (0.15%)
Artifact size: 306.40 MiB -> 306.43 MiB (0.01%)

@cuviper cuviper deleted the flatten-one-shot branch March 10, 2024 21:01
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 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.

8 participants