-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Conversation
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 has assigned @Mark-Simulacrum. Use r? to explicitly pick a reviewer |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
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
I have also been thinking about the opposite pattern with the "one-shot" on the outside, like an |
This comment has been minimized.
This comment has been minimized.
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.
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.
// These are always empty, which is fine to optimize too. | ||
impl<T> OneShot for Empty<T> {} | ||
impl<T> OneShot for array::IntoIter<T, 0> {} |
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 wonder if something flattening over those exists in the wild, but sure, why not.
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.
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.
☀️ Try build successful - checks-actions |
It doesn't appear to be in the queue? Let's try again: @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
looks like it didn't pick up the try build. @rust-timer build 4b67ea2 |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (4b67ea2): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking 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 Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis 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.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeResultsThis 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.
Bootstrap: 638.862s -> 638.174s (-0.11%) |
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. |
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 :) |
@bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (6672c16): comparison URL. Overall result: ❌✅ regressions and improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis 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.
CyclesResultsThis 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.
Binary sizeResultsThis 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.
Bootstrap: 640.998s -> 641.933s (0.15%) |
For iterators like
Once
andoption::IntoIter
that only ever have asingle item at most, the front and back iterator states in
FlatMap
andFlatten
are a waste, as they're always consumed already. We can usespecialization for these types to simplify the iterator methods.
It's a somewhat common pattern to use
flatten()
for options andresults, even recommended by multiple clippy lints. The
implementation is more efficient with
filter_map
, as mentioned inclippy#9377, but this new specialization should close some of that
gap for existing code that flattens.