Issue #12543 has been updated by Eregon (Benoit Daloze).


duerst (Martin D=FCrst) wrote:
> We want programs to be fast, and tail call optimization makes them faster.

That's not true in general.
It might make recursive methods faster, but it makes normal methods calls t=
hat happen at the end of a method body slower with a JIT, because the TCO c=
all is a loop calling two different methods, instead of straight line code =
from inlining the method call which happens to be in tail position.

> Is it possible to use "partial" tail-call optimization, where the backtra=
ce is kept but all other frame state is discarded?

I think not in general, because then we'd need a stack for the backtrace an=
d TCO no longer removes the need for stack space (it might be feasible to, =
e.g., keep a counter if it's a self-recursive call but not in general).

I strongly agree that if we add TCO, it should be explicit in the code, oth=
erwise it breaks backtraces/tracing/debugging and it slows downs JIT-ed exe=
cution.

Maybe it could be implicit if it's restricted to self-recursive calls (e.g.=
; def bar; ...; bar; end).
In that case, there is not much information in the backtrace to be lost, an=
d the performance of JIT-ed execution is likely similar, while allowing the=
 self-recursive style (instead of a loop).

I'm not sure how useful the self-recursive style is in Ruby though.
I am thinking the example above with main_loop is more readable and easier =
to understand what it actually does with a `while socket =3D server_socket.=
accept` loop.
Do we have motivating examples for this feature?

----------------------------------------
Feature #12543: explicit tail call syntax: foo() then return
https://bugs.ruby-lang.org/issues/12543#change-81441

* Author: mame (Yusuke Endoh)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: =

----------------------------------------
How about introducing a new syntax for tail call?

```
def foo()
  foo()
end
foo() #=3D> stack level too deep
```

```
def bar()
  bar() then return
end
bar() #=3D> infinite loop
```

* no new keyword (cf. `goto foo()`)
* no conflict with any existing syntax
* an experimental patch is available (attached)
* no shift/reduce nor reduce/reduce conflict in parse.y

-- =

Yusuke Endoh <mame / ruby-lang.org>

---Files--------------------------------
then_return.patch (9.18 KB)


-- =

https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>