-
Notifications
You must be signed in to change notification settings - Fork 787
feat: implement tail call optimization #7641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: implement tail call optimization #7641
Conversation
The only downside I can think of is that this might lead to confusing stack traces. It would also be good to double check with a microbenchmark that this is actually good for performance; I wouldn't be too surprised if tail calls are slow because the engine needs to do some kind of extra shuffling to make them work. |
Not sure if this is strictly on point: but automatically converting last calls to tail calls may (will) affect semantics of applications. |
Can you elaborate on what the observable semantic differences are? Stack exhaustion doesn't count, since the number of stack frames is an implementation limit and therefore allowed to be broadly nondeterministic, even within a single execution. |
Actually, stack exhaustion, as you put it, DOES count (in languages like Scheme). |
I agree there are no semantic related issue, combine call and return should not have semantic changing except call stack.
According to https://v8.dev/blog/wasm-tail-call#proposal, It should be have some improvement, but I am not sure whether v8 itself do this optimization for some simple cases. I will do this benchmark later. |
Aha, dynamic scope is the issue. @HerrCai0907, you'll have to make sure that the optimized calls are not inside |
I think the current implement already consider it. |
I have run test for quick sort array with 9000 elements in MAC M1 Pro. It can speed up about 0.57% execution time. |
I took a look at the draft implementation, and it doesn't look like it takes |
I think it is better replacement for |
Yes, or at least it could be used inside |
Fixed: #7647
Now it is just a generic concept about tail call optimization. I am not sure whether I am in the correct way. So I put the draft version and expect to get some feedback.
The basic idea is we can convert
to
to
to