Tail call optimization

1,858 views
Skip to first unread message

JONNALAGADDA Srinivas

unread,
Feb 12, 2011, 11:14:59 PM2/12/11
to golan...@googlegroups.com
        While on the topic of optimization, could the Go team comment on the plans for tail call optimization (or at least tail recursion optimization), if any?  Thanks.

                                                            Greetings,
                                                                    JS

Ostsol

unread,
Feb 12, 2011, 11:47:30 PM2/12/11
to golang-nuts

JONNALAGADDA Srinivas

unread,
Feb 13, 2011, 12:00:39 AM2/13/11
to golan...@googlegroups.com
        Thanks.  I had seen that over-a-year-old thread.  I was inquiring about the *current* plans.  By the way, the following consumes all available memory (and more) on most computers, even with the current release.

~ % cat j.go
package main

func foo(n int) {
foo(n)
}

func main() {
foo(42)
}

                                                            Greetings,
                                                                    JS

Russ Cox

unread,
Feb 13, 2011, 12:03:22 AM2/13/11
to golan...@googlegroups.com
On Sat, Feb 12, 2011 at 23:14, JONNALAGADDA Srinivas <sigm...@gmail.com> wrote:
>         While on the topic of optimization, could the Go team comment on the
> plans for tail call optimization (or at least tail recursion optimization),
> if any?

There are no such plans for gc (6g, 5g, 8g).
Personally, I find meaningful stack traces helpful more
often than I find myself using unbounded tail recursions.
I have on other projects used gcc -fno-optimize-sibling-calls
precisely to make stack traces more useful.

I think it's very unlikely that the language would require it.
Go is not Scheme.

Russ

Ostsol

unread,
Feb 13, 2011, 12:27:36 AM2/13/11
to golang-nuts
On Feb 12, 10:03 pm, Russ Cox <r...@golang.org> wrote:
Oops! So that thread that I linked contains obsolete information?

-Daniel

Evan Shaw

unread,
Feb 13, 2011, 12:35:11 AM2/13/11
to Ostsol, golang-nuts
On Sun, Feb 13, 2011 at 6:27 PM, Ostsol <ost...@gmail.com> wrote:
> Oops!  So that thread that I linked contains obsolete information?

Russ just confirmed that what Ian said in that old thread still
applies. There's nothing stopping a compiler from implementing tail
call optimization; it's just that the programmer shouldn't count on it
when writing code because it's not required by the spec. It doesn't
sound like that will change, either.

- Evan

Erwin

unread,
Feb 13, 2011, 5:34:01 AM2/13/11
to r...@golang.org, golan...@googlegroups.com

There are no such plans for gc (6g, 5g, 8g).
Personally, I find meaningful stack traces helpful more
often than I find myself using unbounded tail recursions.
I have on other projects used gcc -fno-optimize-sibling-calls
precisely to make stack traces more useful.

Wouldn't a gc -no-optimize mode be a good thing to have once optimizations get in the way of debugging?

chris dollin

unread,
Feb 13, 2011, 8:51:30 AM2/13/11
to Erwin, r...@golang.org, golan...@googlegroups.com

If I'm assuming/relying on tail-call optimisation in my coding, then
turning it off "for debugging" will likely make my program crash
demanding more stack frames.

We all have our choices: I'd rather have guaranteed TCO than full
stack-frames for debugging, but I don't think it's unreasonable for
someone else to have the opposite preference.

Chris

--
Chris "allusive" Dollin

Evan Shaw

unread,
Feb 13, 2011, 2:18:47 PM2/13/11
to Erwin, r...@golang.org, golan...@googlegroups.com
On Sun, Feb 13, 2011 at 11:34 PM, Erwin <snes...@gmail.com> wrote:
> Wouldn't a gc -no-optimize mode be a good thing to have once optimizations
> get in the way of debugging?

That option already exists as -N.

http://golang.org/cmd/gc/

- Evan

Eoghan Sherry

unread,
Feb 13, 2011, 3:25:24 PM2/13/11
to chris dollin, Erwin, r...@golang.org, golan...@googlegroups.com

I found Newsqueak's become statement a nice fit for TCO in an
imperative setting.

func sum(a, b uint) uint {
if a == 0 {
return b
}
become sum(a-1, b+1)
}

Eoghan

Message has been deleted

Sokolov Yura

unread,
Mar 9, 2015, 2:59:50 AM3/9/15
to golan...@googlegroups.com
I'd prefer special keyword for tailcall too: those who really need tailcall will write it explicitely.

Linker Lin

unread,
Mar 27, 2018, 3:05:13 AM3/27/18
to golang-nuts
become +1
TCO is very important for porting other FP lang to Go.

Ian Lance Taylor

unread,
Mar 27, 2018, 9:23:19 AM3/27/18
to Linker Lin, golang-nuts
On Tue, Mar 27, 2018 at 12:05 AM, Linker Lin <linker...@gmail.com> wrote:
> become +1
> TCO is very important for porting other FP lang to Go.

This is https://golang.org/issue/22624.

Ian
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages