Skip to content

Cache terminal completion catalog across idle repaints - #260

Closed
norci wants to merge 1 commit into
lambda-symbolics:masterfrom
norci:fix/idle-completion-rebuild
Closed

norci wants to merge 1 commit into
lambda-symbolics:masterfrom
norci:fix/idle-completion-rebuild

Conversation

@norci

@norci norci commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Idle repaints no longer rebuild the completion catalog.

application-operation-connect-ui installs a cached completion closure on the terminal UI.
The closure stores three values: entries, generation, registry.
terminal-ui-completion-function compares the stored values with the current values on each paint.
A match returns entries.
A mismatch rebuilds the catalog through application-operation-completion-entries.

A held lock leaves the catalog one paint behind.

The closure returns entries when it cannot take *extension-registry-transaction-lock*.
The next paint sees the change.
Waiting would deadlock with a writer that needs the UI lock.

One counter covers every catalog input.

extension-registry--advance-generation increments *extension-registry-generation*.
application-command--publish-registrations calls it for every command write.
tool-registry-register calls it for tool additions.
tool-registry-delete-if calls it for tool removals.
tool-registry-restrict calls tool-registry-delete-if. It needs no separate increment.

Reads leave the counter unchanged.

application-command-list opens the registry transaction on the read path.
An increment inside that transaction would invalidate the cache on every paint.

Registry writes reach the cached closure through one call chain.

flowchart LR
  A[application-command--publish-registrations] --> G[extension-registry--advance-generation]
  B[tool-registry-register] --> G
  C[tool-registry-delete-if] --> G
  G --> D["*extension-registry-generation*"]
  D --> L[terminal-ui-completion-function]
  P[terminal-ui--current-completions] --> L
  L -->|generation changed| O[application-operation-completion-entries]
  L -->|registry changed| O
  L -->|unchanged| E[entries]
  O --> E
Loading

A CPU profile identified the hotspot.

Sampling an idle image attributed over 10% CPU to application-operation-completion-entries.
The catalog has three inputs: the command registry, the tool registry, static help sections.
Those inputs change only inside registry write transactions.

Painting rebuilt the whole completion directory from the command and tool registries on every idle repaint, consuming over 10% CPU while idle. Cache the catalog in the UI completion closure and invalidate it through a monotonic extension registry generation advanced at every catalog input write point.
@norci

norci commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

This PR only fixed the issue of CPU utilization during idle.
When receiving streaming output, the CPU utilization is still very high. Is it necessary to continue addressing this issue?

@norci norci closed this Nov 21, 2026
@norci
norci deleted the fix/idle-completion-rebuild branch November 21, 2026 01:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant