Skip to content

Fold the call tree only on immediate self-recursion - #78

Merged
revarbat merged 1 commit into
mainfrom
fold-immediate-recursion
Aug 7, 2026
Merged

Fold the call tree only on immediate self-recursion#78
revarbat merged 1 commit into
mainfrom
fold-immediate-recursion

Conversation

@revarbat

@revarbat revarbat commented Aug 7, 2026

Copy link
Copy Markdown
Member

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 in
the tree.

Cause

The two totals are computed differently, and only one survived folding:

  • the flat view measures elapsed time across a site's entries
  • the tree derives cumulative bottom-up as self + Σ children, so it can
    only 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 a multmatrix already open further up and
took 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 parentf 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.

restore()   flat 88.3%   tree 88.4%      (was 81.2% vs 0.9%)

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, plus
DirectSelfRecursionStillFolds to pin that folding still happens where it
should.

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

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>
@revarbat
revarbat merged commit 709da93 into main Aug 7, 2026
3 checks passed
@revarbat
revarbat deleted the fold-immediate-recursion branch August 7, 2026 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant