The Flux.fromIterable(Iterable) has an interesting sentence in its contract:
The
Iterable.iterator()method will be invoked at least once and at most twice for each subscriber.
What is the scenario when the method needs to call the Iterable.iterator() twice?
There is an explanation for it in the documentation, however I don't fully understand it:
This operator inspects the
Iterable'sSpliteratorto assess if the iteration can be guaranteed to be finite. Since the defaultSpliteratorwraps theIteratorwe can have twoIterable.iterator()calls. This second invocation is skipped on aCollectionhowever, a type which is assumed to be always finite.
Until that issue was recently resolved,
Iterable.iterator()was actually called twice. Here isFluxIterablesource code of reactor-core 3.4.25:while
FluxIterable.checkFinite(iterable)isand
Iterable.spliteratorisSo the first call to
.iteratoris made to obtain anSpliteratorand examine whether it's finite, and the second call is performed in order to actually obtain iterator that would be used to iterate over the elements.Since mentioned issue has been resolved, I'm not sure whether that notice about two
iterator()calls is still valid. May be callingiterator()only once now is a kind of undocumented feature that is not reliable in feature.