Repository navigation
Description
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.
copyRowsmaterializes every selectedRowID(apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift:61-88).writeTSVcopies each row's values into a new array, formats every cell to aString, joins the lines and, with headers, builds HTML (DataGridCommands+Copy.swift:107-127,formatRowValuesat:172-184).ClipboardService.writeRowssets the text twice and thensetGridRowsrunsJSONEncoderover 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.copiedRowscuts 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 onlytableView(_: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 throughpresenter.displayText, which on a miss inserts a box into the 50,000-rowRowDisplayCache(apple/TablePro/Core/DataGrid/RowDisplayBox.swift:94), and readsmodel.store.displayFormats, which for a query tab finds and copies the tab (apple/TablePro/Views/Main/Child/QueryTabGridStateStore.swift:145-152). - Display As.
redrawFormattedreports.rowsUpdated(IndexSet(0..<rowCount), columns:)(apple/TablePro/Core/DataGrid/DataGridModel.swift:358-374).refreshRowStatemaps every display index to aRowIDand callsupdateVisualIndexfor all of them (apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift:157-161), andRowVisualIndex.updateRowasks the change manager per row (apple/TablePro/Views/DataGrid/Cells/RowVisualIndex.swift:37). No pending change moved; only the text format did. - Selection runs.
RowIDRunskeeps a select-all as one run, butGridSelectionSnapshot.resolvedexpands it throughorder.displayIndexes(of:)(apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift:106-125), which maps every id and builds anIndexSetfrom an array (apple/TablePro/Core/DataGrid/Engine/DataGridRowOrder.swift:85-87).restoreSelectionruns it on every reload and row-set change (DataGridModel.swift:274-281). - JSON and Map. The readers pass
Set(model.selectedRowIDs)andchangeManager.defaultedCells(in: rows)insideDataGridModelReader(apple/TablePro/Views/Main/Child/MainEditorContentView.swift:679-708).selectedRowIDsbuilds an array of every selected id (DataGridModel.swift:223-225).ResultsJsonViewputs thatSetin itsRenderKey, 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@concurrenttask that walks the runs, and write it when it lands if the pasteboard'schangeCounthas not moved. Publish the HTML and the private grid payload throughNSPasteboardItemwith anNSPasteboardItemDataProvider, 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 intoRowDisplayCachefrom 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
RowVisualIndexalone.refreshRowStateupdates 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,displayRevisionand the change manager's revision, passRowIDRunsinto the@concurrentrender, 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
RowDisplayCacheinsertions per keystroke. Target: zero insertions. - Change Display As on a timestamp column of a 1M-row result: signpost around
apply(.rowsUpdated)andrefreshRowState. Target: one frame. - Select All on 1M rows, then Refresh, Shift+Arrow and a switch to JSON mode: signposts around
restoreSelectionand 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