Skip to content

Walk an expression's nodes without allocating or caching - #2474

Open
Rafael-SOWNet wants to merge 4 commits into
masterfrom
perf/allocation-free-node-walk
Open

Rafael-SOWNet wants to merge 4 commits into
masterfrom
perf/allocation-free-node-walk

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Entity.Nodes was DirectChildren.SelectMany(c => c.Nodes).Prepend(this), cached on every node. Each enumeration cloned two iterators per node visited, about 114 bytes a node, and the cached chain stayed alive on every node that had ever been walked. This is the walk #534 asked for, as re-scoped on 2026-11-15: one that neither allocates per step nor caches the subtree.

Three commits, meant to be merged as they are (rebase, not squash):

  1. Entity.Nodes walks on a pooled stack. NodeStack is a private mutable struct whose explicit stack is rented from ArrayPool<Entity>.Shared; slots are nulled on pop and cleared before the array goes back. Children are pushed in reverse only after their parent has been yielded, so the order is exactly the old pre-order, and a walk that stops at a node never asks for its children. Every walk has its own stack, so a predicate that walks another tree, re-entrantly or on another thread, shares nothing with it. Nodes stays IEnumerable<Entity>: a NodeWalk that is its own enumerator on first use, as LINQ's iterators are, so one object per enumeration and nothing per node, and nothing is cached on the tree.
  2. Struct walks where the walk is hottest. Internal AnyNode, AllNodes and CountNode, and ContainsNode, walk without an enumerator; Simplificator's node-kind checks and GetMinimumSubtree use them. Counted on an instrumented build (reverted): on sin(x)^2+cos(x)^2+x/x+(x+1)*(x-1), 809 of the 854 nodes walked per Simplify were Simplificator.Alternate's checks. Elsewhere what is left is under 1%. No public API is added.
  3. NodesWalkTest, 16 cases: pre-order against a recursive reference; repeated, abandoned and nested walks; a 2000-deep sum on 8 threads.
allocated per call master branch
Simplify, sin^2+cos^2+x/x+(x+1)(x-1) 594,941 B 493,044 B −17.1%
Simplify, 1/a+1/b+1/c+1/d 758,168 B 666,200 B −12.1%
Simplify, (x+1)(x+2)(x+3)(x+4) 1,177,183 B 994,183 B −15.5%

Kernel benchmark, local, master and branch:

benchmark master B/op branch B/op
SimplifyEasy 79,866 29,097 −63.5%
SimplifyHard 161,702,256 142,790,368 −11.7%
SolveMediumHard 1,338,926 1,262,067 −6.2%
SolveHard 8,496,544 8,167,576 −4.1%
SolveEasy 442,796 428,124 −3.4%
SolveEasyMedium 65,682 63,938 −3.1%
SolveMedium 454,229 446,069 −2.5%

No row is slower. Retained after enumerating every node's Nodes on a 2000-term sum: 335 KB on master, nothing on the branch. The kernel performance gate will flag the improved rows; the baseline follows from this PR's own CI run.

No answer changes. ContainsNode still answers a variable from VarsAndConsts, which leaves out a set builder's internal placeholder, as on master; the test notes it.

Full check before the rebase onto 73b77bf: 20,135 unit tests, 0 failed; MCP 444, Terminal 69, F# 134, Interactive 19; AOT 78/78; BoundCheck 0 disagreements, RootCheck 596/596, CasBench 120/121 with 0 wrong, SimpSweep 10463/10463, RuleCheck clean, CanonCheck and Confluence at their baselines. After the rebase, the whole unit suite: 20,588 passed, 0 failed.

Closes #534.

🤖 Generated with Claude Code

Rafael-SOWNet and others added 3 commits November 16, 2026 01:41
Nodes was DirectChildren.SelectMany(c => c.Nodes).Prepend(this), cached
per node: every enumeration cloned two iterators per node it visited
(about 114 B a node), and the cached chain stayed alive on every node
that had ever been walked (335 KB on a 2000-term sum). It is now one
object per enumeration that keeps a pre-order stack rented from
ArrayPool<Entity>.Shared, in the same order, with nothing kept on the
tree; each enumeration has its own stack, so a predicate that walks
another tree shares nothing with it.

Part of #534

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The walk itself is now a struct over the pooled stack, behind Nodes and
behind internal AnyNode, AllNodes and CountNode, which allocate nothing
at all. ContainsNode, the node-kind checks in Simplify's candidate
search and the occurrence counts in GetMinimumSubtree use them: those
were the call sites that still built an enumerator per walk in the
benchmarks, and SimplifyEasy allocates 14.6% less for it.

Part of #534

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…doned

Part of #534

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.97%. Comparing base (73b77bf) to head (2d80cbd).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
Sources/AngouriMath/Core/Entity/Entity.Nodes.cs 96.42% 2 Missing and 1 partial ⚠️
...ouriMath/Functions/Simplification/Simplificator.cs 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2474   +/-   ##
=======================================
  Coverage   86.97%   86.97%           
=======================================
  Files         324      325    +1     
  Lines       71849    71933   +84     
  Branches    20955    20971   +16     
=======================================
+ Hits        62492    62567   +75     
- Misses       5024     5033    +9     
  Partials     4333     4333           
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.50% <96.00%> (+<0.01%) ⬆️
fsharp 32.97% <ø> (ø)
interactive 39.81% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...urces/AngouriMath/Core/Entity/Entity.Definition.cs 94.30% <100.00%> (ø)
...Solvers/EquationSolver/AnalyticalEquationSolver.cs 96.87% <100.00%> (ø)
...ouriMath/Functions/Simplification/Simplificator.cs 95.96% <91.66%> (ø)
Sources/AngouriMath/Core/Entity/Entity.Nodes.cs 96.42% <96.42%> (ø)

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The kernel performance gate on this PR's first run reported seven rows below
the baseline recorded at 6790f7c and none above: SimplifyEasy 79,785 ->
29,097 B/op (-63.5%), SimplifyHard -11.7%, SolveMediumHard -6.3%, SolveHard
-4.1%, SolveEasy -3.4%, EvalPolynomialFresh15Digits -3.3%, SolveEasyMedium
-3.1%. The file is that run's own measurement (benchmark-results artifact of
run 43788847662), copied over the baseline as the gate asks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Rafael-SOWNet

Copy link
Copy Markdown
Member Author

The kernel performance gate failed as expected, on improvements only: seven rows below the baseline recorded at 6790f7c, none above — SimplifyEasy 79,785 → 29,097 B/op (−63.5%), SimplifyHard −11.7%, SolveMediumHard −6.3%, SolveHard −4.1%, SolveEasy −3.4%, EvalPolynomialFresh15Digits −3.3%, SolveEasyMedium −3.1%; every timing faster. The baseline is updated in this PR from that run's own measurement (run 43788847662).

This branch has not been deployed

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

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Walk an expression's nodes without allocating or caching

1 participant