Skip to content
Tech News
← Back to articles

How AST-grep Rewrote Tree-sitter in Rust and Made It 30% Faster

read original more articles
Why This Matters

The rewriting of Tree-sitter's core in Rust by ast-grep significantly enhances parsing speed and efficiency, resulting in a 30% faster parser and overall 22% faster performance for code analysis tools. This advancement demonstrates how leveraging Rust's performance and safety features can optimize foundational components in the developer ecosystem, benefiting both industry tools and end-users.

Key Takeaways

How ast-grep Rewrote Tree-sitter in Rust and Made It 30% Faster ​

Part 1 of 4 — the complete adventure

ast-grep rewrote Tree-sitter's C core in Rust, with AI writing the code. The new core is faster at parsing, faster at reading the completed tree, and faster in ast-grep itself. (The title's “30%” is the parser-only number; end-to-end, ast-grep runs about 22% faster.)

Source repository: HerringtonDarkholme/tree-sitter.

Two quick introductions before the numbers. ast-grep — the structural code-search tool this blog belongs to — searches code by syntax rather than by text, so every file it touches must first become a syntax tree. Tree-sitter is the parser framework that builds that tree: you give it a grammar definition, and it generates a fast parser for that language. Born in the editor world, it now powers an enormous ecosystem of grammars and tools.

Performance and peak RSS. Throughput is normalized so the unmodified C build (“C / normal”) scores 100; higher is better. RSS is peak resident memory, and the raw-parsing row shows it as a range because it varies across the benchmark's language fixtures. The outline row is ast-grep's real workload: parse every file in a repository, then walk each completed tree to extract a structural outline.

Benchmark C / normal Rust Difference Raw parsing Throughput: 100

RSS: 8.48–21.41 MiB Throughput: 129.74

RSS: 8.42–25.70 MiB +29.74% throughput

+20.0% RSS upper bound Tree traversal Throughput: 100

... continue reading