Skip to content

Implement takeInterval and dropInterval without concurrent streams - #3274

Merged
harendra-kumar merged 1 commit into
composewell:masterfrom
GuillaumedeVolpiano:improve-take-drop-interval
Nov 27, 2026
Merged

harendra-kumar merged 1 commit into
composewell:masterfrom
GuillaumedeVolpiano:improve-take-drop-interval

Conversation

@GuillaumedeVolpiano

@GuillaumedeVolpiano GuillaumedeVolpiano commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • dropInterval is now a hand-written state machine. It reads the clock with
    getTime for each element until the interval has passed, and does not read
    it afterwards.
  • takeInterval forks a one-shot timer thread that sets an IORef at the end
    of the interval, and reads the IORef after each step of the stream. The
    timer thread is killed if the stream ends before the interval.
  • Both need only MonadIO instead of MonadAsync.
  • A zero or negative interval is handled explicitly: takeInterval yields
    nothing and dropInterval drops nothing. This resolves the two XXX notes.

Behavior change

takeInterval no longer interrupts the stream at the end of the interval if
its evaluation is blocked: the stream ends when the evaluation returns, and the
element it produced is discarded. Fold.takeInterval already documents the
same behavior. This is stated in the haddock.

Known limitation

If the consumer stops evaluating a takeInterval stream before it ends (e.g.
with take), the timer thread remains asleep until the interval expires. It
holds only the IORef.

Benchmarks

Data.Stream.Prelude, GHC 9.12.4, 100000 elements, interval never expiring:

Benchmark Before After
takeInterval-all 15.2 ms, 55 MB 235 μs, 1.2 KB
dropInterval-all 16.3 ms, 55 MB 9.18 ms, 15 MB

dropInterval-all is the worst case for dropInterval, as the clock is read
for every element.

Tests

Two tests added for the zero interval. The six "Trimming" tests of
Data.Stream.Prelude pass. The DEVBUILD timing tests and the other test
suites were not run locally.

🤖 Generated with the help of Claude Code

. 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Will work on the additional PR now.

@GuillaumedeVolpiano

GuillaumedeVolpiano commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

@harendra-kumar I've addressed your comments. Let me know if and when you want me to squash.

@harendra-kumar harendra-kumar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@harendra-kumar

Copy link
Copy Markdown
Member

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]>
@GuillaumedeVolpiano

Copy link
Copy Markdown
Contributor Author

You will need to rebase it on master as one of the commits has already been merged via the previous PR.

Should be all good

@harendra-kumar

Copy link
Copy Markdown
Member

Created an issue for the design and behavior of take/dropInterval: #3277

@harendra-kumar
harendra-kumar merged commit a57c639 into composewell:master Nov 27, 2026
15 of 17 checks passed
@GuillaumedeVolpiano
GuillaumedeVolpiano deleted the improve-take-drop-interval branch November 27, 2026 16:58
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.

2 participants