Merged
Conversation
Contributor
There was a problem hiding this comment.
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()inpackages/parser/src/cursor.tswith 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
benchscript 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
valuecontains 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 usingindexOf/lastIndexOfto 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.
2cece9f to
942d2b0
Compare
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
offsetout of loop body\nThe 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.