Repository navigation
Start main-actor queue tasks on the main actor - #101
Merged
dfed merged 3 commits intoNov 17, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## claude/2.0-14-has-started-all-work #101 +/- ##
=====================================================================
Coverage 100.00% 100.00%
=====================================================================
Files 15 15
Lines 3290 3605 +315
=====================================================================
+ Hits 3290 3605 +315
🚀 New features to boost your workflow:
|
dfed
force-pushed
the
claude/project-thread-2aoan1
branch
from
November 16, 2026 20:41
84566c3 to
8442ac5
Compare
dfed
changed the base branch from
claude/2.0-13-readme
to
claude/2.0-14-has-started-all-work
November 16, 2026 20:41
A task enqueued on `MainActor.queue`, or on a `FIFOQueue` with an operation isolated to the main actor, started on the global concurrent executor and then hopped to the main actor, which costs a job on the main queue plus one on the global executor. A FIFO task also hopped back off of the main actor to end its turn. When a task's turn is current at the moment it is enqueued, start it on the main actor directly. `Turn.begin()` is now `nonisolated(nonsending)` so that a current turn begins without hopping off. FIFO tasks with main-actor operations end their turn on the main actor. A task that has to wait for its turn still starts on the global executor, because a continuation always resumes there whichever executor it waited on. Tasks on other actors are unchanged: a task switches from the global executor to an idle default actor without enqueuing a job, and benchmarks showed starting those tasks on the actor made them slower. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tests that checked a FIFO task ends its turn on the main actor passed on the old code too, so they could not fail every run on the regression they guard. Remove them, and cover the throwing FIFO path for a main-actor operation that waits for its turn. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add a debug-only hook to `Turns` that is called each time a turn ends, and a debug-only `FIFOQueue.init(turns:)` to inject it. Tests record whether each turn ended on the main thread: a task that hopped off of the main actor before ending its turn fails every run. Release builds compile neither the hook nor the tests that use it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dfed
force-pushed
the
claude/project-thread-2aoan1
branch
from
November 16, 2026 23:40
8442ac5 to
b38aaa3
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.
Requested by Dan · project thread
Part of the 2.0 stack, on top of
claude/2.0-14-has-started-all-work(#102).What
Removes executor hops for queue tasks whose operation runs on the main actor:
Task(on: MainActor.queue)/ anyActorQueue<MainActor>Task(on: fifoQueue) { @MainActor in … }(incl. ops that inherit main isolation)Task(on: CancellableQueue<FIFOQueue>)with a main-actor opIf a task has to wait for its turn, it still starts on the global executor. A continuation always resumes on the global executor, whichever executor it suspended on, so waiting on the main actor would cost an extra main-queue job. FIFO main-actor tasks still end their turn on main in that case.
How it works:
Turns.reserve()now records whether the turn was current when it was reserved (Turn.wasCurrentWhenReserved).Turn.begin()is nownonisolated(nonsending), so a current turn begins without hopping off the main actor.Why not other actors?
I first built this for every actor, using a generic way to start a task on any actor. On default actors it made things ~1.5× slower. A task can switch from the global executor to an idle default actor (and back) inline, without enqueuing a job, so that "hop" was nearly free. Starting a task on an actor, by contrast, always costs a real actor enqueue. The main actor and custom executors can't take an inline switch, which is where the saving is. I limited the change to the main actor because there's no public API to tell a default actor from a custom-executor actor. Ending FIFO turns on default actors without hopping back also gave no measurable gain, so I dropped it.
Benchmarks
Release build on an M-series Mac, median of 7. Tasks are enqueued from a non-main task. Numbers are from three alternating old/new rounds; the machine was noisy, so unchanged paths vary ±15%.
MainActor.queue: enqueue + await, one at a time (10k)FIFOQueue@MainActorop: enqueue + await, one at a time (10k)FIFOQueue@MainActorop: enqueue 10k, await lastActorQueue/ FIFO on default actorsDoes the main-actor check slow down other actors?
No. For
ActorQueue, the main-actor path is a separate overload, chosen at compile time. Generic actor queues pick up only an extraBoolthatTurns.reserve()computes under the lock it already holds. FIFO'sisolatedTo:initializers are unchanged. FIFO's@isolated(any)initializers check at runtime: they read the operation's isolation and compare it by pointer toMainActor.shared, which is a couple of loads per enqueue. Over five alternating rounds, the regular-actor paths came out 4–16% faster in the new build. That's noise, since that code didn't change, but it shows no measurable cost.Observable ordering change on the main actor
When a task's turn is free, it now starts as a main-actor job at the moment you enqueue it. Before, it started on the global executor and hopped over later. So a main-actor queue task now runs before main-actor work enqueued after it, such as a plain
Task { @MainActor in }created right afterTask(on: MainActor.queue). Before, it usually ran after that work, though that was never guaranteed. Ordering among the queue's own tasks is unchanged.ActorQueue.hasStartedAllWorkandFIFOQueue.isIdlecan also becometruesooner for main-actor tasks.Tests
Task { @MainActor in }created right after it. The main queue is FIFO, so this passes deterministically only if the task starts on main. These tests fail on the 2.0 tip.begin()on a current turn doesn't suspend or hop off the main actor. This also fails on the 2.0 tip.Turns(didEndTurn:)andFIFOQueue(turns:)exist only under#if DEBUG, so release builds compile neither. The tests recordThread.isMainThreadeach time a turn ends. They cover both the waiting and non-waiting paths, throwing and non-throwing, plusCancellableQueue. I checked that all 6 fail on 3 of 3 runs whenexecuteOnMainActoris changed to end the turn off the main actor. These tests don't run in the release CI jobs.wasCurrentWhenReservedunit tests.swiftformat --lintis clean.Notes for review
.isolationdirectly from asending @isolated(any)parameter inside aTaskextension initializer crashes the Swift 6.4 compiler in SILGen. The smallisolation(of:)helper works around it.throws(Failure)) helper that took anisolated (any Actor)?parameter silently swallowed errors thrown by the operation. The final code doesn't use typed throws.🤖 Generated with Claude Code