You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve the design of takeInterval and dropInterval #3277
The current implementation of takeInterval has problems due to unintended concurrency because we are using concurrent streams to implement it. Problems:
The stream is executed ahead and buffered this may be a surprise to the user.
the result of the timer thread (Nothing) may be queued behind the results of the stream and queue may be long, so a slow consumer will continue to consume the buffered results for a long time until it sees the timer expiry result.
Design goals for the combinator behavior, there are two possible combinators:
takeInterval provides a guarantee that only those stream results which are available before the deadline are consumed. In other words it has a finishing before the deadline guarantee.
takeInterval_ provides a guarantee that the stream action will be started before the deadline, but it may finish whenever or never. In other words it has start before deadline guarantee.
The combinator should not have concurrency, execution ahead of time more than just that one action which is in progress and may be aborted. If a user needs concurrency they can build it into the action using parBuffered or any other means.
Similar corresponding design is possible for dropInterval:
dropInterval provides a guarantee that any results available before the interval will be dropped.
dropInterval_ provides a guarantee that any actions started before the interval will have their results dropped.
We can implement takeInterval with an MVar and a single worker thread instead of using concurrency streams; the consumer asks for the next element, and the worker runs one step and returns it. For good performance we may have to pin the worker and the consumer to the same capability. Both dropInterval variants will probably not need any concurrency.
We should align the Fold.takeInterval design as well along these lines.
Document the guarantees provided by each combinator in the haddock docs.
@harendra-kumar I have prototyped the rest of this design on top of #3276: takeInterval with one worker thread and MVars, and dropInterval_. The worker version behaves as described here, but it is slow, so I also tried an alternative. I would like your opinion before going further.
Two versions of takeInterval
Worker. As proposed above: the consumer asks for the next element through an MVar, a worker thread runs one step and returns the result. A timer thread ends the wait at the deadline, and the worker is killed. The worker is created on the capability of the consumer.
Interrupt. The steps run in the thread of the consumer. At the deadline, a timer thread throws a private asynchronous exception to that thread if it is inside a step, as System.Timeout.timeout does. Asynchronous exceptions are unmasked only during the step.
In both, each step is run with the monad state of the consumer and its result restores that state, so the changes made by the source are no longer lost. With the channel version, a count kept in StateT by the source is 0 after the stream.
Behavior
Check
Channel (master)
Worker
Interrupt
Source runs, for 5 elements consumed
1495 to 2995
5
5
Count kept in StateT over 100 elements
0
100
100
Source runs in the thread of the consumer
No
No
Yes
Interval 0.2 s, consumer spends 20 ms per element
150 elements over 3.1 s
10 in 201 ms
10 in 201 ms
Source blocked in threadDelay
Interrupted
[1] in 200 ms
[1] in 200 ms
Step under uninterruptibleMask_ for 1 s
Not tested
[1] in 200 ms
[1] in 1001 ms
Exception in the source
Not tested
Reaches the consumer
Reaches the consumer
In both versions the threads are gone after a major GC when the stream is abandoned.
Cost
Time per element, one million elements, interval that never expires, GHC 9.12.4, -O2:
Runtime, thread of the consumer
Worker
Interrupt
takeInterval_
Non-threaded
1340 ns
78 ns
3 ns
Threaded -N1, main thread
8380 ns
89 ns
3 ns
Threaded -N1, forkIO thread
216 ns
96 ns
3 ns
Threaded -N4, main thread
11860 ns
80 ns
3 ns
Threaded -N4, forkIO thread
212 ns
89 ns
3 ns
The channel version on master takes about 130 ns per element in the takeInterval-all benchmark.
The worker version is at its worst when the consumer is the main thread of a threaded program, which is a bound thread. Pinning the worker to the capability of the consumer is not enough either: under -N4 with the consumer in a forkIO thread, I have seen 25 ms and 536 ms for 100,000 elements in two consecutive runs, the consumer itself not being pinned.
Trade-off
The worker version ends the stream on time whatever the step does, since the consumer only stops waiting. It pays two thread switches per step.
The interrupt version has no hand-over, and keeps the source in the thread of the consumer. It cannot end the stream on time if the step masks asynchronous exceptions or is in a foreign call; I expect the same of a step that catches all exceptions, which I have not tested.
The interrupt version is a prototype: it is not optimized, and it does not yet yield the CPU periodically as takeInterval_ now does.
Questions
Which of the two do you prefer for takeInterval? Given the cost in the main thread, I would propose the interrupt version, and document its limit.
dropInterval_ is written as described here, without concurrency, and dropInterval is unchanged. I have not touched Fold.takeInterval yet.
The other combinators built on interject (intervalsOf, sampleIntervalEnd, sampleBurstEnd, timedGroupsOf, classifySessionsOf and their variants) share the two problems listed at the top of this issue, and the lost monad state. With a consumer spending 0.5 s per interval, intervalsOf 0.1 Fold.length gave intervals of sizes [78637,1497,0,0]. Should they be part of this issue or a separate one?
Activity
Align takeInterval_ with the design in composewell#3277
GuillaumedeVolpiano commentedon Nov 27, 2026
@harendra-kumar I have prototyped the rest of this design on top of #3276:
takeIntervalwith one worker thread and MVars, anddropInterval_. The worker version behaves as described here, but it is slow, so I also tried an alternative. I would like your opinion before going further.Two versions of
takeIntervalSystem.Timeout.timeoutdoes. Asynchronous exceptions are unmasked only during the step.In both, each step is run with the monad state of the consumer and its result restores that state, so the changes made by the source are no longer lost. With the channel version, a count kept in
StateTby the source is 0 after the stream.Behavior
StateTover 100 elementsthreadDelay[1]in 200 ms[1]in 200 msuninterruptibleMask_for 1 s[1]in 200 ms[1]in 1001 msIn both versions the threads are gone after a major GC when the stream is abandoned.
Cost
Time per element, one million elements, interval that never expires, GHC 9.12.4,
-O2:takeInterval_-N1, main thread-N1,forkIOthread-N4, main thread-N4,forkIOthreadThe channel version on master takes about 130 ns per element in the
takeInterval-allbenchmark.The worker version is at its worst when the consumer is the main thread of a threaded program, which is a bound thread. Pinning the worker to the capability of the consumer is not enough either: under
-N4with the consumer in aforkIOthread, I have seen 25 ms and 536 ms for 100,000 elements in two consecutive runs, the consumer itself not being pinned.Trade-off
The interrupt version is a prototype: it is not optimized, and it does not yet yield the CPU periodically as
takeInterval_now does.Questions
takeInterval? Given the cost in the main thread, I would propose the interrupt version, and document its limit.dropInterval_is written as described here, without concurrency, anddropIntervalis unchanged. I have not touchedFold.takeIntervalyet.interject(intervalsOf,sampleIntervalEnd,sampleBurstEnd,timedGroupsOf,classifySessionsOfand their variants) share the two problems listed at the top of this issue, and the lost monad state. With a consumer spending 0.5 s per interval,intervalsOf 0.1 Fold.lengthgave intervals of sizes[78637,1497,0,0]. Should they be part of this issue or a separate one?I can push the branch as a draft PR if you want to look at the code, or you can check it here https://github.com/GuillaumedeVolpiano/streamly/tree/interval-design-3277
Prototype the design of takeInterval and dropInterval in composewell#…
Add takeInterval_, a faster takeInterval with a weaker guarantee
Prototype the design of takeInterval and dropInterval in composewell#…