perf(codegen): Use char codes for simple classes#660
Open
scttcper wants to merge 1 commit into
Open
Conversation
Simple character classes were emitted as regexp tests for every accepted character. Hot parser loops like keys and identifiers pay for that over and over. Emit direct charCodeAt comparisons for simple non-unicode, case-sensitive classes and reuse a temp char code inside each check. Keeps the regexp path for the complex cases. Co-Authored-By: Codex GPT-5 <noreply@openai.com>
Author
|
let me know if you need the benchmark or something else |
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.
This optimizes generated parser code for simple character classes.
A lot of grammars have hot loops that look like this:
Peggy currently emits a regexp test for each character consumed by those classes. This PR emits direct
charCodeAtcomparisons when the class is simple enough to do that exactly: noiflag, no unicode mode, and only single-code-unit ranges/characters.Anything more complicated still uses the existing regexp path.
As one real-world benchmark, I used Sentry’s search grammar, which is the grammar we use for parsing search bar queries: https://github.com/getsentry/sentry/blob/master/static/app/components/searchSyntax/grammar.pegjs. That grammar has a lot of key/value filters, so longer searches spend plenty of time in these character-class loops.
AND, about 5.4 KBOR, about 5.4 KBFor that generated parser, repeated class-check code went from 177
charCodeAtcalls to 76 because each check computes the character code once and reuses it.