Skip to content

perf(sidebar): filtering a 100,000-table catalog runs on the main thread on every key #4532

Done

Description

@datlechin

Problem

Typing in the sidebar filter on a large catalog stalls the window. On 50,000 tables the first key t still stalls 110 to 170 ms on an M4 MacBook Pro (#4433, #4438), and the work is linear in the catalog, so 100,000 tables should cost about twice that (not yet measured). The pacer from #4433 spaces the refreshes out but does not make them cheaper. Nobody has profiled how that time splits between the name scan and the outline reload.

Cause

  • The scan. SidebarNameQuery.tier(of:) bridges each name to NSString and calls localizedStandardRange(of:), a locale-aware case- and diacritic-insensitive search, for every candidate (apple/Packages/TableProMacCore/Sources/TableProUtilities/UI/SidebarNameFilter.swift:24-31). SidebarSearch.ranked runs it over every item (apple/Packages/TableProMacCore/Sources/TableProUtilities/UI/SidebarSearch.swift:56-69), and DatabaseTreeFilter.filteredTables then dedups the matches through a Set of TableIdentity (apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift:208-226).
  • On the main actor, from scratch. SidebarViewModel is @MainActor (apple/TablePro/ViewModels/SidebarViewModel.swift:12). Its memo is keyed on the rows and the query (SidebarViewModel.swift:465-469, :499-509), and the outline's bucket cache is keyed on the search text (apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Nodes.swift:703-715), so every new query rescans the whole catalog, even when ord can only narrow the matches of or. SidebarFilterPacer only decides how long a key waits (apple/TablePro/ViewModels/SidebarFilterPacer.swift:13-29).
  • The nodes. For each matched row outside the kept head and tail, tableNodes builds a new id string with DatabaseTreeNode.tableId(ref) and looks it up in nodeCache (DatabaseTreeOutlineCoordinator+Nodes.swift:890-932, apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift:222-224), and rebuild clears childrenCache (apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift:303-314). Narrowing applies as row removals; the first key, a widening and a clear fall back to a reload (apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+RowChanges.swift:31-39).
  • The bodies redo the filter. SidebarTreeView.visibleSchemas, evaluated in body, calls DatabaseTreeFilter.hierarchicalLoadedContent with its default filter: { $0.filtered() } for every loaded schema, with no memo (apple/TablePro/Views/Sidebar/SidebarTreeView.swift:48-70, DatabaseTreeFilter.swift:343-350). In the flat shape, SidebarView.otherSchemaMatches and attachedSchemaMatches call DatabaseTreeFilter.loadedObjectBuckets, also unmemoized (apple/TablePro/Views/Sidebar/SidebarView.swift:43-100, DatabaseTreeFilter.swift:590). The outline coordinator already holds the same verdicts in bucketMemo and loadedContentMemo (DatabaseTreeOutlineCoordinator.swift:45-46). Any unrelated publish to these views during a search repeats the full filter.
  • SidebarView.init calls SidebarViewModel.shared, which calls updateBindings and assigns three @Published bindings (SidebarView.swift:141-148, SidebarViewModel.swift:48-56, :200-202). Each assignment sends objectWillChange and re-runs the sidebar bodies above, though the comment at SidebarView.swift:148 says nothing observable is written there.

Overlaps #4438: SQ4 (apply a refined search as row changes), SQ6 (cut the row build of a search on a large catalog, the 110-170 ms first key), SQ7 (hover tracking during scroll) and SQ8 (AppKit object tree cells). This issue keeps the scan, the off-main filter, the interned nodes and the duplicate body filters. The node interning below is the row-build part of SQ6; land it once.

Change

  • Fold once. When a catalog buffer lands, build a search index for it off the main actor: each name folded with folding(options: [.caseInsensitive, .diacriticInsensitive, .widthInsensitive], locale:) and kept as contiguous UTF-8, keyed by the same SameRows identity the memos use. Fold the query once per key and match bytes; the tier is the match position (exact, prefix or substring). localizedStandardRange also applies locale equivalences that folding does not, so first pin today's results in SidebarNameFilterTests and SidebarSearchTests (apple/Packages/TableProMacCore/Tests/TableProUtilitiesTests/) with non-ASCII cases (ß, ligatures, Turkish i), then switch.
  • Off main, with cancellation. Run the scan in a @concurrent function over the Sendable index, as QuickSwitcherViewModel.buildTableItems already does (apple/TablePro/ViewModels/QuickSwitcherViewModel.swift:1107-1108). Cancel a superseded pass, apply the result on main only if its query is still current, and when the new query extends the previous one, filter only the previous match indices.
  • One node per table. Build the outline nodes once per catalog load, parallel to the catalog buffer, so a filter result maps match indices to existing nodes with no string building. Write a node's kind only when its drawn fields changed.
  • One owner for the verdicts. Move the bucket memo to SidebarViewModel (or expose the coordinator's), pass it as filter: to hierarchicalLoadedContent and loadedObjectBuckets, and have SidebarTreeView and SidebarView read the memoized verdicts. Compute schemas once per body.
  • Keep the three bindings in SidebarViewModel as plain properties: they are plumbing, not drawn state.

Measure

  • apple/scripts/benchmark-large-catalog.sh sidebar-filter step, with a 100,000-table MySQL fixture from apple/scripts/probes/build-large-catalog-fixtures.sh (TABLES=100000) next to the 50,000 one: median and p95 per key, and the script's main-thread stall check.
  • Target: no main-thread stall over 100 ms on any key at 100,000 tables, the budget the script already applies (Measure the sidebar, Open Quickly and autocomplete on 100 schemas and 50,000 tables #3661), and no scan samples on the main thread.
  • Time Profiler over a scripted o, or, ord, then clear, on 100,000 tables in the flat layout and on 100 schemas of 500 tables in the hierarchical one: main-thread ms split between the scan, tableNodes, rebuild and NSOutlineView. Measure the NSOutlineView part before deciding anything about the flat shape's view.
  • A Debug counter of DatabaseTreeObjectBuckets built per key on a 50-schema hierarchical connection. Target: one per schema per query, not two or more.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 26, 2026

    @datlechin
    MemberAuthor

    #4596 ships the part that stands alone: the ASCII byte search (about 20 ms to 2 ms per key over 100,000 names), one memo of container verdicts for the bodies around the outline, and the bindings as plain properties. Left: the scan in a @concurrent pass with cancellation and narrowing from the previous matches, and the outline nodes built once per catalog load (SQ6 of #4438). The filter's callers, the outline's node build and the SwiftUI bodies, read its verdicts synchronously, so moving the scan off the main actor turns them into an async pipeline, which is its own change. The 100,000-table benchmark-large-catalog.sh run and the Time Profiler split were not taken: the shared Mac ran out of disk during this run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions