Skip to content

perf: More efficient computation of used symbols#4568

Merged
WillLillis merged 1 commit intotree-sitter:masterfrom
wetneb:flatten_grammar_performance
Jul 10, 2025
Merged

perf: More efficient computation of used symbols#4568
WillLillis merged 1 commit intotree-sitter:masterfrom
wetneb:flatten_grammar_performance

Conversation

@wetneb
Copy link
Contributor

@wetneb wetneb commented Jul 7, 2025

As the call to symbol_is_used does not depend on the production, it is more efficient to call it only once outside the loop over productions.

More broadly, the reachable symbols are currently computed at three stages of parser generation (listed here from upstream to downstream):

  • When parsing the grammar (in parse_grammar.rs, where I added the warning in feat: Warn about unreachable variables #4567)
  • When flattening the grammar (this location, flatten_grammar.rs)
  • When generating the parse table (in build_tables.rs)

I'm not clear on whether all those stages are useful - can the reachability of some symbols change because of some intermediate transformations?

As the call to `symbol_is_used` does not depend
on the production, it is more efficient to call it
only once outside the loop over productions.

I'm not sure if `rustc` is able to do this optimization
on its own (it would need to know that the function
is pure, which sounds difficult in general).
@clason clason added parser-generation Related to `tree-sitter generate` perf Performance related ci:backport release-0.25 Backport label labels Jul 8, 2025
@WillLillis WillLillis merged commit 36d93ae into tree-sitter:master Jul 10, 2025
21 checks passed
@tree-sitter-ci-bot
Copy link

Successfully created backport PR for release-0.25:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:backport release-0.25 Backport label parser-generation Related to `tree-sitter generate` perf Performance related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants