Skip to content

fix(syntax): avoid quadratic allocation in Node::filter_map#1598

Merged
azjezz merged 1 commit into
carthage-software:mainfrom
dotdash:fix_syntax_avoid_quadratic_allocation_in_node_filter_map
Apr 7, 2026
Merged

fix(syntax): avoid quadratic allocation in Node::filter_map#1598
azjezz merged 1 commit into
carthage-software:mainfrom
dotdash:fix_syntax_avoid_quadratic_allocation_in_node_filter_map

Conversation

@dotdash

@dotdash dotdash commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

📌 What Does This PR Do?

filter_map_internal creates a new vector at every node, returning it and then extending the parent node' vector with it. This means a bunch of unnecessary allocations, and also O(n²) runtime behaviour due to the repeated copies from the child vectors into their parent vectors.

Passing the result vector down as an accumulator avoids both issues.

🔍 Context & Motivation

Making this go brrrrrrrr

🛠️ Summary of Changes

  • Bug Fix: Avoid unnecessary allocations and copies.

📂 Affected Areas

  • Linter
  • Formatter
  • CLI
  • Dependencies
  • Documentation
  • Other (please specify):

🔗 Related Issues or PRs

none

📝 Notes for Reviewers

Summary
  ~/mago-quad --threads 32 lint --retain-code none ran
    1.05 ± 0.04 times faster than ~/mago-base --threads 32 lint --retain-code none

filter_map_internal creates a new vector at every node, returning it and
then extending the parent node' vector with it. This means a bunch of
unnecessary allocations, and also O(n²) runtime behaviour due to the
repeated copies from the child vectors into their parent vectors.

Passing the result vector down as an accumulator avoids both issues.
@azjezz azjezz changed the title fix(syntax): Avoid quadratic allocation in Node::filter_map fix(syntax): avoid quadratic allocation in Node::filter_map Apr 7, 2026
@azjezz azjezz merged commit 2bc8ce2 into carthage-software:main Apr 7, 2026
6 of 7 checks passed
@azjezz azjezz added t-maintenance Code maintenance and refactoring c-syntax PHP lexer, parser, and AST t-enhancement New feature or request labels Apr 7, 2026
@dotdash dotdash deleted the fix_syntax_avoid_quadratic_allocation_in_node_filter_map branch April 13, 2026 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c-syntax PHP lexer, parser, and AST t-enhancement New feature or request t-maintenance Code maintenance and refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants