Skip to content

Add takeInterval_, a faster takeInterval with a weaker guarantee - #3276

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

harendra-kumar merged 1 commit into
composewell:masterfrom
GuillaumedeVolpiano:add-take-interval-lax

Conversation

@GuillaumedeVolpiano

Copy link
Copy Markdown
Contributor

Follow-up to #3274, adding the combinator suggested in its review.

This branch is stacked on #3274. Only the last commit is new.

Change

takeInterval_ is a variant of takeInterval that does not use concurrent
streams. It 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.

  • It requires only MonadIO instead of MonadAsync.
  • A zero, negative or NaN interval yields nothing, as for takeInterval.
  • It is exported from Streamly.Internal.Data.Stream.Time only, and marked
    as pre-release.

Difference with takeInterval

takeInterval_ does not interrupt 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. This is stated in the haddock.

Known limitation

If the consumer stops evaluating the 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 Time Allocated
takeInterval-all 10.6 ms 56 MB
takeInterval_-all 214 μs 1.2 KB

takeInterval_-all is a new benchmark.

Tests

Seven tests added to the "Trimming" tests of Data.Stream.Prelude:

  • An interval longer than the stream, and an infinite one, take all the elements.
  • A short interval ends an endless stream and returns a prefix of it.
  • A zero, negative and NaN interval take nothing.
  • With a stream blocked past the end of the interval, the stream ends when it unblocks and the element is discarded.

The 21 "Trimming" tests pass. The DEVBUILD timing tests and the other test
suites were not run locally.

🤖 Generated with the help of Claude Code

@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 align this with: #3277

r <- step gst st
expired <- liftIO $ readIORef ref
if expired
then return Stop

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.

need killThread here.

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.

The flag is set by the timer's thread's last action, so the thread is already dying. Adding it would be harmless, though, so happy to comply if you insist ;)

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.

That's right.

@GuillaumedeVolpiano

Copy link
Copy Markdown
Contributor Author

Four of your five comments have been addressed. While doing this, I updated the tests: the test of an action in progress now expects its element to be yielded, and a new test covers the loop that does not allocate. It uses a finite stream, so that a regression fails instead of hanging.

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

Looks good.

r <- step gst st
expired <- liftIO $ readIORef ref
if expired
then return Stop

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.

That's right.

@GuillaumedeVolpiano

Copy link
Copy Markdown
Contributor Author

Shall I squash ?

@harendra-kumar

Copy link
Copy Markdown
Member

rebase and squash.

takeInterval_ evaluates the stream in the thread of the consumer, and
does not require MonadAsync. An element is yielded if the evaluation
generating it starts before the end of the interval, whenever it
finishes: the evaluation is not interrupted at the end of the interval.
This is the design of takeInterval_ in composewell#3277.

A one-shot timer thread sets a flag at the end of the interval, which is
cheaper to read than the clock for each element. The evaluating thread
yields the CPU once every 1000 steps, as a fused loop that does not
allocate never yields on its own and the timer thread would not get to
run.

The timer thread holds only a weak reference to the flag, and a
finalizer on the flag kills the thread when the stream is abandoned. The
timer is set up with asynchronous exceptions masked.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@harendra-kumar
harendra-kumar merged commit 6ef0ea3 into composewell:master Nov 27, 2026
15 of 17 checks passed
@GuillaumedeVolpiano
GuillaumedeVolpiano deleted the add-take-interval-lax 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