You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reacted by Nam Chu Hoai, Alberto Leal, Jake Coughlin, Adam Voss, jD91mZM2, Igor Skokov, sighoya, Vijay Bambhaniya, Johann Og, timothy and 20 more
Reacted by Jeff Burdges, Alberto Leal, Adam Voss, jD91mZM2, sighoya, Fedor Gavrilov, Vijay Bambhaniya, Yota Toyama, Simer, Felipe S. S. Schneider and 1 more
Reacted by jD91mZM2, Tyler Neely, sighoya, John Gallagher, LJZN, Louis Pilfold, Abhik Jain, Yota Toyama, Simer, Felipe S. S. Schneider and 2 more
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.
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)?
Reacted by Andrés Testi, Jake Coughlin, Joe Farebrother, Patryk Wychowaniec, Ludwig Stecher, Chetan Bhasin, Robin Brown, John Hainline, OrHayat, Simer and 2 more
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.
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.
Reacted by Ethan Wang, Mach, Sofie Hanzlíková, OrHayat, Mithun and Gabriel B. Sant'Anna
@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).
Reacted by Constantin, jD91mZM2, Lance Roy, Kornel, Vlad Frolov, Adowrath, Jeremy Day, Robin Brown, GuillaumeDIDIER, Sofie Hanzlíková and 6 more
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).
@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.
@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.
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.
@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.
@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.
@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.
Activity
ranma42 commentedon Jan 12, 2015
In the PR several people mentioned that they preferred
becomeoverbe.Even tough the PR has been postponed, would it be a good idea to reserve
becomefor tailcalls before releasing 1.0?I'm not suggesting to implement tail call elimination, just reserving the keyword for the future.
ticki commentedon Dec 14, 2015
What's the state of this?
steveklabnik commentedon Jan 21, 2016
@ticki status is "nobody has submitted a new RFC yet"
ticki commentedon Jan 21, 2016
I might make one when I get time, if no-one is working on this.
hatahet commentedon Feb 26, 2016
In Scala, annotating a method with
@tailrecwould 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)?steveklabnik commentedon Feb 26, 2016
@hatahet we reserved a 'become' keyword, the prevailing idea seems to be that
return->becomewould indicate that the function should guarantee TCOhatahet commentedon Feb 26, 2016
Does that mean that having
becomeat the tail position would always be required?otherwise, what if it were mixed as follows:
steveklabnik commentedon Feb 26, 2016
I would imagine so, but that's the work that an RFC proposing the syntax would have to work through :)
ConnyOnny commentedon Dec 27, 2016
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
U007D commentedon Jan 8, 2017
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
becomekeyword and automatically optimizereturnwhen applicable.I understand that
becomehas been reserved for quite some time for this purpose, but I propose that the Rust compiler recognize and optimize explicit and implicitreturntail 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 becomefastsafe.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.
ranma42 commentedon Jan 8, 2017
@BradleyGibson TCO can already happen in some cases (i.e. the compiler might optimise tail calls if it decides so).
becomeis about guaranteed tail calls (i.e. getting a compilation error when the compiler is unable to perform TCO).ssokolow commentedon Jan 8, 2017
@BradleyGibson Last time I looked at it, the impression I got was that we're going in this direction:
becomemeans "Throw a compile-time error if we cannot apply tail-call optimization here"Given that
becomeis 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.
nagisa commentedon Jan 8, 2017
This kinda goes against the explicitness of rust.
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
returnwithbecomein cases where its known to work.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).
17 remaining items
le-jzr commentedon Mar 1, 2018
@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.
U007D commentedon Mar 1, 2018
@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.
ssokolow commentedon Mar 1, 2018
"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.
le-jzr commentedon Mar 1, 2018
@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.
ssokolow commentedon Mar 3, 2018
I see your point and it definitely sounds like it would be well-suited to being handled the same way overflow checks are.
kephas commentedon Mar 9, 2018
@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.
OvermindDL1 commentedon Mar 9, 2018
Exactly that, some algorithms depend on guaranteed TCO.
U007D commentedon Mar 9, 2018
@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.
Centril commentedon May 15, 2018
We're not doing postponement issues anymore; just continue discussion over at #1888.
Closing this therefore to reduce the backlog.
nixpulvis commentedon May 10, 2019
@Centril can we reopen #1888 then? This is currently in a graveyard of sorts.
Centril commentedon May 10, 2019
@nixpulvis It's probably not on the roadmap this year.
nixpulvis commentedon May 11, 2019
That's fine, I'm interested in Rust for the long haul ;) Been watching these issues for years now.
shirshak55 commentedon Jul 17, 2020
is there any planning for proper TCO?
matu3ba commentedon Jul 20, 2020
@shirshak55 look at #1888