Repository navigation
Walk an expression's nodes without allocating or caching - #2474
Open
Rafael-SOWNet wants to merge 4 commits into
Open
Rafael-SOWNet wants to merge 4 commits into
Rafael-SOWNet wants to merge 4 commits into
Conversation
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 Report❌ Patch coverage is Additional details and impacted files@@ 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
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 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>
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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