Repository navigation
Give every scheduled event its own heap node - #193
Merged
Merged
Conversation
Each thread schedules events into its own queue, which the loop thread drains. The queue's read and write indices were plain fields, so the loop thread could see an advanced write index before the event in the slot, or keep reading a storage array that a resize had replaced. It then lost the event, and a coroutine waiting for it never resumed. On the JVM, bursts of yields on a thread pool hung within a few rounds. The indices are now atomic, so storing the write index after the slot publishes both to the loop thread. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
scheduler/yield_burst runs many coroutines that only yield, through CoroRun.run. scheduler/heap_same_time and scheduler/heap_mixed measure the heap on its own with a VirtualTimeScheduler, with one due time for all events and with 1000 different due times. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MinimumHeap used to attach events with the same due time to an existing node as child events, found by searching subtrees and undoing the sift when a match turned up. That made insertion O(n) in the worst case, and FIFO among equal due times only held when the search found the match. Dispatching a node also dispatched its children, so if one of them threw, the rest were lost. The heap now orders events by due time and then by insertion sequence, with one node per event. The schedulers extract and dispatch one event at a time, so the events after a throwing one stay in the heap. ThreadAwareScheduler moves the events from the thread queues into the heap instead of collecting due events into a list of its own. ScheduledEvent loses addChildEvent, its child events and next. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With atomic indices, the scheduler's CircularQueue had become the same data structure as WorkStealingQueue, so the scheduler now uses that. The loop thread is the only consumer, so steal only returns null when the queue is empty. WorkStealingQueue now also publishes its storage atomically. A consumer that loaded the write index before a resize could still read the new storage afterwards through a plain field, without seeing the slots that were copied into it. It would then take an empty slot and lose that element. This affected both the thread pool and the scheduler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # src/hxcoro/schedulers/ThreadAwareScheduler.hx
On hxcpp, AtomicObject.load returns an untyped hx::Object, so accessing a field directly on its result generated a member access on hx::Object, which doesn't compile. The slots are now loaded through a non-inline function with a declared return type, which makes hxcpp convert the object first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On neko, assigning a declared field for the first time adds it to the object, which is not safe while another thread reads the object. The thread pool shares continuations between threads like that, so the test failed there for reasons unrelated to the scheduler. This is tracked in HaxeFoundation/neko#335. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> # Conflicts: # src/hxcoro/schedulers/ThreadAwareScheduler.hx
Member
Author
|
The benchmark times don't look particularly great but I think I've been paying too much attention to |
Member
Author
|
The C++ failures are unrelated with master failing too. We're currently not sure yet where they come from. |
Simn
added a commit
that referenced
this pull request
Oct 30, 2026
* Keep TrampolineDispatcher usable after a dispatched object throws The exception still propagates to the caller of dispatch, but the trampoline no longer stays marked as running, which made every later dispatch on that thread queue up forever. Objects that were queued behind the throwing one stay queued and run first on the next dispatch, which keeps FIFO order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Don't lose due scheduler events when dispatching one throws The heap merges events with the same due time into child events of the first one, and ThreadAwareScheduler collects all due events into a list before dispatching them. With a dispatcher that runs events right away, such as TrampolineDispatcher, an exception from one event dropped the rest, e.g. other tasks' delay resumptions, which then never resumed. The exception still propagates immediately, but the events that were not dispatched yet go back into the heap, so the next loop runs them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Don't let a throwing task callback stop the others ThreadSafeCallbacks.run stopped at the first callback that threw. For a task's awaiters, that left the other awaiting tasks waiting forever. For cancellation callbacks, the exception also escaped from cancel() and skipped cancelling the children and the completion check. Every callback now runs. An exception from one is reported by rethrowing it from a separately scheduled event, so it surfaces like any uncaught exception on the task's dispatcher without unwinding through the task machinery. Rethrowing it synchronously after the other callbacks would not be enough, because task completion nests: a child completes its parent before resuming its own awaiters. The same applies to a callback that is registered after the task completed, which used to throw into the caller of onCompletion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Test a throwing completion callback with the default test setup On the targets whose CI runs use a thread pool, this checks that the reported exception only aborts a worker thread and the run carries on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Revert "Don't lose due scheduler events when dispatching one throws" This reverts commit d32d175. #193 fixed the same problem by giving every scheduled event its own heap node and dispatching one event at a time, so the requeue workaround is no longer needed. Its tests are on master now, as part of #193. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * simplify trampoline dispatch * catch dispatch exception in LuvDispatcher too * use @:allow instead of @:privateAccess --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
This replaces the child-event grouping in
MinimumHeapwith a plain binary heap that gives every event its own node. The schedulers now dispatch one event at a time.Why
The heap used to attach events with the same due time to an existing node as child events. This had three problems:
TrampolineDispatcher, an exception from one event dropped the others due at the same millisecond. Those could be other tasks'delayresumptions, which then never resumed. Don't let exceptions from callbacks strand unrelated work #191 works around this with arequeuecallback. This PR fixes it at the source instead.insertsearched subtrees for an event with the same due time, and undid the sift when it found one. That made insertion O(n) in the worst case.What changes
MinimumHeaporders events by due time, then by an insertion sequence number. Equal due times now always come out in insertion order.ThreadAwareSchedulermoves the events from the thread queues into the heap, then dispatches the due ones from there. It used to collect them into a linked list of its own, which lost the rest of the list when one event threw.ScheduledEventlosesaddChildEvent, its child events andnext.Benchmarks
The new benchmarks are:
scheduler/yield_burst: many coroutines that onlyyield(), run throughCoroRun.run.scheduler/heap_same_timeandscheduler/heap_mixed: the heap on its own, on aVirtualTimeScheduler. The first uses one due time for all events, the second spreads them over 1000.The results are in ops/sec, from single runs on one machine. Other benchmarks varied by up to about 10% between runs.
Int64comparisons are expensive on eval, neko and js.yield_burstmoves between −16% and +4%. That is within the noise of single runs.Alternative I tried: an event whose due time equals the previously inserted event's could join that event's FIFO group. Extraction would still take one event at a time, so it stays exception-safe, and it never searches the tree.
It brings back the same-time speed, but every new due time then allocates a group, so mixed due times get slower than in this PR. End to end it's within noise as well. So I kept the simpler plain heap. The variant is about 30 more lines, if we ever want it.
Tests
schedulers.TestMinimumHeapchecks ordering and FIFO among equal due times, with random due times and with interleaved inserts and extracts.schedulers.TestSchedulerExceptionscomes from Don't let exceptions from callbacks strand unrelated work #191. With this PR it passes without therequeuechange, so Don't let exceptions from callbacks strand unrelated work #191 should drop its scheduler commit once this is merged.🤖 Generated with Claude Code