Skip to content

perf(engine): pre-allocate AST slice buffers using node size - #44

Merged
HarshK97 merged 1 commit into
mainfrom
perf/ast-utils-presize
Aug 2, 2026
Merged

perf(engine): pre-allocate AST slice buffers using node size#44
HarshK97 merged 1 commit into
mainfrom
perf/ast-utils-presize

Conversation

@HarshK97

@HarshK97 HarshK97 commented Aug 2, 2026

Copy link
Copy Markdown
Owner

Problem

  1. PostOrder and PreOrder were allocating brand new slices at every single node level during AST traversal, so on larger files all that slice merging created unnecessary heap allocations and GC overhead.
  2. Descendants was also missing a capacity hint for its output slice.

What Changed

  • Pre-allocating AST Slices: Pre-allocated output slices using Size() upfront for PostOrder and PreOrder traversals, so we only allocate a single heap buffer per traversal.
  • Descendants buffer: Pre-allocated the descendants slice and added early nil and single-leaf checks.
  • Nil Safety: Added explicit nil checks and unit test assertions for nil node inputs.

Benchmark Results

Ran benchstat across all 60 integration pipeline test fixtures comparing main against these changes:

  • Heap Allocations: Geometric mean dropped from 93.5k allocations down to 48.3k allocations per diff (a 48.3% overall reduction in allocations)

@HarshK97
HarshK97 merged commit e3f62ab into main Aug 2, 2026
8 checks passed
@HarshK97
HarshK97 deleted the perf/ast-utils-presize branch August 2, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant