Repository navigation
Add a minimum priority to ActorQueue - #97
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## claude/2.0-10-is-idle #97 +/- ##
========================================================
Coverage 100.00% 100.00%
========================================================
Files 14 15 +1
Lines 3120 3294 +174
========================================================
+ Hits 3120 3294 +174
🚀 New features to boost your workflow:
|
dfed
force-pushed
the
claude/2.0-11-actor-queue-priority
branch
from
November 16, 2026 04:56
1626034 to
9ba2419
Compare
dfed
force-pushed
the
claude/2.0-11-actor-queue-priority
branch
from
November 16, 2026 05:13
9ba2419 to
e72e580
Compare
dfed
force-pushed
the
claude/2.0-11-actor-queue-priority
branch
from
November 16, 2026 11:40
e72e580 to
1bf79f4
Compare
Owner
Author
|
Correction: the CodeQL Swift failure I reported here is fixed by Generated by Claude Code |
In 1.x, an `ActorQueue` ran every operation inside one long-lived task, at the priority of the code that created the queue. In 2.0, each `Task(on: actorQueue)` is its own task, so an operation enqueued from low-priority code runs at low priority, and an `ActorQueue` had no way to give its tasks a priority of its own. `ActorQueue(priority:)` works like `FIFOQueue(priority:)`: it gives each of the queue's tasks at least that priority. A task that would run at a lower priority runs at the queue's priority instead, and a task enqueued at a higher priority keeps it. All four `Task(on:)` initializers for `ActorQueue` use it, and so do the four for a `CancellableQueue` that wraps an `ActorQueue`; their documentation now says so. `ActorQueue()` still compiles and gives its tasks no minimum priority. `MainActor.queue` has none either, which its documentation now says. The README's priority section now points `ActorQueue` users to `ActorQueue(priority:)`. `FIFOQueue`'s code that works out a task's priority moves unchanged into an internal `MinimumPriority` type that both queues use, so `FIFOQueue` behaves exactly as before and its tests are unchanged. Without a minimum priority, the `ActorQueue` enqueue path adds one check of an optional. A task that runs at the queue's priority is escalated like any other task. New tests cover the minimum priority in each `ActorQueue` initializer, the priority a task inherits inside and outside of a task, and escalation of a task that runs at the queue's priority. The existing test of an explicit priority on an `ActorQueue` now covers four priorities in all four initializers, as `FIFOQueue`'s does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM
dfed
force-pushed
the
claude/2.0-11-actor-queue-priority
branch
from
November 16, 2026 23:40
1bf79f4 to
70d113b
Compare
This was referenced Nov 17, 2026
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.
What
Adds
ActorQueue(priority:), which gives each of the queue's tasks at least that priority, asFIFOQueue(priority:)already does.ActorQueue()still compiles and gives its tasks no minimum priority.Why
In 1.x, an
ActorQueueran every operation inside one long-lived task, so operations ran at the priority of the code that created the queue. In 2.0, eachTask(on: actorQueue)is its own task and runs at the priority of the code that enqueues it, so an operation enqueued from.backgroundcode runs at.background. Before this change, the only way around that was to passpriority:at every call site. AFIFOQueuecan be given a priority once, when you create it, and now anActorQueuecan too.Semantics
These match
FIFOQueueexactly:nilfrom lower-priority code, runs at the queue's priority.nilfrom higher-priority code, keeps its priority.nil, which is the default, changes nothing.Task(on:)goes toTask.init(name:priority:)unchanged, soTaskworks out the task's priority itself.ActorQueueTask(on:)initializers use the queue's priority: the ones for your own actor and the ones for the main actor, each nonthrowing and throwing.Task(on:)initializers for aCancellableQueuethat wraps anActorQueueforward to those, so they use it too, as theFIFOQueue-backed ones already did.MainActor.queuehas no priority of its own and is unchanged. Its documentation now says so, and tells callers to passpriority:instead.Implementation
FIFOQueue's code that works out a task's priority moves unchanged into a new internalstruct MinimumPriorityinSources/AsyncQueue/Utilities/MinimumPriority.swift, and both queues store one. That includespriorityOfNewTask(enqueuedWith:), the copy ofTask's rules for the priority a new task inherits:.mediumif the thread has none;.high.The function bodies are the same code, so
FIFOQueuebehaves exactly as before.FIFOQueueTestsare unchanged and pass.ActorQueue.init()becomesinit(priority: TaskPriority? = nil), the same shape asFIFOQueue's.ActorQueue(),ActorQueue<T>(),MainActor.queue, the README examples and every test that creates a queue compile unchanged. The only call that stops compiling is a reference to the initializer as a() -> ActorQueuefunction value, such asActorQueue<T>.initpassed as a closure, which this 2.0 release can accept.Docs
ActorQueue.init(priority:)documentation usesFIFOQueue.init(priority:)'s wording.priorityparameter of each of the fourActorQueueTask(on:)initializers adds the same sentence asFIFOQueue's: "If the queue was created with a higher priority, the task runs at the queue's priority instead."priorityparameter of each of the fourCancellableQueueTask(on:)initializers that take aCancellableQueue<ActorQueue<…>>, matching the fourFIFOQueue-backed ones.MainActor.queuedocuments that it has no priority of its own.FIFOQueue(priority:)andActorQueue(priority:)give each of the queue's tasks at least that priority, and thatMainActor.queuehas no priority of its own.Tests
The new tests mirror
FIFOQueue's tests ofFIFOQueue(priority:). Each one readsTask.currentPriorityinside the operation, throughpriorityOfTask(enqueuedFromTaskWithPriority:)or a stream. No test awaits the enqueued task before it reports, because awaiting would raise its priority. The tests that don't involve escalation assert exact priorities. The two escalation tests assert the starting priority exactly, and the raised priority with>= .high, like every other escalation test inActorQueueTestsandFIFOQueueTests. None of them sleeps, times out or polls.New tests in
ActorQueueTests:task_onQueueWithPriority_withLowerPriority_executesAtQueuePriority:[.medium, .medium, .medium, .medium]across the four initializers.task_onQueueWithPriority_withHigherPriority_executesAtThatPriority:[.high, .high, .high, .high].task_onQueueWithPriority_withNilPriority_executesAtHigherOfCallersAndQueuePriority:.mediumfrom.backgroundand.highfrom.high, in all four initializers.task_onQueueWithPriority_enqueuedFromEscalatedTaskWithLowerBasePriority_executesAtQueuePriority: the queue's priority is compared with the enqueuing task's base priority, not its escalated priority.task_onQueueWithPriority_enqueuedOutsideOfAnyTask_executesAtHigherOfNewTaskAndQueuePriority: enqueuing from aThreadwith each quality of service.task_onQueueWithPriority_withUnspecifiedPriority_executesAtHigherOfCallersAndQueuePriority:TaskPriority(rawValue: 0).task_onQueueWithUserInteractivePriority_enqueuedFromUserInteractiveTask_executesAtQueuePriority: the lowering of an inherited user-interactive priority. This test asserts==whereFIFOQueue's uses>=, which is equivalent because nothing ranks above user-interactive.task_onQueueWithPriority_isEscalatedWhenAwaitedFromHigherPriorityTask: the task starts at exactly the queue's priority,.low, and awaiting it then raises it to.highor above.throwingTask_onQueueWithPriority_isEscalatedWhenItsTurnBeginsWhileHigherPriorityTaskWaits: a task that runs at the queue's priority is raised to.highor above by a task waiting behind it.A queue without a minimum priority is
ActorQueue(), which the existing tests already use, so it needs no new tests. The existingtask_withNilPriority_inheritsCallersPrioritycovers it in all four initializers.task_withPriority_executesAtThatPriority, which checked only.lowin two initializers, now hasFIFOQueue's shape:.backgroundthrough.high, enqueued from a.hightask, in all four initializers.No separate
MinimumPriorityunit test was added: the queue tests run every line of it through both queues. NoCancellableQueueTestscase was added either, since theCancellableQueueinitializers only forward to the queue's own, andFIFOQueue(priority:)has none there.Mutants
Each mutant below was built, and the 10 new and changed
ActorQueueTestswere run 10 times against it: 7 runs unpinned and 3 pinned to one CPU withtaskset -c 0. Each mutant failed all 10 runs, and no run hung.ActorQueueignores its priority (MinimumPriority(nil))<flipped to>, so the lower of the two priorities winsTask.currentPriorityinstead ofTask.basePriority ?? Task.currentPriorityTask(on:)dropped when the queue's priority doesn't applytask_withPriority_executesAtThatPriority.mediumtask_withPriority_executesAtThatPrioritythrowingTask_onQueueWithPriority_isEscalatedWhenItsTurnBeginsWhileHigherPriorityTaskWaitsThe mutants of
MinimumPrioritychangeFIFOQueuetoo. These runs used only theActorQueueTestslisted above;FIFOQueueTestsare unchanged and run every line ofMinimumPriorityas well.Checks
All checks used Swift 6.4.2 on Linux, in release mode.
-warnings-as-errors.SourcesorTests.--maximum-repetitions 100 --repeat-until failon the 10 new and changedActorQueueTestspassed unpinned and pinned to CPU 0.SourcesorTeststhat never runs. Every line ofMinimumPriority.swiftruns.Mutexreplaced by a pthread-based lock. On Linux, ThreadSanitizer flagsSynchronization.Mutexwhen two threads contend for it, because it can't see the lock's slow path in the prebuilt standard library.-warnings-as-errors.Benchmark
Each round runs 20,000 enqueues with every
Task(on:)initializer ofFIFOQueueandActorQueue, for each build, in a shuffled order, each in its own process. The builds are the base branch, a second copy of it as an A/A control, theisIdlechange, and this change. The ratios below are the medians, over rounds, of each build's time divided by the base build's time in the same round. "Enqueue" is the time spent in the loop ofTask(on:)calls. "Total" also includes waiting for the last task to finish.Pinned to CPU 0, 60 rounds:
FIFOQueueFIFOQueue, throwingFIFOQueue,isolatedTo:FIFOQueue,isolatedTo:, throwingActorQueueActorQueue, throwingActorQueue<MainActor>ActorQueue<MainActor>, throwingUnpinned, 40 rounds, the enqueue ratios for this change were 0.988 to 1.021, against 0.980 to 1.036 for the A/A control. Allocations per enqueue are the same as on the base branch.
The total time of the two
MainActorcases moves by a few percent, in either direction, between builds of the same library code, so the lower totals in the table are not a speed-up:ActorQueueon the base code slowed the nonthrowingMainActorcase by about 3%. An unused 8-byte field, and changes to code alone, did not.Without a minimum priority, the
ActorQueueenqueue path adds one check of an optional.ActorQueuegains one storedTaskPriority?, andFIFOQueuestores the same value as before.Series
Stacked on #96, the
isIdlechange, which is stacked on #94. The two changes are independent: they touch the same files, but different lines. #99, which adds the macOS Thread Sanitizer and iOS 18 simulator jobs, is stacked on this PR.🤖 Generated with Claude Code
https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM