fix(portal): repaint the divider overlay only when it could have changed
Status: rebased onto main (623e5bcbb80) as a linear 4-commit stack, no merge commits. Three conflicts, all in Sources/TerminalWindowPortal.swift (upstream extracted SplitDividerOverlayView into its own file in a9547067ddb and moved split-terminal sync into a transaction in a9547067a95): the overlay's paint helpers now live in Sources/TerminalWindowPortalDividerOverlayView.swift (plus the new RenderInputs gate inputs), and the per-entry sync keeps upstream's deferDividerOverlay branch while running this PR's geometry refresh only outside batch syncs. Local clean build blocked by an unrelated Xcode 27 issue that reproduces on pristine main (CodexTurnRestoreIntentPolicy.swift cannot see RestorableAgentProcessLiveness from the CmuxWorkspaces package); CI is the arbiter.
Reviewer's guide
- Read
ensureDividerOverlayOnTop,refreshDividerOverlayIfGeometryChanged,markDividerOverlayNeedingDisplayandSplitDividerOverlayView.RenderInputsinSources/TerminalWindowPortal.swift. The gate compares the overlay's own render inputs, so it cannot drift from whatdrawpaints. - Tests:
cmuxTests/PortalDividerOverlayRepaintTests.swift. Red-then-green: the first commit adds the two failing tests, the second the fix. - Skip:
Sources/Debug/RemoteTmux/TerminalWindowPortal+DebugDiagnostics.swift(the counter).
Summary
The divider overlay repainted on every portal sync, including syncs that changed nothing, and each repaint walks the whole window view tree.
ensureDividerOverlayOnTop() ended with an unconditional dividerOverlayView.needsDisplay = true. The two guards above it are careful, touching the view only when the overlay's superview, z-order, or frame is actually wrong. Then it repainted anyway.
SplitDividerOverlayView.draw walks the entire window view tree from contentView looking for NSSplitViews, and only consults dirtyRect after that walk, so a one-pixel dirty region still costs a full traversal. synchronizeHostedView calls it once per hosted view per geometry tick.
This gates the invalidation on whether what the overlay draws could have moved.
Measurement
20-second sample of cmux 0.64.22 sitting idle, no window interaction:
| Frame | Samples |
|---|---|
SplitDividerOverlayView.collectDividerSegments | 34 |
SplitDividerOverlayView.draw (self) | 4 |
Main thread took 11527 samples, 10748 of them parked in mach_msg2_trap. Of the 779 that were real work, the divider walk was 34. That is 4.4%, and the heaviest single cmux frame on the main thread in an app doing nothing.
34 vs 4 also says the walk is 89% of the repaint, so the fix has to avoid the walk, not the fill.
The gate is cheaper than what it prevents: in a second sample under load, externalGeometrySignature() cost 2 samples against collectDividerSegments's 14.
The regression test measures the same thing directly. Three syncs of a hosted view that has not moved:
| Repaints | |
|---|---|
| before | 6 |
| after | 0 |
Six, not three, because each synchronizeHostedViewForAnchor reaches ensureDividerOverlayOnTop twice.
What the gate compares
shouldRenderOverlay paints a segment only where a portal-hosted surface crosses that divider's centerline, and those rects come from hostedFramesLikelyToOccludeDividers(). So the gate asks the overlay for its own render inputs: its bounds, plus exactly the frames that helper returns. One source for both, so the comparison cannot drift from the drawing.
Dragging a divider resizes the surfaces on either side, which changes those frames. Adding, closing, hiding or revealing a split changes which frames the helper returns at all. Bounds and hosted frames are window-relative, so moving the window by its titlebar still costs nothing, which is the property the window-move echo storm taught this file to protect.
An earlier revision of this PR compared ExternalGeometrySignature instead, and CodeRabbit was right to flag it. That type records hosted frames, while the helper above also filters out hidden and windowless surfaces, and a hidden entry keeps its frame on purpose. Hiding a surface therefore compared equal and left stale divider pixels. Widening ExternalGeometrySignature would have fixed the comparison at the cost of making every hide and reveal escalate to a full layout pass in synchronizeLayoutHierarchy, so the overlay got its own inputs instead.
Appearance is the one input no geometry comparison can see, since overlayDividerColor resolves each split view's dividerColor against the current appearance. viewDidChangeEffectiveAppearance repaints for that.
Tests
Red-then-green per the repo policy. The first commit adds the first two against today's unconditional invalidation, so CI shows the idle test failing before the fix lands.
testRedundantHostedViewSyncDoesNotRepaintDividerOverlay: three syncs of an unmoved hosted view must not invalidate.testMovedHostedViewRepaintsDividerOverlay: the other direction. A gate like this can fail by never invalidating at all, which would leave divider lines painted at stale positions after a split resize.testHidingHostedViewWithoutMovingItRepaintsDividerOverlay: hides a surface throughupdateEntryVisibilityand asserts the frame did not move, so it fails if the repaint ever comes from something other than the visibility change.
They settle with a deadline-bounded poll rather than a fixed wait: sync until a sync stops producing repaints, bounded at 50 attempts. realizeWindowLayout ends in a 50ms run-loop spin, and layout settling after that on a loaded worker would repaint inside the measured window and fail a test for the wrong reason.
The counter they assert on, RemoteTmuxSizingDiagnostics.dividerOverlayRepaintCount, sits with the existing sizing counters because it guards the same class of bug.
Related
Fourth idle-work fix in this area, all found in the same profiles:
- #10856: portal ownership scanned every pane's tabs to answer an identity question
- #10351: spinner titles woke the app on every frame
- #10349: focus-history entries resolved more than once per menu snapshot
- #10353: closed-item history grew unbounded
https://claude.ai/code/session_01Bj791kgph6Xk9CJf9c41Db
Summary by CodeRabbit
-
Bug Fixes
- Prevented unnecessary divider overlay repaints when hosted-view placement and geometry remain unchanged, reducing redundant rendering work.
- Ensured divider overlays repaint when geometry or visibility changes, keeping visual dividers accurately aligned.
- Updated overlays when the app’s appearance changes.
-
Tests
- Added coverage confirming redundant synchronization does not trigger repaints.
- Added coverage verifying resizing or hiding a hosted view triggers the expected repaint.