perf(treesitter): compile AST rules into lookup sets and cache pointers - #118
Merged
Conversation
- treesitter: compile rules slices into lookup sets on init to replace O(N) slice scans with O(1) set checks - treesitter: cache *Rules pointers in rulesCache to eliminate per-call struct copying and allocations in GetRules - treesitter: add nil-safe helper query methods with fallback for uncompiled rules - engine: fast-path matching when root ASTs have identical hashes and are isomorphic - tests: add unit tests for Rules query helper methods across compiled, uncompiled, and nil receivers
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.
Problem
buildASTWithRuleswas runningslices.Containsacross 5 different slices (Ignored,Keywords,LabelIgnored,Unordered,Flattened) on every single node, so on larger files with thousands of nodes, doing linear scans over and over added unnecessary overhead.AreTypesEquivalentwas looping through nested 2D slices on every node comparison during matching.GetRulesstored and returnedRulesby value, so it copied the whole struct on every language lookup.What Changed
map[string]struct{}lookup sets forFlattened,Ignored,LabelIgnored,Keywords,Blocks, andUnorderedat startup incompileSets(), turning linear slice checks into O(1) map lookups while keeping slice fallback for uncompiled rules.EquivalentTypesinto anequivGroupsmap (map[string][]int), so type equivalence lookups just check matching group IDs directly instead of scanning every group.*Rules(IsIgnored,IsKeyword,IsLabelIgnored,IsUnordered,IsFlattened,IsBlock,Alias) to keep all rule lookups clean and encapsulated inbuildASTWithRules.rulesCacheto store and return*Rulespointers directly, stopping all the struct copying onGetRulescalls.engine.Matchso identical subtrees pair up immediately when root node hashes match.internal/treesitter/rules_test.gocovering compiled sets, uncompiled fallback, and nil receiver safety across all helper methods.