Skip to content

perf(autocomplete): size the completion popup before showing it and update rows in place #4541

Done

Description

@datlechin

Problem

While typing about 420 keys in the 1.9M-character file, SwiftUI took 980 ms of main-thread CPU, almost all of it the completion popup (CodeSuggestionLabelView, SuggestionContentView), and SuggestionController.updateWindowSizeFromContent took another 99 ms of main-queue time (Time Profiler, typing-1900k). That is about 2.6 ms per key spent on a list of at most a few hundred short rows, of which about nine are visible.

Cause

  • Every key with the popup open re-ranks and assigns a new items array (apple/Packages/TableProEditor/Sources/TableProEditorKit/CodeSuggestion/Model/SuggestionViewModel.swift:267-274).
  • A Combine sink on model.$items with .receive(on: DispatchQueue.main) resizes the window on a later main-queue turn (apple/Packages/TableProEditor/Sources/TableProEditorKit/CodeSuggestion/Window/SuggestionController.swift:75-80). updateWindowSizeFromContent reads hostingView.fittingSize, a full SwiftUI layout of the panel, on every change (apple/Packages/TableProEditor/Sources/TableProEditorKit/CodeSuggestion/Window/SuggestionController+Window.swift:62-68).
  • On first show, the items are set and showWindowOnParent orders the panel front in the same turn (SuggestionViewModel.swift:174-183, SuggestionController.swift:190), so it is placed with the size the last session left, and the real size lands one turn later. Whether a wrong-size frame reaches the screen depends on whether that turn runs before the Core Animation commit; not measured.
  • The size does not need a layout pass: the list's width and height are already computed from the items alone (apple/Packages/TableProEditor/Sources/TableProEditorKit/CodeSuggestion/View/SuggestionContentView.swift:100-117, fixed rowHeight at :12). Only the preview below the list has a content-dependent height.
  • Rows use ForEach(Array(model.items.enumerated()), id: \.offset) (SuggestionContentView.swift:52), and the model notes that entries carry no identity (SuggestionViewModel.swift:335). A re-rank gives every visible row new content under the same identity, so SwiftUI rebuilds each row's label and detail text on every key.
  • The list sits in a ScrollView and LazyVStack, not a List, on purpose: a focusable table view made the panel ask to become key (SQL editor lost focus after tying 'se'. #1885, SuggestionContentView.swift:43-47). Any replacement must keep the panel out of focus.

Change

  • Compute the panel size from the items when they are assigned: width and list height from the existing formulas, plus the preview's measured height only when the selected item has a preview. Apply it before orderFront, in the same turn, and drop the receive(on:) hop and the fittingSize read.
  • Give suggestion entries a stable identity (the item's kind plus label, or an id from the provider) and key the ForEach on it, so a re-rank moves and keeps row views instead of rewriting all of them.
  • If SwiftUI still dominates after that, host the list in a view-based NSTableView that refuses first responder, with reused cells, which AppKit documents for long reusable lists, and keep the model-driven selection.

Measure

  • Re-run typing-1900k: SwiftUI time under the popup against 980 ms and updateWindowSizeFromContent against 99 ms over about 420 keys.
  • 120 fps screen capture of 20 popup opens: frames from the key to the popup at its final size and position, and whether a frame at the wrong size appears.
  • The .completion interval from InteractionTracer, median of 50 popups, before and after.

Part of the performance plan.

Activity

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

    @datlechin
    MemberAuthor

    #4583 sizes the panel from its items in the same turn, before it is ordered front, measures only the preview, and keys rows by what they show with an Equatable label view.

    Left:

    • The NSTableView step. The issue makes it conditional on SwiftUI still dominating, and that needs the typing-1900k Time Profiler run, which the lanes do not run. In a local Debug harness (a 200-row list re-ranked 200 times, assign, layout and draw) main-thread CPU per key went from 7.18 ms to 6.96 ms, and fittingSize there cost only 0.07 ms, so most of the per-key cost is the list's own SwiftUI update and drawing.
    • The 120 fps capture of 20 popup opens and the .completion interval were not run here.
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