Skip to content

guaranteed tail call elimination #271

Closed

Description

@rust-highfive

Tracking issue for postponed PR #81

Activity

  1. added
    postponedRFCs that have been postponed and may be revisited at a later time.
    on Sep 24, 2014
  2. ranma42 commented on Jan 12, 2015

    @ranma42
    Contributor

    In the PR several people mentioned that they preferred become over be.
    Even tough the PR has been postponed, would it be a good idea to reserve become for tailcalls before releasing 1.0?

    I'm not suggesting to implement tail call elimination, just reserving the keyword for the future.

  3. ticki commented on Dec 14, 2015

    @ticki
    Contributor

    What's the state of this?

  4. steveklabnik commented on Jan 21, 2016

    @steveklabnik
    Contributor

    @ticki status is "nobody has submitted a new RFC yet"

  5. ticki commented on Jan 21, 2016

    @ticki
    Contributor

    I might make one when I get time, if no-one is working on this.

  6. hatahet commented on Feb 26, 2016

    @hatahet

    In Scala, annotating a method with @tailrec would make the compiler enforce that that method's implementation is tail recursive, otherwise it is a compiler error.

    Would something similar be a good fit for Rust (e.g. tagging functions with #[tailrec], or something of that sort)?

  7. steveklabnik commented on Feb 26, 2016

    @steveklabnik
    Contributor

    @hatahet we reserved a 'become' keyword, the prevailing idea seems to be that return -> become would indicate that the function should guarantee TCO

  8. hatahet commented on Feb 26, 2016

    @hatahet

    Does that mean that having become at the tail position would always be required?

    fn loop1() {
      // ...
      become loop1()
    }
    

    otherwise, what if it were mixed as follows:

    fn loop1() {
      // ...
      if foo {
        // ...
        become loop1()
      } else {
        // ...
        loop1()
      }
    }
    
  9. steveklabnik commented on Feb 26, 2016

    @steveklabnik
    Contributor

    I would imagine so, but that's the work that an RFC proposing the syntax would have to work through :)

  10. added
    T-langRelevant to the language team, which will review and decide on the RFC.
    on Aug 17, 2016
  11. ConnyOnny commented on Dec 27, 2016

    @ConnyOnny

    How about we first implement this with a trampoline¹ as a slow cross-platform fallback implementation, and then successively implement faster methods for each architecture/platform?
    This way the feature can be ready quite quickly, so people can use it for elegant programming. In a future version of rustc such code will magically become fast.

    ¹ see here for a small and to-be-generalized example trampoline: https://gist.github.com/ConnyOnny/5917e4e549056f6a3db01a5277480f6b

  12. U007D commented on Jan 8, 2017

    @U007D

    I'm new to the Rust community, but would be forever sad if I didn't mention this before we committed to this path-- I am coming from the C++ community, where there is a strong (perhaps too strong) reticence to introduce new keywords.

    TL;DR - abandon become keyword and automatically optimize return when applicable.

    I understand that become has been reserved for quite some time for this purpose, but I propose that the Rust compiler recognize and optimize explicit and implicit return tail calls, freeing developers from the burden of having to specify TCO 'manually'. To borrow @ConnyOnny's phrasing, with this scheme, in a future version of rustc, existing code will magically become fast safe.

    TCO would not be enabled by default for debug builds, but would for (optimized) release builds. A compiler switch would be provided for explicit control (for example, when a user wanted to selectively disable just TCO, but retain other optimizations).

    If there is someone willing to guide with a Rust newb, I'm willing to work an RFC for this.

  13. ranma42 commented on Jan 8, 2017

    @ranma42
    Contributor

    @BradleyGibson TCO can already happen in some cases (i.e. the compiler might optimise tail calls if it decides so). become is about guaranteed tail calls (i.e. getting a compilation error when the compiler is unable to perform TCO).

  14. ssokolow commented on Jan 8, 2017

    @ssokolow

    @BradleyGibson Last time I looked at it, the impression I got was that we're going in this direction:

    1. Optimize recognized tail-calls whether or not it's asked for
    2. become means "Throw a compile-time error if we cannot apply tail-call optimization here"

    Given that become is already reserved, that would most definitely be in line with the approach Rust takes to this sort of thing.

    EDIT: ...and @ranma42 beat me to it by less than a second.

  15. nagisa commented on Jan 8, 2017

    @nagisa
    Member

    abandon become keyword and automatically optimize return when applicable

    This kinda goes against the explicitness of rust.

    with this scheme, in a future version of rustc, existing code will magically become fast.

    Guaranteed tail-calls if anything make stuff slower. The only benefit they have are guaranteed bounded stack usage. And there’s nothing preventing compiler from replacing return with become in cases where its known to work.

    If there is someone willing to guide with a Rust newb on this, I'm willing to work an RFC for this.

    There’s a considerable amount of work going on already. Most notably this and the corresponding prototyping effort (turned out to be quite a bag of snakes).

  16. 17 remaining items

  17. le-jzr commented on Mar 1, 2018

    @le-jzr

    @ssokolow I'm not aware of any other optimization that behaves like this. Can you make an example for your first point?

    For the second one, counting a number is much less overhead than calling a function without TCO, so the net performance is still a lot better than you have any right to expect from an unreliable optimization.

    That being said, if the recursion limit is enforced only in debug builds, that's fine by me.

  18. U007D commented on Mar 1, 2018

    @U007D

    @le-jzr For example, inlining optimizations can behave like this, depending on register pressure, hot-path threshold analysis or resulting binary size constraints.

    My thinking is that non-mandatory TCO won't make the situation worse than not having any TCO at all in terms of stack usage, so I'm looking at it exactly as you and @ssokolow are suggesting--an optimization.

    From the perspective of predictability, I agree there could be an issue with this (like many optimizations) and also support the ability to disable and/or enforce recursion limits in debug.

  19. ssokolow commented on Mar 1, 2018

    @ssokolow

    "like this" in what sense?

    If you don't explicitly ask for guaranteed TCO, whether your program exhausts the stack depends on whether the TCO optimizer kicks in. If you don't explicitly warn the compiler about accesses to volatile memory, whether your program works or silently corrupts data or crashes is entirely dependent on the internal implementation details of passes like the loop-invariant code motion and redundant instruction combining optimizations.

    When it comes to benchmarking, one of the first things you'll be taught to watch out for is the loop-deletion pass removing your entire test because the only used result it produces is the time it took to run.

    On the embedded side, whether the code fits into the microcontroller can be determined by the vagarities of optimizer internals.

  20. le-jzr commented on Mar 1, 2018

    @le-jzr

    @ssokolow What I mean is that TCO effectively removes a resource limit. It does not just increase the limit by a finite amount, it eliminates it completely/turns it infinite, and this happens in safe code (in contrast to your examples).

    In practice, what might happen is that you test and debug your application with arbitrarily large data and everything works flawlessly, and then it doesn't work at all on the user's side, and you are left confused, unable to reproduce the issue.

  21. ssokolow commented on Mar 3, 2018

    @ssokolow

    I see your point and it definitely sounds like it would be well-suited to being handled the same way overflow checks are.

  22. kephas commented on Mar 9, 2018

    @kephas

    @U007D, does inlining changes the correctness of the program? I don't think it does. Whether the code is inlined or not, the program should still pass or fail the same tests. This is not true if you write TCO-dependent code and TCO is or isn't done.

  23. OvermindDL1 commented on Mar 9, 2018

    @OvermindDL1

    Exactly that, some algorithms depend on guaranteed TCO.

  24. U007D commented on Mar 9, 2018

    @U007D

    @kephas I agree completely. I brought up inlining as a response to the question of what other optimizations "works or not works depending on random compiler heuristics". Of course no good optimization would change the correctness of the program.

    I am saying that in addition to the needs of TCO-dependent code, non-TCO-dependent code can also benefit--I would like to see that happen too.

  25. Centril commented on May 15, 2018

    @Centril
    Contributor

    We're not doing postponement issues anymore; just continue discussion over at #1888.
    Closing this therefore to reduce the backlog.

  26. nixpulvis commented on May 10, 2019

    @nixpulvis

    @Centril can we reopen #1888 then? This is currently in a graveyard of sorts.

  27. Centril commented on May 10, 2019

    @Centril
    Contributor

    @nixpulvis It's probably not on the roadmap this year.

  28. nixpulvis commented on May 11, 2019

    @nixpulvis

    That's fine, I'm interested in Rust for the long haul ;) Been watching these issues for years now.

  29. shirshak55 commented on Jul 17, 2020

    @shirshak55

    is there any planning for proper TCO?

  30. matu3ba commented on Jul 20, 2020

    @matu3ba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.postponedRFCs that have been postponed and may be revisited at a later time.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions