-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
concatMapIterable could be enhanced to discard iterator remainder #2014
Comments
Currently We could attempt to discard the iterator on top of the prefetched source elements. This is tricky though, since an arbitrary In order to implement a best effort solution, we can retain the |
Something to consider: |
the |
What would that mean for the API? Currently it takes a mapper with |
Every https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#spliterator-- |
…from iterator If both an Iterator and a Spliterator can be generated for each of the processed Iterables, then the Spliterator is used to ensure the Iterable is SIZED. This allows us to safely assume we can iterate over the remainder of the iterator when cancelling, in order to discard its elements that weren't emitted. Not doing this check would likely cause trouble with infinite discarding loops in the case of infinite Iterables (which is technically possible). For Streams, since both the iterator() and spliterator() methods are terminating the Stream we only generate the Spliterator. We use it to check SIZED and then wrap it in an Iterator adapter for iteration (which is what BaseStream does by default). Note that using a Spliterator to drive the internal iteration doesn't work that well, since the default Iterable#spliterator isn't SIZED and its estimatedSize() method doesn't behave like hasNext(). Iterator#hasNext is far better suited for looking ahead of the emitted element to trigger onComplete immediately after the last onNext.
See more details in #2021, but going An approach mixing the |
In this change, the goal is to discard elements of the Iterable that haven't been processed yet. The challenge is to avoid attempting doing so for _infinite_ Iterables (which would lead to infinite discarding loops). If the Iterable is a Collection, it should be finite. If both an Iterator and a Spliterator can be generated for each of the processed Iterables, then the Spliterator is used to ensure the Iterable is SIZED. This allows us to safely assume we can iterate over the remainder of the iterator when cancelling, in order to discard its elements that weren't emitted. For Streams, since both the iterator() and spliterator() methods are terminating the Stream we only generate the Spliterator. We use it to check SIZED and then wrap it in an Iterator adapter for iteration (which is what BaseStream does by default). Implementation Notes ---- We didn't fully switch to using a Spliterator to drive the internal iteration. It doesn't work that well, since the Iterable#spliterator default implementation isn't SIZED and its estimatedSize() method does not behave like hasNext(). Iterator#hasNext is far better suited for looking ahead of the emitted element to trigger onComplete immediately after the last onNext.
This commit improves the javadoc from reactor#2014: - missed adding the javadoc discard tags to `fromIterable` and `fromStream` - align javadocs of `concatMapIterable` and `flatMapIterable` since they are aliases It also improves the wording and clarifies that `flatMapIterable` and `fromIterable` discard support can lead to multiple `iterator()` calls. Fixes reactor#2127
This commit improves the javadoc from #2014: - missed adding the javadoc discard tags to `fromIterable` and `fromStream` - align javadocs of `concatMapIterable` and `flatMapIterable` since they are aliases It also improves the wording and clarifies that `flatMapIterable` and `fromIterable` discard support can lead to multiple `iterator()` calls. Fixes #2127
From #1925:
Here is a simplified test:
Originally posted by @rstoyanchev in #1925 (comment)
The text was updated successfully, but these errors were encountered: