Repository navigation
Compute only the case a compiled piecewise takes - #2438
Open
Rafael-SOWNet wants to merge 2 commits into
Open
Rafael-SOWNet wants to merge 2 commits into
Rafael-SOWNet wants to merge 2 commits into
Conversation
A provided and a piecewise compiled every case and kept the one whose predicate held, so a case undefined or costly where it is not taken was computed anyway: piecewise(primepi(x) provided x < 100, 0) at 9999991 sieved ten million numbers, 625 KB and 18 ms a call, for a value it threw away, where evaluation takes 13 us. FastExpression now has two forward jumps, JUMP and JUMP_UNLESS_HOLDS, and runs its instructions by index. Each predicate is tried in turn; the first that holds has its case computed and jumps past the rest. Every jump is forward, so a call still ends. CALL_SELECT and CALL_PROVIDED go. A jump can pass over the instruction that saves a repeated subexpression to the cache, and the slot then holds what an earlier call left there. So the compiler logs what it saves, and once code a jump may pass over is compiled, what that code saved is marked unsaved again and computed afresh where next met. What a predicate saves stays valid for the cases after it, which are reached only once it was computed. No value changes: compilecheck's tables are identical, and its new branch section finds no disagreement with evaluation over ten samples sharing subexpressions between cases, at real, negative, complex and NaN points. Without the cache discipline three of them read a previous call's value. Part of #2195. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every subexpression became a local assigned before the expression's value, so the conditional a provided or a piecewise compiles to chose between cases already computed, as FastExpression did: piecewise(primepi(x) provided x < 100, 0) at 9999991 allocated 625 KB a call over Complex and over double. A case's value, and every predicate after the first, is now built as a block of its own inside the conditional, with a copy of the cache, so what it computes is cached inside the block and not read outside it, where it may not have been computed. The AOT smoke test compiles a piecewise whose cases are blocks. Part of #2195. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2438 +/- ##
=======================================
Coverage 86.88% 86.89%
=======================================
Files 313 313
Lines 69951 69994 +43
Branches 20450 20453 +3
=======================================
+ Hits 60775 60819 +44
- Misses 4932 4933 +1
+ Partials 4244 4242 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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.
Part of #2195.
Both compilers now compute only the case a
piecewiseor aprovidedtakes. #2209 made FastExpression compile them, but without a jump: it computed every case and kept the one that held. The LINQ compiler did the same in effect, becauseBuildTreeassigns every node to a local before the expression's value, so its conditional only chose between cases already computed. No value was wrong, since no kernel throws. The cost was the untaken case's:piecewise(primepi(x) provided x < 100, 0)at 9999991 built a 625 KB sieve on every call.Complexmobius(x)case not taken, at 999999999999989, master → branchThese are single runs, but the ratios are large. On an expression with no branch,
x + x/2 + sin(x), FastExpression is within noise: 41.3 / 41.9 ns on master and 37.6 / 41.4 ns here, best of 7 rounds, interleaved.FastExpression gets two forward jumps:
JUMP_UNLESS_HOLDSpops a truth value and jumps unless it is exactly true, so an undecided predicate passes the case over, as evaluation does.JUMPgoes to the end.Each case compiles as its predicate, a jump to the next case unless it holds, its value, then a jump to the end.
PUSH NaNfollows the last case. Every jump goes forward, so a call always ends.The subexpression cache needed care:
piecewise(sin(x) provided x > 1, sin(x)+1 provided x > 0, sin(x)+2)at 1/2 gave 1.909, where the value is 1.479.The LINQ compiler builds each case's value, and each predicate after the first, as its own block inside the conditional, with a copy of the cache. That is the second commit, kept apart.
No answer changes.
compilecheck's tables are identical before and after, and the new agreement tests passed on master before the change, so no BREAKING-CHANGES.md row is owed.What #2195 still holds:
gamma(a, z)stays refused, as its thread decided.gcdandlcmat whole arguments, and whetherCompileshould evaluate calculus nodes first, are maintainer decisions.and/orstill compute both sides; Kleene's tables would allow the same jump.Checked on this branch:
BranchNotTakenCompilationTestchecks 13 expressions against evaluation at real, negative, zero, complex and NaN points, visited in both orders, in FastExpression and in LINQ overComplexanddouble. It also asserts that a call at a point where a case is not taken allocates under 64 KB; on master that fails 8 of 8. After rebasing onto Compile a function of a matrix argument as the function of each entry, and vectors as arrays #2435, the compile, matrix and public-API tests pass (1576).🤖 Generated with Claude Code