Skip to content

[diffs/edit] tokenizer cache optimization - #1070

Open
ije wants to merge 1 commit into
mainfrom
ije/tokenizer-incrementality
Open

[diffs/edit] tokenizer cache optimization#1070
ije wants to merge 1 commit into
mainfrom
ije/tokenizer-incrementality

Conversation

@ije

@ije ije commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Previously, every edit discarded all cached bracket metadata from the edited line to the end of the document—even when tokenization proved that the downstream state was unchanged.

Example

function foo() {
  return 1;
}

Changing 1 to 100 does not affect the TextMate state after that line. The highlighting below it remains valid, but the old implementation still deleted its cached string, comment, and regular-expression ranges.

A later bracket lookup on a distant line therefore had to invoke the grammar again to rebuild metadata that was still valid.

New behavior

  1. Re-tokenize the edited line.
  2. Overwrite that line’s bracket metadata.
  3. Compare its ending state with the cached state.
  4. If the states match, preserve the unchanged downstream metadata.
  5. If the state has not converged, invalidate from where background tokenization will continue.
  6. For structural edits, invalidate from the edit because line indexes have shifted.

This is safe because unchanged text tokenized from an identical starting state produces identical bracket classifications.

Benchmark

The benchmark primes a 100,000-line cache, edits line 0, and queries the final line.

Metric Before After
Grammar calls for the edit 1 1
Additional grammar calls for the final line 1 0
Median benchmark time 0.198 ms 0.104 ms

The measured 47% improvement applies specifically to this cache-invalidation scenario, not to overall tokenizer performance. The primary benefit is eliminating unnecessary grammar work after ordinary state-neutral edits.

Avoid invalidating unchanged bracket metadata after state-neutral edits. Extend the tokenizer benchmark and add regression coverage.
@vercel

vercel Bot commented Aug 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
pierre-docs-diffs Ready Ready Preview Aug 10, 2026 12:50am
pierre-docs-diffshub Ready Ready Preview Aug 10, 2026 12:50am
pierre-docs-trees Ready Ready Preview Aug 10, 2026 12:50am
pierrejs-diff-demo Ready Ready Preview Aug 10, 2026 12:50am

Request Review

@ije

ije commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 20aed789bd

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ije
ije requested review from amadeus and necolas August 10, 2026 13:20
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