Repository navigation
Limit the threads that many views use to lay out text - #43
Merged
servercimen merged 11 commits intoNov 24, 2026
Merged
Conversation
Each layout engine had a serial queue with no target. GCD gives such a queue a new thread when it has work, so a screen that replaces 20 documents at once held 20 layout threads. The engine queue now targets the global queue at the same QoS. Each engine stays serial, so the order of work for a view does not change. The engines share the pool that GCD keeps at about one thread for each core. The target is the global queue itself: through a concurrent queue of the package, a sync call from the main thread waits until the busy pool is free. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The stream queue now targets the global queue, as the layout queues do. A screen with many streams does not hold a parse thread for each stream. TranscriptStream.flush() still calls sync on the stream queue from the main thread, and it does not wait for a busy pool. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tests start the work of many engines and streams at the same moment. They count the work items that run at once and the threads that run them. They also check that a sync call from a thread outside the pool does not wait for the busy pool: relayout, measure, and the sync call that flush makes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One burst of layouts can go above the bound: when a thread of the pool waits inside Core Text, GCD adds threads. The bound tests now take the run with the fewest items at once out of five runs. The sync tests call relayout, measure and the sync call of flush from a thread outside the pool at the QoS of the main thread. They compare the wait with the time that the busy engines take to finish. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When more queues have work than the pool has threads, the queues wait in turn. A burst of long layouts then held back the parse of streamed text and the short layouts of other views for 300 to 700 ms. Short work now runs at the QoS of the main thread with .enforceQoS, so GCD gives it a thread at once. Short work is a parse of a short text, a short document, or a little text added to the end of the last layout. Long layouts stay at the lower QoS. The work of one view still runs in order. StreamWorker now has parse and applyNow, so that the tests call the code that TranscriptStream uses. The thread measurement test runs only with TRANSCRIPTKIT_TIMING, and the bound tests use shorter documents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -mode many, the benchmark app shows a stream and 20 texts. In each round, all texts get a new long document in the same main-thread turn, and the stream gets a short chunk. The app prints one MANY_RESULT line for each round: the peak thread count of the process, the time until the stream parses its chunk, the time until every text installs its layout, and the longest main-thread frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A stream can leave two blocks outside the storage that its versions share, as at the end of a list. The check that an append is short now allows up to 8 such blocks, so that such appends stay urgent. The short work test now also covers an append that comes within a frame of the last layout. Its drain is scheduled with asyncAfter, and it keeps the higher QoS. The stream bound test uses the long text again, because the shorter text does not fail with no target. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The shared pool did not limit the threads in the first burst after an app launched. On a phone with 6 cores, 20 views that got a long document at once used 12 to 23 busy threads in that burst, about one for each view. GCD starts a thread when a thread of the pool waits, for example while Core Text loads a font the first time. So each queue with waiting work could get a thread again. Long layout drains now go through WorkQueue.asyncLong. At most one long drain for each core runs at a time. The other drains wait outside their queues, oldest first, and do not ask GCD for a thread. A drain that waits keeps its place in the limit, so GCD has no work to start more threads for. No thread waits for the limit. Short drains, the drain that ends fades, and the sync calls do not go through the limit. A drain that waits there takes the newest request when it runs. When a sync call or a short drain took the request first, the drain finds nothing and ends. Each scheduled drain now has an ID, so that an older drain that runs late does not end the schedule of a newer one. Stream parses do not go through the limit, because a later flush must not parse its text before an earlier parse. On a Mac with 14 cores, in a cold test process without the font warm up, 20 engines used 20 threads in 5 of 5 runs before this change, and 14 threads in 9 of 10 runs (15 in 1) after it. With 42 queues of work that waits 50 ms, 42 ran at once before, and 14 after. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LayoutEngine: the engine uses the shared work queue and keeps the pending work count. LayoutEngine.submit: a request for main never schedules a drain, and short work still schedules an urgent drain. LayoutEngine.submit: each scheduled drain counts as work until it has run. LayoutEngine.drain: only the scheduled drain ends the schedule, and a request for main stays for the main thread. LayoutEngine.settleFinishedFades: the urgent work on the shared queue counts as work until it ends. TranscriptStream.flush: the stream applies the text on the shared queue, and then runs its idle actions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Part 13 of a stack of 13 pull requests. The base is #42 (
feature/selection-menu). #31 (part 1) tells how to merge the stack.On this stacked branch,
swift testpasses (394 tests), and the iOS build and build-for-testing pass.Summary
A screen that shows many texts can give all of them new documents at once. Before this change, each view and each stream had its own serial queue with no target. GCD gives such a queue a new thread when it has work, so 20 views used about 20 threads. Refs #10.
TranscriptTextViewandTranscriptStreamnow targetDispatchQueue.global(qos: .userInitiated). They share the system pool of about one thread for each core. Each queue stays serial.synccall does not wait for a layout in the list..userInteractivewith.enforceQoS.-mode many.No public API changes.
Tests
swift teston macOS: 187 tests pass.SharedQueueTestschecks the limit, the thread bound of parses, short work while the pool is busy, andsynccalls during a burst. The tests of the limit, the parse bound and the short work fail when their part of the change is undone.xcodebuild build -scheme TranscriptKit -destination 'generic/platform=iOS'(Xcode 27.2) succeeds with no warnings.Checked on an iPhone 12 mini (6 cores)
A check screen in a local test app gave 20 views long documents (83,512 UTF-16 units) at once, in up to 9 bursts after each cold launch. A busy thread is a thread, other than the main thread, that used 5 ms of CPU or more in a burst. The limit, set before the runs, was 12.
synccalls did not hang, and a resize took 94 to 104 ms, against 197 to 205 ms on main. Heights and pixels are the same as on main.The thread check ran on main first and showed the problem. The other checks are guards. Round 2 also ran one main control, because the phone was faster that day.
Round 1 found that the first burst after a cold launch still used about one thread for each view (11 to 23 busy threads). GCD adds threads while layouts wait for fonts. The last commit limits long layouts to one for each core, also while a layout waits.
Stand-ins:
thread_infoCPU time takes the place of a system trace, andCADisplayLinkgaps take the place of scroll smoothness. UIKit views take the place of SwiftUI views in a scroll view, so the SwiftUI path is not checked on the phone.Not checked
synccall on a view whose own long layout runs inside the limit. By the code, it waits only for that layout, as on main.Known limits
TranscriptStreamdoes not have this limit.🤖 Generated with Claude Code