c7406c1788
Refactor iteration logic in the `Flatten` and `FlatMap` iterators The `Flatten` and `FlatMap` iterators both delegate to `FlattenCompat`: ```rust struct FlattenCompat<I, U> { iter: Fuse<I>, frontiter: Option<U>, backiter: Option<U>, } ``` Every individual iterator method that `FlattenCompat` implements needs to carefully manage this state, checking whether the `frontiter` and `backiter` are present, and storing the current iterator appropriately if iteration is aborted. This has led to methods such as `next`, `advance_by`, and `try_fold` all having similar code for managing the iterator's state. I have extracted this common logic of iterating the inner iterators with the option to exit early into a `iter_try_fold` method: ```rust impl<I, U> FlattenCompat<I, U> where I: Iterator<Item: IntoIterator<IntoIter = U>>, { fn iter_try_fold<Acc, Fold, R>(&mut self, acc: Acc, fold: Fold) -> R where Fold: FnMut(Acc, &mut U) -> R, R: Try<Output = Acc>, { ... } } ``` It passes each of the inner iterators to the given function as long as it keep succeeding. It takes care of managing `FlattenCompat`'s state, so that the actual `Iterator` methods don't need to. The resulting code that makes use of this abstraction is much more straightforward: ```rust fn next(&mut self) -> Option<U::Item> { #[inline] fn next<U: Iterator>((): (), iter: &mut U) -> ControlFlow<U::Item> { match iter.next() { None => ControlFlow::CONTINUE, Some(x) => ControlFlow::Break(x), } } self.iter_try_fold((), next).break_value() } ``` Note that despite being implemented in terms of `iter_try_fold`, `next` is still able to benefit from `U`'s `next` method. It therefore does not take the performance hit that implementing `next` directly in terms of `Self::try_fold` causes (in some benchmarks). This PR also adds `iter_try_rfold` which captures the shared logic of `try_rfold` and `advance_back_by`, as well as `iter_fold` and `iter_rfold` for folding without early exits (used by `fold`, `rfold`, `count`, and `last`). Benchmark results: ``` before after bench_flat_map_sum 423,255 ns/iter 414,338 ns/iter bench_flat_map_ref_sum 1,942,139 ns/iter 2,216,643 ns/iter bench_flat_map_chain_sum 1,616,840 ns/iter 1,246,445 ns/iter bench_flat_map_chain_ref_sum 4,348,110 ns/iter 3,574,775 ns/iter bench_flat_map_chain_option_sum 780,037 ns/iter 780,679 ns/iter bench_flat_map_chain_option_ref_sum 2,056,458 ns/iter 834,932 ns/iter ``` I added the last two benchmarks specifically to demonstrate an extreme case where `FlatMap::next` can benefit from custom internal iteration of the outer iterator, so take it with a grain of salt. We should probably do a perf run to see if the changes to `next` are worth it in practice. |
||
---|---|---|
.cargo | ||
.github | ||
.vscode | ||
assets | ||
bench_data | ||
crates | ||
docs | ||
editors/code | ||
lib | ||
xtask | ||
.editorconfig | ||
.git-blame-ignore-revs | ||
.gitattributes | ||
.gitignore | ||
Cargo.lock | ||
Cargo.toml | ||
LICENSE-APACHE | ||
LICENSE-MIT | ||
PRIVACY.md | ||
README.md | ||
rustfmt.toml | ||
triagebot.toml |
rust-analyzer is a modular compiler frontend for the Rust language. It is a part of a larger rls-2.0 effort to create excellent IDE support for Rust.
Quick Start
https://rust-analyzer.github.io/manual.html#installation
Documentation
If you want to contribute to rust-analyzer or are just curious about how things work under the hood, check the ./docs/dev folder.
If you want to use rust-analyzer's language server with your editor of choice, check the manual folder. It also contains some tips & tricks to help you be more productive when using rust-analyzer.
Security and Privacy
See the corresponding sections of the manual.
Communication
For usage and troubleshooting requests, please use "IDEs and Editors" category of the Rust forum:
https://users.rust-lang.org/c/ide/14
For questions about development and implementation, join rust-analyzer working group on Zulip:
https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer
Quick Links
- Website: https://rust-analyzer.github.io/
- Metrics: https://rust-analyzer.github.io/metrics/
- API docs: https://rust-lang.github.io/rust-analyzer/ide/
- Changelog: https://rust-analyzer.github.io/thisweek
License
rust-analyzer is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for details.