Skip to content

Limit the threads that many views use to lay out text - #43

Merged
servercimen merged 11 commits into
feature/selection-menufrom
feature/shared-layout-queue
Nov 24, 2026
Merged

servercimen merged 11 commits into
feature/selection-menufrom
feature/shared-layout-queue

Conversation

@servercimen

Copy link
Copy Markdown
Owner

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 test passes (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.

  • The queues of TranscriptTextView and TranscriptStream now target DispatchQueue.global(qos: .userInitiated). They share the system pool of about one thread for each core. Each queue stays serial.
  • Long layouts have a hard limit: at most one for each active core runs at a time. The others wait in a list outside their queues and hold no thread. A layout that waits for a font keeps its place, so GCD does not start more threads. A sync call does not wait for a layout in the list.
  • Short work goes ahead of long layouts: the parse of a short stream chunk, the layout of a short document or append, and the end of finished fades. It runs at .userInteractive with .enforceQoS.
  • The README tells about the shared pool, the limit and the short work. The benchmark app gets -mode many.

No public API changes.

Tests

  • swift test on macOS: 187 tests pass.
  • SharedQueueTests checks the limit, the thread bound of parses, short work while the pool is busy, and sync calls during a burst. The tests of the limit, the parse bound and the short work fail when their part of the change is undone.
  • iOS build: 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.

  • Threads: main used 20 to 23 busy threads in every burst. The branch used 7 to 9 in the first burst after a cold launch (8 of 8 launches), and 6 to 8 later.
  • Time: in the first burst, all 20 views installed in 575 to 603 ms, against 584 ms on main. The first view installed in 185 to 206 ms, against 530 ms.
  • Guards: the worst main-thread frame in a replace burst was 16.6 to 16.7 ms. Short work and the stream stayed within main + 50 ms. sync calls 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_info CPU time takes the place of a system trace, and CADisplayLink gaps 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

  • A sync call 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

  • When more views have long work than there are cores, a view waits for its turn. Its new text can show later than on main.
  • An app that parses each version of a long text itself does not get urgent layouts. TranscriptStream does not have this limit.
  • Long stream parses are not in the limit. While many of them wait, GCD can still add threads.

🤖 Generated with Claude Code

servercimen and others added 11 commits November 23, 2026 17:18
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>
@servercimen
servercimen added this pull request to stack #44 November 24, 2026 20:57
@servercimen
servercimen removed this pull request from stack #44 November 24, 2026 21:01
@servercimen
servercimen added this pull request to stack #45 November 24, 2026 21:01
@servercimen
servercimen merged commit 158c9f2 into main Nov 24, 2026
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant