Hasty Briefsbeta

Bilingual

My Formatter Will Never Be as Fast as Ruff

a year ago
  • #zig
  • #code-formatting
  • #tree-sitter
  • The author built a universal code formatter in Zig using tree-sitter, aiming for consistent formatting across multiple languages.
  • Initial benchmarks showed promise, but Ruff (a Python formatter) outperformed it significantly on large files.
  • Profiling revealed that 79% of the time was spent parsing the AST, not in the formatting logic itself.
  • Attempts to optimize tree-sitter (parser reuse, custom allocators, partial parsing) yielded minimal gains.
  • Tree-sitter's design requires parsing entire files at once, making it less optimal for formatting compared to specialized parsers like Ruff's.
  • Ruff achieves its speed by using a hand-written recursive descent parser optimized for Python and skipping unnecessary AST features.
  • Despite the performance gap, the author sees value in tree-sitter's versatility and multi-language support.
  • The project currently achieves ~90% compatibility with Black's formatting rules, with plans to reach 100%.