Skip to content

Optimize position tracking performance#19

Merged
darthmaim merged 1 commit intomainfrom
feature/advance-performance
Feb 21, 2026
Merged

Optimize position tracking performance#19
darthmaim merged 1 commit intomainfrom
feature/advance-performance

Conversation

@darthmaim
Copy link
Member

  • Move offset out of loop body
  • Add fast path for \n
  • Add fast path for no line breaks

The fast paths should in theory handle all cases the parser currently supports, but for correctness and future additions the advance algorithm falls back to the simple loop over all characters if the fast paths are not hit.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improves parser position tracking performance by optimizing how the cursor advances through consumed input, and updates benchmarks/tooling to measure the impact.

Changes:

  • Optimize advance() in packages/parser/src/cursor.ts with fast paths for '\n' and strings without line breaks, and move offset updates out of the per-character loop.
  • Update the parser benchmark input to include frequent newlines to better exercise newline handling.
  • Add a root bench script and a changeset for a patch release of @gw2/markup-parser.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
packages/parser/test/parser.bench.ts Adjust benchmark input to include \n separators for newline-heavy parsing scenarios.
packages/parser/src/cursor.ts Add fast paths and restructure cursor advancement to reduce per-character overhead.
package.json Add a root bench script to run workspace benchmarks.
.changeset/small-geese-refuse.md Declare a patch release for the parser performance change.
Comments suppressed due to low confidence (1)

packages/parser/src/cursor.ts:39

  • When value contains a newline, this does two passes over the string (includes('\n') and then the full character loop). If multiline chunks are possible, consider folding the newline detection into the loop (or using indexOf/lastIndexOf to jump directly) to avoid scanning from the start twice.
  // fast path for no line breaks (this is the most common case)
  if (!value.includes('\n')) {
    start.column += value.length;
    return;
  }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Move `offset` out of loop body
- Add fast path for `\n`
- Add fast path for no line breaks

The fast paths should in theory handle all cases the parser currently supports, but for correctness and future additions the advance algorithm falls back to the simple loop over all characters if the fast paths are not hit.
@darthmaim darthmaim force-pushed the feature/advance-performance branch from 2cece9f to 942d2b0 Compare February 21, 2026 19:13
@darthmaim darthmaim added this pull request to the merge queue Feb 21, 2026
Merged via the queue into main with commit 1566e24 Feb 21, 2026
3 checks passed
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