Repository navigation
Implement takeInterval and dropInterval without concurrent streams - #3274
harendra-kumar merged 1 commit into
Conversation
| . interject (return Nothing) d . fmap Just | ||
| takeInterval :: MonadIO m => Double -> Stream m a -> Stream m a | ||
| takeInterval d (Stream.Stream step state) = | ||
| Stream.Stream step1 (TakeIntervalInit state) |
There was a problem hiding this comment.
A major issue with this implementation is that if the stream is blocked in the step, takeInterval will not finish promptly even if the timer expires. The original version did not have that problem. So let's revert to the original version with some improvements, something like this:
takeInterval d xs
-- Do not use "d <= 0", "not (d > 0)" works for NaN case as well
| not (d > 0) = Stream.nil
| otherwise =
Stream.catMaybes
$ Stream.takeWhile isJust
$ parListEagerFst
[fmap Just xs, Stream.fromEffect (Stream.sleep d >> return Nothing)]
We can add another combinator takeInterval_ which will be faster but with lax guarantees, like the one you implemented, via a separate PR.
You can add tests for the cases that we are solving with this change like d = 0, NaN etc.
There was a problem hiding this comment.
Done. Will work on the additional PR now.
|
@harendra-kumar I've addressed your comments. Let me know if and when you want me to squash. |
harendra-kumar
left a comment
There was a problem hiding this comment.
Let's squash and merge it.
takeInterval as it stands today has a bigger design/impl issue due to unintended concurrency and issues arising from that. But we will take care of that in another issue/PR.
|
You will need to rebase it on master as one of the commits has already been merged via the previous PR. |
takeInterval: merge the stream with a timer stream of a single element, instead of an endless stream of ticks generated by interject. dropInterval: read the clock for each element until the interval has passed, in a hand-written state machine. It requires only MonadIO instead of MonadAsync. Drop all the elements if the interval in nanoseconds overflows an Int64. Both: for a zero, negative or NaN interval, takeInterval takes nothing and dropInterval drops nothing. Add tests for zero, negative, NaN and overflowing intervals, and for takeInterval on a stream blocked in its evaluation. Co-Authored-By: Claude Fable 5.2 <[email protected]>
91a652f to
e42763b
Compare
Should be all good |
|
Created an issue for the design and behavior of take/dropInterval: #3277 |
Follow-up to #3272, implementing the improvements suggested in its review.
This branch is stacked on #3272, so until that one is merged this PR shows its
commit as well. Only the second commit is new.
Change
Both functions merged the stream with an endless stream of ticks using
interject, and then filtered the ticks out.dropIntervalis now a hand-written state machine. It reads the clock withgetTimefor each element until the interval has passed, and does not readit afterwards.
takeIntervalforks a one-shot timer thread that sets anIORefat the endof the interval, and reads the
IORefafter each step of the stream. Thetimer thread is killed if the stream ends before the interval.
MonadIOinstead ofMonadAsync.takeIntervalyieldsnothing and
dropIntervaldrops nothing. This resolves the twoXXXnotes.Behavior change
takeIntervalno longer interrupts the stream at the end of the interval ifits evaluation is blocked: the stream ends when the evaluation returns, and the
element it produced is discarded.
Fold.takeIntervalalready documents thesame behavior. This is stated in the haddock.
Known limitation
If the consumer stops evaluating a
takeIntervalstream before it ends (e.g.with
take), the timer thread remains asleep until the interval expires. Itholds only the
IORef.Benchmarks
Data.Stream.Prelude, GHC 9.12.4, 100000 elements, interval never expiring:takeInterval-alldropInterval-alldropInterval-allis the worst case fordropInterval, as the clock is readfor every element.
Tests
Two tests added for the zero interval. The six "Trimming" tests of
Data.Stream.Preludepass. TheDEVBUILDtiming tests and the other testsuites were not run locally.
🤖 Generated with the help of Claude Code