When writing tree-sitter queries, prioritize performance by choosing efficient predicates and avoiding computationally expensive operations. Use `#any-of?` instead of complex `#match?` patterns when checking against multiple literal values, as it reduces regex compilation overhead. Avoid predicates like `#has-ancestor?` which have poor performance...
When writing tree-sitter queries, prioritize performance by choosing efficient predicates and avoiding computationally expensive operations. Use #any-of?
instead of complex #match?
patterns when checking against multiple literal values, as it reduces regex compilation overhead. Avoid predicates like #has-ancestor?
which have poor performance characteristics due to unbounded tree traversal. Consider the computational complexity of your queries, especially for features that run frequently like syntax highlighting during scrolling.
Example of optimization:
; Instead of expensive regex matching:
((identifier) @variable.builtin
(#match? @variable.builtin "^(this|msg|block|tx)$"))
; Use efficient literal matching:
((identifier) @variable.builtin
(#any-of? @variable.builtin "this" "msg" "block" "tx"))
For performance-critical features, ensure optimized builds are used and consider caching strategies when queries involve expensive calculations that run repeatedly during user interactions.
Enter the URL of a public GitHub repository