Repository navigation
A long chain of sums is simplified from the bottom up, not by a recursion as deep as the chain - #2167
Merged
Rafael-SOWNet merged 1 commit intoOct 27, 2026
Conversation
…sion as deep as the chain A sum simplifies its left operand first, and that operand's left operand, so a chain of n sums nested to the left was simplified by a recursion n deep. A sum built one term at a time is that shape: CollectLikeTerms builds one, and Simplify's search hands the collected chain on. The integral of (q + p x)/(c + b x + a x^2)^3 with its symbols pinned reached about 900, which overflowed C# Test's Debug build on a 1 MB stack on most Windows runs since 2026-09-15 (#2165 moved that leg to Release), and needed more than 384 KB in Release, where macOS gives 512 KB. Sumf and Minusf now simplify every 32nd node of a left spine at least 64 deep first, deepest first. Every node's simplification is cached, so each of those recursions stops at the checkpoint below it. The same nodes are simplified, each once, in a different order, and an ordinary sum pays for a walk of a few nodes and nothing else. On a thread of a given stack size: cubic row, Release 384 KB overflow (909 repeats) -> 256 KB passes cubic row, Debug 1 MB overflow (654 repeats) -> 1 MB passes (a_1+..+a_30)(b_1+..+b_30) expanded, Release, 256 KB: 400 terms overflow -> 900 and 1,600 pass What overflows next on the cubic row, at 192 KB in Release, is CostModel.ChildrenCost, 872 deep: a different recursion, with 2.7x headroom on macOS. Unit suite 17,128 passed, 0 failed. Against master on one machine: allocation is identical (23,356 B per round of InnerSimplified on five ordinary sums; 15.79 MB per round of Simplify on ten expressions), and time is within noise (InnerSimplified 2,525 ms vs 2,557 ms, mean of three 200,000-round runs whose spread in one arm is 5%). Fixes #2166 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2167 +/- ##
=======================================
Coverage 86.41% 86.41%
=======================================
Files 295 295
Lines 64790 64806 +16
Branches 19053 19059 +6
=======================================
+ Hits 55987 56005 +18
+ Misses 4773 4770 -3
- Partials 4030 4031 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
1 of 4 tasks
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.
Fixes #2166.
The defect: a sum simplifies its left operand first, and that operand's left operand, and so on. So a chain of n sums nested to the left was simplified by a recursion n deep. A sum built one term at a time has that shape:
CollectLikeTermsbuilds one, andSimplify's search hands the collected chain on to later candidates.The integral of
(q + p x)/(c + b x + a x^2)^3, with its symbols pinned, reached about 900 levels:The change:
SumfandMinusffirst simplify every 32nd node of a left spine that is at least 64 deep, deepest first. Every node's simplification is cached, so each of those recursions stops at the checkpoint below it. The same nodes are simplified, each once, in a different order. An ordinary sum pays only for a walk of a few nodes.Measured on a thread of a given stack size:
(a_1+..+a_30)(b_1+..+b_30)expanded, Release, 256 KBThe next thing to overflow on the cubic row, at 192 KB in Release, is
CostModel.ChildrenCost, 872 deep. That is a different recursion, and it has 2.7× headroom on macOS.Test:
LongSumStackDepthTestexpands the 900-term product on a 256 KB thread.Suite and performance:
InnerSimplifiedon five ordinary sums, bytes per roundSimplifyon ten expressions, bytes per roundThe time difference is +1.3%, inside the 5% spread between runs of a single build.
No answer changes, so there is no BREAKING-CHANGES entry.
🤖 Generated with Claude Code
https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura