6 ms·
- Should there be a reference to C++/Rust in the title ? > Tail calls: I can't think of a Rust or C++ abstraction that relies on TCO to be zero-cost. This is
by toolslive 6y ago
- Should there be a reference to C++/Rust in the title ?
> Tail calls: I can't think of a Rust or C++ abstraction that relies on TCO to be zero-cost.
This is an interesting one. Doesn't the absence of TCO mean that there are a lot of (recursive) abstractions that cannot ever be zero cost ? First thing that comes to mind is processing of composites (for example serialization of a protobuf type definition). There are others.
- nicoburns 6y agoYeah, I thought this was a bizarre one to exclude. Especially as I can't imagine it has a particularly large effect on compile time. At least not if it's enabled by a keyword.
- toolslive 6y agoWhat I think happened is rationalization: the early versions of the rust compiler omitted TCO. "It doesn't fit with the rest of it. We might revisit this later". Later, it turned out too difficult to retrofit, and now the stance is "you don't need it". https://dev.to/seanchen1991/the-story-of-tail-call-optimizations-in-rust-35hf https://dev.to/seanchen1991/the-story-of-tail-call-optimizat...
- swsieber 6y agoCan't one fake TCO by hoisting things out into a loop? > Tail calls: I can't think of a Rust or C++ abstraction that relies on TCO to be zero-cost. Rust doesn't have TCO, so it's hard to rely on something that doesn't exist (unless it's on nightly and I missed it).
- steveklabnik 6y agoRust doesn't guarantee TCO, but LLVM may decide to do it if it wishes.
- Gibbon1 6y agoLack of a guarantee says you should use explicit loops.
- majewsky 6y agoThat's assuming the tail call is visible in human-level source code. You could also have a situation where A tail-calls B, B tail-calls C and C tail-calls A. If B and C get inlined, you now have the opportunity to convert A's self-tail-call into a loop. Refactoring A manually may not be desirable, e.g. if inlining B and C cause significant code duplication, or if they live in entirely different libraries.
- heavenlyblue 6y agoThis is obviously a mistake if that happened, are you proposing for the compilers to “fix” this bug automatically until someone changes one of these functions to not do a tail call and it explodes out of the blue?
- majewsky 6y agoWhy would that be a mistake?
- steveklabnik 6y agoI agree. Not sure why you're downvoted.
- roca 6y agoIn my experience, Rust and C++ developers seldom use tail recursion with the expectation that it's zero-cost. In almost all cases it's much more natural to use a loop in those languages.
- CyberDildonics 6y agoWhy use recursion for iteration if the language already makes iteration easy?