Repository navigation
Add takeInterval_, a faster takeInterval with a weaker guarantee - #3276
Merged
harendra-kumar merged 1 commit intoNov 27, 2026
Merged
Conversation
GuillaumedeVolpiano
force-pushed
the
add-take-interval-lax
branch
from
November 27, 2026 06:34
16c0d9c to
a544281
Compare
harendra-kumar
requested changes
Nov 27, 2026
harendra-kumar
left a comment
Member
There was a problem hiding this comment.
Let's align this with: #3277
| r <- step gst st | ||
| expired <- liftIO $ readIORef ref | ||
| if expired | ||
| then return Stop |
Contributor
Author
There was a problem hiding this comment.
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 ;)
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
approved these changes
Nov 27, 2026
| r <- step gst st | ||
| expired <- liftIO $ readIORef ref | ||
| if expired | ||
| then return Stop |
Contributor
Author
|
Shall I squash ? |
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]>
GuillaumedeVolpiano
force-pushed
the
add-take-interval-lax
branch
from
November 27, 2026 16:28
f06b32f to
6235a52
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 oftakeIntervalthat does not use concurrentstreams. It forks a one-shot timer thread that sets an
IORefat the end ofthe interval, and reads the
IORefafter each step of the stream. The timerthread is killed if the stream ends before the interval.
MonadIOinstead ofMonadAsync.takeInterval.Streamly.Internal.Data.Stream.Timeonly, and markedas pre-release.
Difference with takeInterval
takeInterval_does not interrupt the stream at the end of the interval if itsevaluation 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:takeInterval-alltakeInterval_-alltakeInterval_-allis a new benchmark.Tests
Seven tests added to the "Trimming" tests of
Data.Stream.Prelude:The 21 "Trimming" tests pass. The
DEVBUILDtiming tests and the other testsuites were not run locally.
🤖 Generated with the help of Claude Code