perf: memoize split-button SF Symbol resolution

Merged
ST
2mo ago
+158-0

This takes a symbol-catalog lookup out of the tab bar's render path - 219ms of a 20-second profile, 3.3% of the process's CPU. No behavior change: same glyphs, same sizes, same rotations.

TabBarStyling.splitActionSystemImage(for:) decided whether a name was a real SF Symbol by allocating an NSImage and discarding it:

if NSImage(systemSymbolName: name, accessibilityDescription: nil) != nil {
    return SplitActionSystemImage(name: name, rotationDegrees: 0, pointSize: 12)
}

splitActionButtonIcon calls that once per split button, and TabBarView.body calls splitActionButtonIcon. So every body evaluation paid one catalog lookup per button.

What it costs

Instruments Time Profiler, 20 seconds, attached to a host app whose tab titles carry agent spinners. That workload re-evaluates the tab bar about 20 times a second.

MeasurementTime
Total process CPU sampled6726 ms (34% of one core)
Inside SF Symbol image creation219 ms, 3.3% of total
└ +[NSImage(NSSymbolImages) _imageWithSymbolName:inCatalog:...]140 ms
└ -[_NSSimpleLRUCache objectForKey:creationBlock:]83 ms

The stack it sits under:

TabBarView.splitButtonRow.getter
  TabBarView.splitActionButton(_:tooltips:)
    Button.init(action:label:)
      NSImage.__allocating_init(systemSymbolName:accessibilityDescription:)
        +[NSImage(NSSymbolImages) _imageWithSymbolName:inCatalog:variableValue:]
          -[NSImage bestRepresentationForHints:]
            -[_NSSimpleLRUCache objectForKey:creationBlock:]

AppKit already caches symbol images. _NSSimpleLRUCache is that cache, and most of the cost here is just getting to it.

The change

Whether a name is a real SF Symbol is a pure function of the name, and the answer cannot change while the process runs. SplitActionSystemImageCache now remembers it, behind an NSLock, following SplitActionButtonImageCache in the same file. It resolves outside the lock, so a symbol lookup never blocks another caller. Two threads racing on one name both resolve and agree.

Measured over 2000 lookups of the same name:

PathPer lookup
Uncached~27 µs
Cached~0.1 µs

The dictionary holds at most 256 entries. Names come from host configuration, so the live set is a handful and never turns over. The bound is there so a pathological host cannot grow it without limit.

Tests

Four new tests in SplitActionSystemImageCacheTests. Each builds its own cache instance instead of reaching for .shared, so they do not race each other under parallel execution.

  • Repeated lookups of four names resolve exactly four times.
  • Memoizing returns what the uncached resolver returns, across all three branches: a real symbol, the rotated ellipsis.vertical special case, and the questionmark.circle fallback.
  • TabBarStyling.splitActionSystemImage(for:) goes through the cache rather than around it.
  • A repeat lookup costs at least ten times less than resolving. The bound is loose on purpose so the test still means something if the cache gets replaced later. Measured gap is closer to 280x.

Full suite green: 221 XCTest tests and 31 swift-testing tests. The four new ones are the only additions; nothing existing changed.

Summary by CodeRabbit

  • Performance
    • Improved tab-bar efficiency by reusing split-action symbols during repeated display updates, reducing unnecessary image creation.
  • Bug Fixes
    • Preserved the existing symbol selection and fallback behavior for split actions.