Skip to content

perf(grid): copy, type-select and format changes walk every row on the main thread #4531

Backlog

Description

@datlechin

Problem

On a large result, several grid actions do work in proportion to every row, or every selected row, on the main thread, and the window stops responding until they finish. Estimates from the code, not yet measured:

  • Copy of a 50,000-row selection, 20 columns: 0.5 to 3 s. 50,000 x 200 columns: several seconds or more.
  • Typing a letter that matches nothing in a 1M-row grid (type-select): 0.5 to 2 s, and it evicts the visible rows' cached text.
  • Display As on a column of a 1M-row result: about 100 to 250 ms.
  • After Select All on 1M rows, each reload or row-set change resolves the selection row by row: about 20 to 60 ms.
  • JSON or Map mode after Select All on 1M rows: about 50 to 150 ms per SwiftUI pass, and passes are frequent (perf(tabs): one tab change invalidates the views of every tab in the window #4529).

The rest of the whole-result work on main folds into #4521 and is not repeated here: the value filter (apple/TablePro/Models/Query/GridDisplayOrderResolver.swift:12-36, run twice on a reread through apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsMutation.swift:70-79), in-memory sort of held rows and of the table listing (apple/TablePro/Models/Query/TableRowsSorting.swift:17-45, MainContentCoordinator+TableRowsMutation.swift:215, apple/TablePro/Views/Structure/HeldRowsGridAdapter.swift), find next and previous over loaded rows (apple/TablePro/Core/DataGrid/Engine/GridFindEngine.swift:419-443), and the row inspector's select-all (apple/TablePro/Views/Main/Extensions/MainContentView+EventHandlers.swift:220-222, apple/TablePro/Models/UI/MultiRowEditState.swift:175-201). In #4521 those become store operations off the main thread.

Cause

  • Copy. copyRows materializes every selected RowID (apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift:61-88). writeTSV copies each row's values into a new array, formats every cell to a String, joins the lines and, with headers, builds HTML (DataGridCommands+Copy.swift:107-127, formatRowValues at :172-184). ClipboardService.writeRows sets the text twice and then setGridRows runs JSONEncoder over the whole [[PluginCellValue]] payload for the app's private type on every row copy (apple/TablePro/Core/Services/Infrastructure/ClipboardService.swift:145-170). All of it is eager and on main, though a paste reads one type. copiedRows cuts the selection at 50,000 rows (DataGridCommands+Copy.swift:46-51, apple/TablePro/Core/Services/Query/RowOperationsManager.swift:10) with only a log line, which hides the cost from the user.
  • Type-select. The main pane allows type-select (apple/TablePro/Views/DataGrid/DataGridPane.swift:62), and the grid implements only tableView(_:typeSelectStringFor:row:) (apple/TablePro/Views/DataGrid/DataGridViewController+TableDelegate.swift:46-63). AppKit then asks for each row's string until one matches. Each call reads the row, formats through presenter.displayText, which on a miss inserts a box into the 50,000-row RowDisplayCache (apple/TablePro/Core/DataGrid/RowDisplayBox.swift:94), and reads model.store.displayFormats, which for a query tab finds and copies the tab (apple/TablePro/Views/Main/Child/QueryTabGridStateStore.swift:145-152).
  • Display As. redrawFormatted reports .rowsUpdated(IndexSet(0..<rowCount), columns:) (apple/TablePro/Core/DataGrid/DataGridModel.swift:358-374). refreshRowState maps every display index to a RowID and calls updateVisualIndex for all of them (apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift:157-161), and RowVisualIndex.updateRow asks the change manager per row (apple/TablePro/Views/DataGrid/Cells/RowVisualIndex.swift:37). No pending change moved; only the text format did.
  • Selection runs. RowIDRuns keeps a select-all as one run, but GridSelectionSnapshot.resolved expands it through order.displayIndexes(of:) (apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift:106-125), which maps every id and builds an IndexSet from an array (apple/TablePro/Core/DataGrid/Engine/DataGridRowOrder.swift:85-87). restoreSelection runs it on every reload and row-set change (DataGridModel.swift:274-281).
  • JSON and Map. The readers pass Set(model.selectedRowIDs) and changeManager.defaultedCells(in: rows) inside DataGridModelReader (apple/TablePro/Views/Main/Child/MainEditorContentView.swift:679-708). selectedRowIDs builds an array of every selected id (DataGridModel.swift:223-225). ResultsJsonView puts that Set in its RenderKey, compares it on every pass through .task(id:) and converts it back to indices on main (apple/TablePro/Views/Results/ResultsJsonView.swift:55-80, :103, :204).

Change

  • Copy: on main, take a snapshot of the rows and the selection as RowIDRuns. Build the text in a @concurrent task that walks the runs, and write it when it lands if the pasteboard's changeCount has not moved. Publish the HTML and the private grid payload through NSPasteboardItem with an NSPasteboardItemDataProvider, AppKit's lazy pasteboard data, so they are built only when a paste asks for that type. Encode the private payload in a binary form, not JSON. With the work off main, remove the 50,000-row cut: the user's selection is what gets copied.
  • Type-select: implement tableView(_:nextTypeSelectMatchFromRow:toRow:for:), the documented hook for custom type-select, so AppKit asks once per search, not once per row. Compare the active column's raw text for plain columns and format with a local formatter only for formatted columns. Never insert into RowDisplayCache from this path, and read the display formats once per search. After perf(results): hold every Mac result in the core's columnar store #4521 this is a store prefix search.
  • Display As: give a reformat its own grid change that repaints the visible rows' column band and leaves RowVisualIndex alone. refreshRowState updates the visual index only for rows whose change state moved, never for a full-range repaint.
  • Selection runs: resolve each run to a display range (arithmetic when the order is unfiltered, the inverse index when filtered) instead of expanding ids. Publish the model's row runs once per change.
  • JSON and Map: key the task on selectionRevision, displayRevision and the change manager's revision, pass RowIDRuns into the @concurrent render, and compute the selected indices and defaulted cells there.

Measure

  • Copy 50,000 x 20 and 50,000 x 200 selections, and Select All then copy on 1M rows: time to return from copy(_:) and main-thread hang duration (Hangs instrument). Paste into TablePro, TextEdit and a spreadsheet to check every type still arrives.
  • Type an unmatched letter in a 1M-row grid: main-thread samples under the type-select path, and a Debug counter of RowDisplayCache insertions per keystroke. Target: zero insertions.
  • Change Display As on a timestamp column of a 1M-row result: signpost around apply(.rowsUpdated) and refreshRowState. Target: one frame.
  • Select All on 1M rows, then Refresh, Shift+Arrow and a switch to JSON mode: signposts around restoreSelection and the JSON render key, and SwiftUI body counts.
  • Target for every action above: no main-thread hang of 250 ms or more at 1M rows.

Part of the performance plan; depends on #4521.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
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