Fold the call tree only on immediate self-recursion - #78
Merged
Conversation
Folding a re-entered call site onto any ancestor credited that whole subtree to the ancestor, and every node between it and the re-entry lost the time. Cumulative is derived bottom-up, so it can only count what is genuinely below a node -- the flat view, which measures elapsed time across a site's entries, kept counting it. The two views then disagreed about the same call site. On a BOSL2 attachment chain that was restore() at 226ms in All Call Sites and 2.5ms in the Call Tree: its callees re-entered a multmatrix already open further up and took 223ms with them. 24 of 390 sites diverged, all on that one chain, each losing the same block of time. Folding now applies only when the match IS the parent -- f calling itself directly, which is what folding exists for and still collapses to one node. A cycle through other call sites gets a real node, so the tree contains its own work again. The cost is more nodes on mutually recursive chains; kMaxProfilePathNodes still bounds it. The agreement test is restricted to sites reached by exactly one path. A site that nests within itself has a node per level now, and summing those double-counts while the flat view is recursion-guarded -- comparing them there would prove nothing. The first fixture written for this test never folded at all and passed against the broken code; this one reproduces the 86x gap. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
The Call Tree and All Call Sites disagreed about the same call site — on a
BOSL2 attachment chain,
restore()read 226 ms flat and 2.5 ms inthe tree.
Cause
The two totals are computed differently, and only one survived folding:
self + Σ children, so it canonly count what is genuinely below a node
Folding a re-entered site onto any ancestor credited that whole subtree
to the ancestor, and every node between it and the re-entry lost the time.
restore()'s callees re-entered amultmatrixalready open further up andtook 223 ms with them.
24 of 390 sites diverged, all on that one chain, each losing the same block.
This is the flip side of #77: cumulative used to be measured per node,
which broke containment (children summing past their parent); deriving it
bottom-up fixed that and introduced this.
Fix
Fold only when the match is the parent —
fcalling itself directly,which is what folding exists for and still collapses to one node. A cycle
through other call sites gets a real node, so the tree contains its own
work again.
Every single-path site in that model now agrees to 0.0000 ms. Node count
goes 10,930 → 14,132 (+29%) against the 200,000 cap.
Test
TreeAndFlatAgreeForASiteReachedOneWay, plusDirectSelfRecursionStillFoldsto pin that folding still happens where itshould.
Restricted to single-path sites deliberately: a self-nesting site now has a
node per level, and summing those double-counts while the flat view is
recursion-guarded, so comparing them there proves nothing.
Worth noting the first fixture written for this test passed against the
broken code — it never folded across anything. The one committed
reproduces the 86× gap and is confirmed to fail without the fix.
1409 tests pass.
🤖 Generated with Claude Code