-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(cli): pre-compute and cache tree-sitter query metadata (#7171)
All of our tree-sitter queries are cached, this adds some additional metadata caching to prevent recomputing on every query execution and in some cases every query match. --- ### Changes are visible to end-users: no - Searched for relevant documentation and updated as needed: yes - Breaking change (forces users to change their own code or config): no - Suggested release notes appear below: yes Caching of `aspect configure` tree-sitter query metadata across query executions. ### Test plan - Covered by existing test cases GitOrigin-RevId: 85d25355eded802cdb408d0c24364b40d1062b08
- Loading branch information
Showing
4 changed files
with
68 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package treesitter | ||
|
||
import sitter "github.com/smacker/go-tree-sitter" | ||
|
||
// Basic wrapper around sitter.Query to cache tree-sitter cgo calls. | ||
type sitterQuery struct { | ||
q *sitter.Query | ||
|
||
// Pre-computed and cached query data | ||
stringValues []string | ||
captureNames []string | ||
predicatePatterns [][][]sitter.QueryPredicateStep | ||
} | ||
|
||
func mustNewQuery(lang LanguageGrammar, query string) *sitterQuery { | ||
q := mustNewTreeQuery(lang, query) | ||
|
||
captureNames := make([]string, q.CaptureCount()) | ||
for i := uint32(0); i < q.CaptureCount(); i++ { | ||
captureNames[i] = q.CaptureNameForId(i) | ||
} | ||
|
||
stringValues := make([]string, q.StringCount()) | ||
for i := uint32(0); i < q.StringCount(); i++ { | ||
stringValues[i] = q.StringValueForId(i) | ||
} | ||
|
||
predicatePatterns := make([][][]sitter.QueryPredicateStep, q.PatternCount()) | ||
for i := uint32(0); i < q.PatternCount(); i++ { | ||
predicatePatterns[i] = q.PredicatesForPattern(i) | ||
} | ||
|
||
return &sitterQuery{ | ||
q: q, | ||
stringValues: stringValues, | ||
captureNames: captureNames, | ||
predicatePatterns: predicatePatterns, | ||
} | ||
} | ||
|
||
// Cached query data accessors mirroring the tree-sitter Query signatures. | ||
|
||
func (q *sitterQuery) StringValueForId(id uint32) string { | ||
return q.stringValues[id] | ||
} | ||
|
||
func (q *sitterQuery) CaptureNameForId(id uint32) string { | ||
return q.captureNames[id] | ||
} | ||
|
||
func (q *sitterQuery) PredicatesForPattern(patternIndex uint32) [][]sitter.QueryPredicateStep { | ||
return q.predicatePatterns[patternIndex] | ||
} |