Automatically apply tail call optimizations#14933
Merged
JaroslavTulach merged 13 commits intodevelopfrom Apr 21, 2026
Merged
Conversation
JaroslavTulach
commented
Apr 2, 2026
| return switch (getTailStatus()) { | ||
| case TAIL_DIRECT -> directCall.executeCall(frame, function, callerInfo, state, arguments); | ||
| case TAIL_LOOP -> throw new TailCallException(function, callerInfo, arguments); | ||
| case TAIL_DIRECT, // -> directCall.executeCall(frame, function, callerInfo, state, arguments); |
Member
Author
There was a problem hiding this comment.
- the commit aad4b85 is an attempt to gently revert Opt-in TCO #1219
- basically it is inserting
@Tail_Callannotation everywhere it makes sense - that should work, should it not?
- basically it is inserting
- so we can see what was the megamorphic behavior reported by Make Tail Call Optimisation Opt-In #1217 that had to be fixed
- however the revert isn't working
- the enso runtime won't even run
- can't find basic symbols on objects
- I am not yet sure what's going on...
Member
Author
There was a problem hiding this comment.
Let's start from basics again:
sbt:runtime-benchmarks> benchOnly CallableBenchmarks
Benchmark Mode Cnt Score Error Units
benchSumTCOfromCall avgt 5 749,231 ± 71,350 ms/op
benchSumTCOmethodCall avgt 5 410,419 ± 22,373 ms/op
benchSumTCOmethodCallWithDefaultedArguments avgt 5 395,680 ± 20,531 ms/op
benchSumTCOmethodCallWithNamedArguments avgt 5 400,371 ± 19,671 ms/op
is the basic result of TCO related benchmark from this branch. It is indeed slow the develop branch runs at:
Benchmark Mode Cnt Score Error Units
benchSumTCOfromCall avgt 5 75,386 ± 1,417 ms/op
benchSumTCOmethodCall avgt 5 75,965 ± 3,086 ms/op
benchSumTCOmethodCallWithDefaultedArguments avgt 5 85,326 ± 6,681 ms/op
benchSumTCOmethodCallWithNamedArguments avgt 5 75,170 ± 2,196 ms/op
Member
Author
There was a problem hiding this comment.
- with 0d6509c I have following results locally
Benchmark Score Error Units
benchSumTCOfromCall 71,632 ± 7,940 ms/op
benchSumTCOfromCallWithTailCall 74,268 ± 10,342 ms/op
benchSumTCOmethodCall 77,010 ± 15,595 ms/op
benchSumTCOmethodCallWithDefaultedArguments 72,061 ± 3,377 ms/op
benchSumTCOmethodCallWithDefaultedArgumentsWithTailCall 68,628 ± 0,750 ms/op
benchSumTCOmethodCallWithNamedArguments 69,425 ± 2,434 ms/op
benchSumTCOmethodCallWithNamedArgumentsWithTailCall 69,752 ± 0,438 ms/op
benchSumTCOmethodCallWithTailCall 73,485 ± 4,974 ms/op
- verifying with engine bench run
TailCallException when possible
jdunkerley
approved these changes
Apr 17, 2026
…d as tail call eligible
JaroslavTulach
commented
Apr 20, 2026
| deep_error n:Integer = if n <= 0 then Error.throw "Here is an error" else | ||
| deep_error n-1 | ||
| x = deep_error n-1 | ||
| x+1 |
Member
Author
There was a problem hiding this comment.
The need to capture x return value and return some other value is a demonstration of the success of automatic tail call optimization! Without these additional operations the deep_xyz call gets optimized and the stack is one and not 21:
Prior to #14480 the depth of Error.throw was ignored altogether
Expecting depth of 21, but was 1
If x is an error, then the execution will return the error as x+1 will still yield an error. In case of deep_throw the panic is propagated and x+1 code is skipped.
Member
Author
Member
Author
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.



Pull Request Description
@Tail_CallChecklist
Please ensure that the following checklist has been satisfied before submitting the PR:
Scala,
Java,