perf: make TabItem a reference so one title write wakes one tab
A tab title change currently re-renders the whole tab bar. This makes it re-render the one tab that changed.
PaneState.tabs is an @Observable property and TabItem is a value, so this line in updateTab:
pane.tabs[tabIndex].title = title
writes the array rather than the element. Observation invalidates every reader of tabs, which is all of TabBarView.body: visibleTabEntries, every TabItemView, tabScrollContent, and splitButtonRow.
Nothing observes the individual tab either. TabItemView gets let tab: TabItem by value, so the title it renders is a copy and no view is subscribed to it. Redraws happen only because the parent rebuilds.
What it costs
The host is cmux, where agent tools write a spinner frame into the terminal title. Each frame is a title change. Instruments Time Profiler, 20 seconds, 8 animating tabs:
| Measurement | Value |
|---|---|
| Total process CPU | 6726 ms (34% of one core) |
| SwiftUI graph updates | 65% of it |
| Graph update episodes | 400, about 19.5 per second |
| Median episode | 9 ms on the main thread |
| Episodes that touched the tab bar | 294 of 400, 73.5% |
The sidebar in the same profile is 0.6%, so the tab bar is where the time actually goes.
The change
TabItem becomes an @Observable final class, and updateTab writes through the reference it already had:
let currentTab = pane.tabs[tabIndex]
...
-pane.tabs[tabIndex].title = title
+currentTab.title = title
pane.tabs is now only read there, so readers of the array are not invalidated. Observation delivers the title to the TabItemView that reads it, which is the view that has to redraw anyway.
tabs still changes on insert, remove, and move. Those change layout, and TabBarView does need to see them.
Why the diff is small
TabItemalready declared==andhash(into:)byidalone, so identity comparisons behave exactly as before.Codablewas already hand-rolled with explicitCodingKeys,init(from:), andencode(to:). Onlyrequiredwas added.updateTabis the only place in the package that writes a tab field, and no local copy of aTabItemis mutated anywhere. Nothing depended on value semantics.PaneStateandBonsplitControllerare already@Observable, so this bringsTabItemin line with the models around it.
The whole diff is 35 added lines and 15 removed, most of it the doc comment.
Tests
Two commits: the first adds the tests and is red, the second makes them green. Check out 24bdd8f to see them fail.
Red on the first commit:
testChangingOneTabTitleDoesNotInvalidateTheTabsArray- awithObservationTrackingblock readingpane.tabs, standing in forTabBarView.body, fires on a title change.testChangingOneTabTitleInvalidatesThatTab- a block readingtab.title, standing in forTabItemView.body, does not fire, and the tab it was handed still reads the old title.
Green on both commits, so they guard the parts that were already right:
- A sibling tab is not invalidated by another tab's title.
- Adding a tab still invalidates the array.
- Removing a tab still invalidates the array.
- Writing an unchanged title wakes nobody.
Full suite: 227 XCTest and 27 swift-testing tests, no failures, no changes to existing tests.
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Tab title changes now re-render only the affected tab. PaneState.tabs is an @Observable property, so writing pane.tabs[i].title used to write the whole array and invalidate every view that had read it—all of TabBarView, not just the changed tab. On a host whose agent tools animate the terminal title, that meant roughly 20 full tab bar redraws per second.
TabItemis now an@Observable final class, andupdateTabwrites through the reference it already holds instead of thetabsarray; observation delivers the change to the oneTabItemViewthat reads it.- Insert, remove, and move still publish through
tabs, whichTabBarViewneeds for layout. - Equality and hashing stay by
id, andCodableremains hand-rolled (onlyrequiredadded); no public API changes. - Tests pin observation scope: a title change does not invalidate the array or sibling tabs, only the changed tab's readers; insert/remove still invalidate the array; no-op writes wake nobody.