Skip to content

fix(tui): eliminate O(n) mention-trigger scan on every keystroke - #49634

Closed
deadadm1n wants to merge 1 commit into
anomalyco:devfrom
deadadm1n:fix/tui-mention-trigger-perf
Closed

deadadm1n wants to merge 1 commit into
anomalyco:devfrom
deadadm1n:fix/tui-mention-trigger-perf

Conversation

@deadadm1n

@deadadm1n deadadm1n commented Sep 17, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #49635

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Typing or dictating a large amount of text into the TUI chat input made it lag badly as the buffer grew, because mentionTriggerIndex runs on every keystroke (onContentChange → autocomplete onInput) and was doing two full-buffer Intl.Segmenter passes per keystroke — measured ~65ms at 100KB of input.

The fix adds two cheap raw-string gates so the display-aware scan only runs when an actual @ trigger needs resolving:

  1. no @ in the buffer → return immediately (~0.001ms)
  2. cursor at the end of an ASCII buffer → resolve the trailing whitespace-delimited word with a plain scan instead (~0.04ms)

Non-ASCII text, mid-buffer cursors and active @ mentions still take the exact original code path, so behavior is unchanged. A randomized parity test (~500 cases) additionally proves the new fast paths return identical results to the old implementation.

How did you verify your code works?

  • bun test in packages/tui: 202 pass, 0 fail (includes the new test/util/display.test.ts)
  • tsgo --noEmit: clean
  • oxlint: 0 errors

Screenshots / recordings

N/A – no visual change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Sep 17, 2026
@github-actions github-actions Bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Sep 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@thdxr

thdxr commented Oct 10, 2026

Copy link
Copy Markdown
Member

This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2.

— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

@thdxr thdxr closed this Oct 10, 2026
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.

TUI: typing large amounts of text into the chat input causes severe lag

2 participants