From: eregontp@... Date: 2019-09-07T09:57:44+00:00 Subject: [ruby-core:94821] [Ruby master Feature#12543] explicit tail call syntax: foo() then return Issue #12543 has been updated by Eregon (Benoit Daloze). duerst (Martin D�rst) 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 that happen at the end of a method body slower with a JIT, because the TCO call 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 backtrace is kept but all other frame state is discarded? I think not in general, because then we'd need a stack for the backtrace and 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, otherwise it breaks backtraces/tracing/debugging and it slows downs JIT-ed execution. 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, and 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 = 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() #=> stack level too deep ``` ``` def bar() bar() then return end bar() #=> 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 ---Files-------------------------------- then_return.patch (9.18 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: