Repository navigation
Walk an expression's nodes without allocating or caching - #2474
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #2474 +/- ##
=======================================
Coverage 86.96% 86.97%
=======================================
Files 325 326 +1
Lines 72200 72284 +84
Branches 21033 21049 +16
=======================================
+ Hits 62787 62866 +79
- Misses 5061 5065 +4
- Partials 4352 4353 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
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). |
|
Rebased onto 6ac879d (#2471, #2475, #2477 merged meanwhile). The previous push ran no CI at all: the PR conflicted with master ( |
45421dc to
0ecdba3
Compare
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>
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>
0ecdba3 to
d73f92c
Compare
Entity.NodeswasDirectChildren.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):
Entity.Nodeswalks on a pooled stack.NodeStackis a private mutable struct whose explicit stack is rented fromArrayPool<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.NodesstaysIEnumerable<Entity>: aNodeWalkthat 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.AnyNode,AllNodesandCountNode, andContainsNode, walk without an enumerator;Simplificator's node-kind checks andGetMinimumSubtreeuse them. Counted on an instrumented build (reverted): onsin(x)^2+cos(x)^2+x/x+(x+1)*(x-1), 809 of the 854 nodes walked perSimplifywereSimplificator.Alternate's checks. Elsewhere what is left is under 1%. No public API is added.NodesWalkTest, 16 cases: pre-order against a recursive reference; repeated, abandoned and nested walks; a 2000-deep sum on 8 threads.Simplify,sin^2+cos^2+x/x+(x+1)(x-1)Simplify,1/a+1/b+1/c+1/dSimplify,(x+1)(x+2)(x+3)(x+4)Kernel benchmark, local, master and branch:
No row is slower. Retained after enumerating every node's
Nodeson 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.
ContainsNodestill answers a variable fromVarsAndConsts, 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