perf: make TabItem a reference so one title write wakes one tab

Merged
ST
2mo ago
+173-15

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:

MeasurementValue
Total process CPU6726 ms (34% of one core)
SwiftUI graph updates65% of it
Graph update episodes400, about 19.5 per second
Median episode9 ms on the main thread
Episodes that touched the tab bar294 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

  • TabItem already declared == and hash(into:) by id alone, so identity comparisons behave exactly as before.
  • Codable was already hand-rolled with explicit CodingKeys, init(from:), and encode(to:). Only required was added.
  • updateTab is the only place in the package that writes a tab field, and no local copy of a TabItem is mutated anywhere. Nothing depended on value semantics.
  • PaneState and BonsplitController are already @Observable, so this brings TabItem in 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 - a withObservationTracking block reading pane.tabs, standing in for TabBarView.body, fires on a title change.
  • testChangingOneTabTitleInvalidatesThatTab - a block reading tab.title, standing in for TabItemView.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.


View with [code]smith Autofix with [code]smith
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.

  • TabItem is now an @Observable final class, and updateTab writes through the reference it already holds instead of the tabs array; observation delivers the change to the one TabItemView that reads it.
  • Insert, remove, and move still publish through tabs, which TabBarView needs for layout.
  • Equality and hashing stay by id, and Codable remains hand-rolled (only required added); 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.