Repository navigation
Optimize string comparisons and parser state management - #297
Open
bartveneman wants to merge 3 commits into
Open
bartveneman wants to merge 3 commits into
bartveneman wants to merge 3 commits into
Conversation
Avoid substrings for keyword/function-name checks (compare source ranges instead), replace tuple-returning helpers with fields, drop the at-rule name string and trim_boundaries tuple on the hot path, and reuse lexer snapshots in the declaration parser. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Member
Author
|
The Generated by Claude Code |
Contributor
|
| 📦 Package | 📏 Base Size | 📏 Source Size | 📈 Size Change |
|---|---|---|---|
| @projectwallace/css-parser | 45.9 kB | 46.2 kB | +308 B |
commit: |
At-rule names are rare compared to tokens, so the range-based dispatch wasn't worth ~1.4 kB of extra code. Also build the reusable lexer snapshot without a long literal and shorten new comments. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.
Summary
This PR optimizes string comparisons throughout the parser to avoid unnecessary substring allocations and improves lexer state management by reusing position objects in hot paths.
Key Changes
String Comparison Optimizations
substring()+str_equals()patterns withstr_equals_range()to compare string ranges directly without allocating substringsis_and_or_not_range()utility function to efficiently check for logical operators (and,or,not) without allocationsstrip_vendor_prefix()function and replaced with inline range-based vendor prefix stripping inparse_prelude_dispatch()Parser Method Refactoring
parse_prelude()into two methods:parse_prelude(): accepts at-rule name as stringparse_prelude_named_in_source(): accepts name as source range, avoiding substring allocationparse_prelude_dispatch()to work with source ranges instead of pre-extracted stringsscan_matching_paren()return type from tuple to boolean, storing results in instance fields (paren_content_end,paren_close_end) to avoid tuple allocationsLexer State Management
save_position_into()method to reuse existingLexerPositionobjects instead of allocating new oneslookahead_positionfield inDeclarationParserfor property-name lookahead snapshotsConditionParser.end_positiongetter toseek_to_end()method that directly updates target lexer stateData Structure Changes
DECLARATION_AT_RULESfromSetto array for simpler range-based lookupsatrule_has_declarations()to use range-based string comparison instead of Set lookupBoundary Trimming Refactoring
trim_boundaries()calls with explicitskip_whitespace_and_comments_forward()andskip_whitespace_and_comments_backward()calls for clearer intent and better control over trimmed rangesImplementation Details
https://claude.ai/code/session_01Xv2dWHgzdat1eEeyhYGMbA