Skip to content

perf(client): avoid whole-document work on each keystroke - #171

Merged
DontBlameMeDev merged 1 commit into
mainfrom
t3/63f59bad
Nov 20, 2026
Merged

DontBlameMeDev merged 1 commit into
mainfrom
t3/63f59bad

Conversation

@DontBlameMeDev

@DontBlameMeDev DontBlameMeDev commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Typing in a large shared buffer and republishing a large grant both did work proportional to the whole document or folder on every event. A one-line edit rebuilt two 100k-row shadow tables and scanned UTF-16 lengths line by line; a grant event revalidated every path from scratch.

The shadow is now patched in place, UTF-16 lengths are batched for ASCII documents, offset arithmetic caches the end of the document and computes both ends of a change in one pass, and grant path validation is cached across whole-listing republishes. Behavior and the shared contracts are unchanged.

Proof (Neovim v0.12.6, cheapest of five os.clock runs, median of three runs):

nvim --headless -l scripts/bench-reshadow.lua 100000
  whole-buffer reshadow        22.917 ms -> 4.795 ms
  on_bytes first row            1.941 ms -> 0.005 ms
  on_bytes last row             2.272 ms -> 0.166 ms
  single-row reshadow         ~1.7    ms -> 0.003 ms

nvim --headless -l scripts/bench-reshadow.lua 20000
  whole-buffer reshadow         4.880 ms -> 0.905 ms
  on_bytes last row             0.570 ms -> 0.038 ms

whole-buffer on_bytes, 100k lines: 93.5 ms -> 66.5 ms
grantReport, 100k synthetic paths, warm cache: 134.923 ms -> 6.332 ms
grantReport, 10k synthetic paths, warm cache:  22.211 ms -> 0.947 ms

The rebuild-spread timing guard now samples a thousand rebuilds: after the in-place patch, ten rebuilds fell below os.clock resolution on CI and made the ratio flaky.

Checks: bash scripts/test-lua.sh (18 files ALL OK), nvim --headless -l test/lua/document.lua (8 consecutive passes), npm run typecheck, npm test (182 pass), and the workspace contract checker (4 comparisons, 0 drifted, 0 errors). A differential script compared the old and new UTF-16 conversions over ASCII, BMP, astral, invalid, negative and out-of-range inputs with zero mismatches. scripts/ci-local.sh all passed every part except e2e, which cannot start here because this worktree has no sibling reference_server checkout or selvaged binary; that is a pre-existing environment gap, unchanged by this PR.

Reviewed with review-loop: 1 round with opencode-go/hy4-flash-free. verdict: approved

Created with opencode-go/hy4-flash-free in pi.

Copilot AI balanced review requested due to automatic review settings November 20, 2026 02:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

The pull request caches grant-path validation results. It also changes UTF-16 length calculations and updates Document’s cached offsets, shadow rows, and buffer-edit handling.

Changes: Grant path checks

Layer / File(s) Summary
Cache grant path checks
companion/grant.ts
grantReport caches path-validation results and clears the cache when it reaches 200,000 entries. Reports still include the full path listing and include unsafe when paths are ungranted.

Document offsets and shadow updates

Layer / File(s) Summary
Compute UTF-16 lengths and initialize offsets
lua/selvage/utf16.lua, lua/selvage/document.lua
The UTF-16 utilities add bulk line-length calculation and adjust conversion behavior for out-of-range offsets. Document initialization uses the bulk lengths and records the document end offset.
Cache prefixes and patch the shadow
lua/selvage/document.lua, scripts/bench-reshadow.lua, test/lua/document.lua
Document adds paired prefix lookup and updates shadow rows in place. Benchmark and test comments describe the changed work and rebuild behavior.
Update offsets during resync and buffer edits
lua/selvage/document.lua
resync rebuilds cached lengths and uses the previous end offset for the published change range. on_bytes uses paired prefixes and removes a trailing newline only for edits ending at column zero on or beyond the last buffer row.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Suggested reviewers: claude

Merge Risk: Low · up to d2fce

The performance changes keep behavior the same on standard Neovim builds. Two narrow gaps remain. Size-changing edits fail on Neovim builds without LuaJIT, because they call table.move. Documents containing malformed UTF-8 can get inconsistent UTF-16 offsets. Both are small fixes worth making before merge.

Pre-merge checks: Passed 2

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "perf(client): avoid whole-document work on each keystroke" matches the main change. The changeset patches the shadow in place, caches document end and change offsets, batches UTF-16 length …
Full details: Title check

Explanation

The title "perf(client): avoid whole-document work on each keystroke" matches the main change. The changeset patches the shadow in place, caches document end and change offsets, batches UTF-16 length calculations, and caches grant-path validation, all aimed at reducing per-keystroke and republish cost. The title is specific and concise, and it follows a conventional commit style.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@DontBlameMeDev
DontBlameMeDev merged commit 7b9e88f into main Nov 20, 2026
3 checks passed
@DontBlameMeDev
DontBlameMeDev deleted the t3/63f59bad branch November 20, 2026 02:14
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.

2 participants