Skip to content

Keep expansions and nested results across one outermost Simplify - #2015

Merged
Rafael-SOWNet merged 2 commits into
masterfrom
keep-expansions-across-one-simplify
Oct 21, 2026
Merged

Rafael-SOWNet merged 2 commits into
masterfrom
keep-expansions-across-one-simplify

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Part of #233.

Simplify expanded, factored and re-simplified the same trees over and over:

  • the polynomial pass in Simplificator.Alternate ran res.Expand() once per variable;
  • Patterns.SumOfFractions simplified the product of the denominators afresh, and the Expand and Factorize candidates were simplified again;
  • PolynomialFactoring.TryGetRationalCoefficients expanded once more.

The cost was located by attributing bytes to AddHistory call sites. Two examples:

  • Rubi's 4.1.7:136. For t^4/((b t^2 + a + a t^2)^2 (1 + t^2)), the integrand that row hands the homogeneous substitution, 11.0 of 12.09 GB was one 342-term product expanded 72 times.
  • Rubi's 7.4.2:891. It ran 2,145 factoring calls on 200 distinct polynomials and 1,232 nested Simplify calls on 214 distinct trees.

The fix:

  • The polynomial pass expands each candidate once.
  • Expansions and nested Simplify(expr, level) results are kept in a thread-static table that lives for the outermost Simplify only, keyed by the tree's value.
  • Each entry keeps its own SettingsState snapshot and is used only while that still holds. A single stamp for the whole call missed 2,219 of 2,325 lookups, because settings register lazily and the snapshot grows partway through.
  • Only elementary trees are kept: numbers, variables, arithmetic, logarithms, trigonometric functions, abs and sgn. Nothing is kept while a RewriteRecording is open.

Answers are identical.

  • On the probe, all 4,796 rows of Rubi's 4.1.x and 7.4.x give 0 answer changes against master 08a3c49, with allocation over the rows answered by both 482.2 → 320.2 GB. The 26 heavy rows of 4.1.7 and 7.4.2 are byte-identical:
row master this
4.1.7:136 14.9 s, 12.14 GB 1.2 s, 0.41 GB
7.4.2:455 17.8 s, 7.79 GB 1.0 s, 0.77 GB
7.4.2:891 18.1 s, 21.58 GB 2.9 s, 2.65 GB
7.4.2:908 28.2 s, 21.48 GB 4.1 s, 2.55 GB

Chain against master 08a3c49, with the harness at its current revision on both arms:

  • pocket of 3,836 rows (every 15th of each family, every 2nd of 4.1.7 and 7.4.2): verdicts identical but one, 6.6.3:213, which goes from timeout to declining;
  • allocation over the 3,813 rows solved on both: 648.4 → 375.8 GB;
  • time over the same rows: 1,012 → 815 s;
  • alone, 1.3.2:599 and 3.3:246 go from timeout to answered;
  • no row's allocation grew past 1.3×, in the pocket or the samples.

The samples of every family are unchanged in verdicts, and allocate less:

family sample master this
1 157.4 GB 110.4 GB
0, whole 67.0 54.1
2 8.7 2.6
3 51.5 30.3
4 133.5 64.7
5 20.5 12.2
6 75.0 58.8
7 46.8 37.7
8 11.8 9.1

Tests: a new class pins four integrals with allocation bounds on the test's own thread, plus one Simplify form. With the change disabled, all five fail on their bounds (7.4–19.7 GB against 3–8 GB). Locally, 4,302 tests across the patterns, simplification, factoring, recording, polynomial, expansion, transformation, corpus and multithreading classes pass.

The kernel benchmark gate will report IMPROVED. Measured locally on the test thread: SimplifyHard allocates 176.66 → 158.15 MB/op (−10.5%), and SimplifyEasy +0.9%, within the band. All eight benchmark answers are byte-identical. I will copy this run's measured-CommonFunctionsInterVersion.json from the CI artifact over performance-baseline.json, as version_performance_control.md says a decrease is to be recorded.

🤖 Generated with Claude Code

https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura

Simplify expanded, factored and re-simplified the same trees over and over.
The polynomial pass in Simplificator.Alternate ran res.Expand() once per
variable; Patterns.SumOfFractions simplified the product of the denominators
afresh and the Expand and Factorize candidates were simplified again; and
PolynomialFactoring.TryGetRationalCoefficients expanded once more. For
t^4/((b t^2 + a + a t^2)^2 (1 + t^2)), the integrand Rubi's 4.1.7:136 hands
the homogeneous substitution, 11.0 of 12.09 GB was one 342-term product
expanded 72 times; 7.4.2:891 ran 2,145 factoring calls on 200 distinct
polynomials and 1,232 nested Simplify calls on 214 distinct trees.

The polynomial pass now expands each candidate once. Expansions and nested
Simplify(expr, level) results are kept in a thread-static table that lives
for the outermost Simplify only, keyed by the tree's value, each entry with
its own SettingsState snapshot and used only while it still holds; only
elementary trees are kept, and nothing while a RewriteRecording is open.

Answers are identical. Chain against master 08a3c49: pocket of 3,836 rows
(every 15th of each family, every 2nd of 4.1.7 and 7.4.2), allocation over
the 3,813 solved on both 648.4 -> 375.8 GB and time 1,012 -> 815 s; samples
of every family unchanged in verdicts, allocation down 19-70% per family
(family 4 133.5 -> 64.7 GB); no row's allocation grew past 1.3x. Alone,
1.3.2:599 and 3.3:246 timeout -> answered, 6.6.3:213 timeout -> declined.

Part of #233.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
…line

The gate reported IMPROVED: SimplifyHard allocates 157,895,384 B/op, 10.0%
below the 175,442,312 recorded at 7c0dce12 on 2026-09-28, where the baseline was last
taken. The file is the one this pull request's own Benchmark job wrote,
measured-CommonFunctionsInterVersion.json from its artifact, copied over the
baseline as version_performance_control.md describes. Every other gated row
is within its band: SimplifyEasy +0.8%, Derivate +0.8%, SolveHard -1.5%.

The run was on .NET 10.0.13 on a 4-core Ubuntu 24.04 runner, where the
previous baseline records .NET 10.0.10 on an 8-core Ubuntu 26.04 machine, so
the recorded times are the runner's; the gate fails on time only past 3x.
The ungated map and its reasons are carried over.

Part of #233.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant