Skip to content
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

Remove caching from filter, drop and drop_while #120

Merged
merged 8 commits into from
Aug 15, 2023
Merged

Conversation

tcbrindle
Copy link
Owner

@tcbrindle tcbrindle commented Aug 14, 2023

This might be a bit controversial...

The filter, drop and drop_while adaptors, when operating on multipass sequences, cache the first cursor: that is, the first call to first() does a (potentially) O(N) pass over the data, but subsequent calls to first()returns the cached cursor. I originally did this for the same reason that Ranges does it: I wanted first() 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 be O(1); I'm very sure we also want last() to be O(1) for bounded sequences. But I'm less convinced about first(). After all, getting the second element with inc() 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.

@codecov
Copy link

codecov bot commented Aug 14, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.01% ⚠️

Comparison is base (16cd229) 97.58% compared to head (5e4c62e) 97.58%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #120      +/-   ##
==========================================
- Coverage   97.58%   97.58%   -0.01%     
==========================================
  Files          66       66              
  Lines        2280     2278       -2     
==========================================
- Hits         2225     2223       -2     
  Misses         55       55              
Files Changed Coverage Δ
include/flux/op/drop.hpp 100.00% <100.00%> (ø)
include/flux/op/drop_while.hpp 100.00% <100.00%> (ø)
include/flux/op/filter.hpp 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tcbrindle tcbrindle merged commit 4bb895e into main Aug 15, 2023
24 of 25 checks passed
@tcbrindle tcbrindle deleted the pr/no_caching branch December 8, 2023 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant