Remove caching from filter, drop and drop_while #120
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This might be a bit controversial...
The
filter
,drop
anddrop_while
adaptors, when operating on multipass sequences, cache the first cursor: that is, the first call tofirst()
does a (potentially)O(N)
pass over the data, but subsequent calls tofirst()
returns the cached cursor. I originally did this for the same reason that Ranges does it: I wantedfirst()
to always be (amortised?)O(1)
.As time goes on though, I'm less certain that this was the right choice. We definitely need
is_last()
to beO(1)
; I'm very sure we also wantlast()
to beO(1)
for bounded sequences. But I'm less convinced aboutfirst()
. After all, getting the second element withinc()
can never be constant-time, so why do we special-case getting the first one?Various later Flux adaptors don't do caching where the Ranges versions do, and there seems to be little ill effect. Also, the internal iteration implementation of
filter
has never used the cache, and I don't think anyone has ever noticed.Getting rid of caching allows const-iteration, which is useful for such fundamental adaptors, and stops people needing to reach for
mut_ref
. It also makes life easier for the optimiser.