10 comments

  • drdexebtjl 3 hours ago
    Unless the language can guarantee TCO, I don’t feel comfortable writing tail recursive code and being at the compiler’s/interpreter’s mercy.

    I think the framing of TCO as an optimization has been very unfortunate.

    • kevincox 3 hours ago
      It's hard to argue that it isn't an optimization, because it doesn't affect the semantics of the program. However most optimizations are very hard to observe. The vast majority of optimizations only affect code size and runtime. TCO is one of the few exceptions. It affects memory usage, and more sensitive stack memory at that. This is why a missed optimization can be so much more catastrophic and it is worth considering things like `musttail` attributes so that the code fails to compile rather than misses the optimization.

      I can only think of a few other optimizations that affect memory usage. Register spilling (arguably not really an optimization but a necessity), Rust's niche filling for enum discriminants and C++'s std::vec<bool> (a language-level optimization, arguably a different thing entirely).

      I often think about how few memory optimizations we have. The reason is most likely that they tend to be non-local so are much harder to apply than CPU optimizations that generally have no effect outside of the function they are in.

      • steveklabnik 2 hours ago
        > It's hard to argue that it isn't an optimization, because it doesn't affect the semantics of the program.

        Depends on the semantics of the programming language itself. For some languages, it is truly an optimization, for some, it is required, and does meaningfully change observed semantics.

      • mort96 1 hour ago
        If the semantics of 'while (true)' was "will crash the program after an implementation-defined but often fairly low number of iterations", I would stop using 'while (true)'.
        • cmovq 49 minutes ago
          Note that compilers are more than happy to delete 'while (true)' if the loop doesn't have side-effects.
          • mort96 4 minutes ago
            Of course. I wasn't talking about empty loops.

            But also, I wouldn't rely on a compiler to remove empty 'while (true)' loops.

      • lilbigdoot 1 hour ago
        If my program crashes without it, that's a semantic difference no?
      • jmalicki 2 hours ago
        JVM does a lot of escape analysis to turn heap allocated memory into stack local variables.

        It doesn't matter if it's local since it's a VM, it's doing it at runtime and can change an entire call stack of non local code for an optimization.

        • pjmlp 7 minutes ago
          Some fact correcting, first of all while most people refer to "The JVM", most likely impling OpenJDK, Java is a standard and there are many implementations.

          Which exactly in this subject varies a lot between implementations, on how well escape analysis is done, if there is a JIT cache between JVM executions, or AOT compilation.

          Additionally Valhalla is finally getting added to the language with a new EA made available last week, thus value classes will add yet another way to have stack values.

      • drdexebtjl 2 hours ago
        The main difference is not that it affects memory usage, imo.

        It’s that it makes memory usage bounded when it’s on, and unbounded when it’s off.

        In languages that have guaranteed tail call eliminations, the semantics of tail recursion is the same as that of a loop. So you can express the same iterative algorithm without using iterative code.

      • tialaramex 2 hours ago
        std::vector<bool> is just a terrible specialisation, it isn't an optimisation.

        If std::vector<bool> was an optimisation we couldn't write C++ which blows up because it's actually a bitset, it would be semantically transparent - but that's easy to do even by accident because it's not transparent at all.

        In fact the existing std::vector<bool> should just be named std::growable_bitset or something and then std::vector<bool> would make what you actually wanted like Rust's Vec<bool> does.

    • eterm 1 hour ago
      C# is an interesting case because it shares a common runtime with F#, and F# guarantees TCO in most circumstances ( try / catch can stop it ) .

      There is a "tail" prefix in the intermediate language (IL) bytecode that F# uses but Roslyn, the C# compiler, never emits.

      So unlike F#, whether the same algorithm written in C# becomes a loop depends on JIT behaviour. This means if you're coming to a function cold in C# you can overflow the stack, while if you enter the same function fresh after it's been warmed up, it may have been optimised away by RyuJIT and if so you are able to call it safely for what would be large numbers of recursions.

    • LukeShu 3 hours ago
      GCC has `[[gnu::musttail]] return`.

      But yes, framing TCO as an optimization is unfortunate.

    • throw838489448 2 hours ago
      Some languages have TCO annotation, it throws compiler error if TCO fails. You want stronger type system, not smart compiler guarantees or promises!
      • mort96 1 hour ago
        That's not a type system thing... Whether a call gets TCO'd isn't represented in the type system if it's just an annotation on a return statement.
  • torginus 1 hour ago
    What practical patterns are enabled by TCO in C? My impression is that every tail call can written as a loop much more naturally. Tail calls are important in functional languages where you don't have mutable loop variables.

    And imo they are an ugly hack even there - one of the few core constructs where its readily apparent you're not programming an abstract machine but a real, and limited computer. For example the most natural way to write factorial:

          let rec factorial n = if n <= 1 then 1 else n * factorial (n - 1)
    
    is not tail recursive, and will overflow if the compiler fails to optimize.
    • adrian_b 40 minutes ago
      Not every tail call is for a loop.

      You can have a set of mutually recursive functions, which tail call each other.

      In C you can write state machines using "goto" (the implementations with "switch" are typically much more inefficient), but in languages with guaranteed tail call optimizations you can write a state machine where each state is a function.

      In general, it is frequent enough to call another function as the last step of a function, even when there is no recursion involved. It is quite stupid for a compiler to use a CALL in such instances, instead of using a JMP. The only problem is that the function calling convention must be compatible with this optimization, while traditionally the C language used an inefficient calling convention that is not compatible with optimizations. That convention is a residue of the time when functions could be used without being declared and it should never be used by modern compilers.

      • fluoridation 21 minutes ago
        I can't see why the calling convention could matter as long as the functions agree on the return type. Can you give an example?
  • kenjin4096 6 hours ago
    I think Anton is replying to me in that LWN article IIRC. I personally didn't know C only had tail calls that late and learnt something new there!

    On the other hand, I am pretty new to the compiler space myself, and I count early 2000s as a pretty long time ago, though again it is not that far back considering how long other language implementations had tail calls like in ML or variants since 1980-90s.

  • mmsc 6 hours ago
    and TCO was added then removed from js! https://stackoverflow.com/a/54721813

    This leads to fun stack-overflow bugs too in a lot of js code (one solution is to flatten: https://joshua.hu/javascript-infinite-tail-call-recursion-st...)

    • pfdietz 6 hours ago
      Lack of TCO is also a common footgun for Scheme programmers using Common Lisp.
      • guenthert 5 hours ago
        Only if they are using an insufficiently smart compiler. SBCL handles TCO just fine, as do a number of other implementations, see : https://0branch.com/notes/tco-cl.html
        • mort96 1 hour ago
          If it's not encoded in the language's specification, it's not a feature of the language but just an optimization. You can't rely on optimizations for correctness.
        • pfdietz 5 hours ago
          Even SBCL doesn't do TCO at all times. Compiling at (debug 3) means no TCO.

          Another related footgun is deep recursion of other kinds, for example when recursively traversing down lists. For long lists it's easy to exceed the stack size limit. The common idiom is to recur on list elements, but iterate or map to go along a list.

          • guenthert 5 hours ago
            > Even SBCL doesn't do TCO at all times. Compiling at (debug 3) means no TCO.

            Presumably one intends to debug the code, when setting (debug 3). Then it'll be helpful to see the stack, no?

            > Another related footgun is deep recursion of other kinds, for example when recursively traversing down lists. For long lists it's easy to exceed the stack size limit. The common idiom is to recur on list elements, but iterate or map to go along a list.

            Not going to argue with seasoned lispers here, but IMHO recursive code makes most sense when accessing recursive data structures.

            • ux266478 1 hour ago
              > Presumably one intends to debug the code, when setting (debug 3). Then it'll be helpful to see the stack, no?

              You don't necessarily need to give up TCO to do that though. You just do some bookkeeping and synthesize virtual stack frames. DWARF has native facilities to handle this.

              CL goes the route it does mostly out of history, which includes the fact it has its own debugging ecosystem, more than any fundamental technical reason. There are technical hurdles with doing this in an image-based dynamic compilation model, but it's very far from intractable. Especially if you just do what GHC did and add a DWARF workflow. Most CL users wouldn't ever touch it though, because that's a drastically different debugging model that costs them a lot of ergonomic power, which may even be the reason they're working in CL to begin with.

            • pfdietz 4 hours ago
              One place where this shows up is in parse trees. The grammar for a list of things may involve productions that look like list constructors. This, directly translated into a data structure, would give a very long chain of parse tree nodes dangling off to the right. It's a recursive data structure, but a very deep one for large lists, and traversing it recursively can use a lot of stack.

              This can also be seen as an argument against building parse trees that way. Instead, have a node with an unbounded number of children, the elements of the list.

      • tialaramex 5 hours ago
        This footgun is the reason I'm so enthusiastic about the Rust `become` keyword.

        This proposal would give Rust a specific keyword which says that you intend TCO and so two things happen: 1. The compiler goes to more length to deliver TCO even where it wouldn't "just work" and 2. If it cannot deliver TCO your code doesn't compile, because you asked for TCO.

        • chriswarbo 5 hours ago
          Sounds similar to @tailrec in Scala

          I personally use the phrase "tail call elimination" when it's a requirement that can be relied on; and "tail call optimisation" when it might be implementation-dependent, context-dependent, limited (e.g. to immediate self-calls), etc.

          • tialaramex 3 hours ago
            I am definitely not a Scala expert.

            As I wrote in a sibling comment, the key benefit here is the extra work from the compiler to deliver what you wanted, on top of the diagnostic if it can't.

            I don't know if Scala has the problem that `become` addresses (C++ calls this RAII, but I have no idea what Scala would call it if they have the same idea)

            However in my brief attempt to validate what Scala does do here, I found discussion of "always" optimising to a loop which is a bad sign. Tail recursion is an elegant way to write some loops but that's not the only thing it's useful for, and it seems as though Scala just doesn't care about other cases, at least for @tailrec

            One thing you want TCO for in a language like Rust with lots of monomorphisation is to avoid function call overhead for the deliberately out-of-line slow path in some code. So in this case there was never an implied loop and we're not averting a stack overflow, we wanted to do a single instruction pointer change instead of an expensive function call wrapper. Seems like @tailrec isn't for that.

            • pjmlp 0 minutes ago
              Scala is in the way to get capture checking for effects, which will allow to do RAII like stuff, or borrow checker like stuff for that matter.
            • chriswarbo 1 hour ago
              I jsut did some digging and it seems you're right, it's only for methods which call themselves (which indeed get compiled into a loop, as an entirely local transformation). So not hugely useful.

              Apologies, I've not written Scala for many years; I just recalled that there was a way to annotate tail calls which the compiler checks. I didn't realise it was so limited!

        • jmalicki 2 hours ago
          Does 1 really happen? I would never trust a compiler where 1 was a possibility. If it can work it should.
        • StilesCrisis 5 hours ago
          Sounds like clang::must_tail?
          • tialaramex 5 hours ago
            I am not a Clang expert, but first, obviously that's a C++ attribute and so while Clang can decide what it means in Clang in the programming language itself it has no semantic weight because the ISO document says attributes are always ignorable.

            Secondly however in these languages you often won't naively get TCO because you have at least one local variable which C++ would say has a "non-trivial destructor" or Rust would say "implements Drop". These both mean that naively the "tail call" wasn't actually the last thing to happen, the destructor / Drop::drop happen at the end of the function, after the tail call.

            The proposed become keyword tries to core::mem::drop any such variables, if it succeeds now that tail call is last and we can do TCO, if it fails [e.g. because the variables it wants to drop are needed for the tail call] we can diagnose the problem. I believe the Clang attribute doesn't have this behaviour.

            • StilesCrisis 3 hours ago
              Clang tail-calls aren't guaranteed to work with all C++ code. If you have a non-trivial constructor, as you mention, it will tell you this and fail instead of silently letting you believe you have tail-calls when you don't.
              • tialaramex 0 minutes ago
                I have to say that at least knowing if I didn't get what I wanted is most of the value for me.
              • fweimer 56 minutes ago
                There are far more cases. Some ABIs use callee-saved registers for parameter-passing under certain circumstances, for example. Usually, there are compatibility restrictions on the signatures of the current and tail-called functions beyond the return type, too.

                This is different from Scheme or the MLs (there as a quality-of-implementation feature) where tail calls into arbitrary functions are expected not to lead to space leaks.

            • im3w1l 3 hours ago
              Reordering destructors is not safe in C++, as it's fairly common to rely on objects being destroyed in reverse order and doing stuff like

                A a;
                B b(&a);
              
              In rust the borrow checker would guard against reordering such things, but a caveat is that there might be unsafe code relying on drop-order which the borrow checker would be oblivious to. There could also potentially be objects representing external resources like a temp file where dropping them out of order leads to issues.
              • steveklabnik 2 hours ago
                > In rust the borrow checker would guard against reordering such things

                It doesn't even get that far: Rust guarantees that things drop in reverse order of declaration, full stop.

                One interesting wrinkle here: for struct members, Rust does the opposite of what C++ does. We debated changing it to match, but

                > there might be unsafe code relying on drop-order which the borrow checker would be oblivious to.

                There was no super real compelling argument to choose one direction over the other in the abstract, and "be the same as C++" was not considered important enough to risk breaking unsafe code that relied on the (what was at the time) implementation defined behavior.

                • tialaramex 1 hour ago
                  > It doesn't even get that far: Rust guarantees that things drop in reverse order of declaration, full stop.

                  The drops happen (if implemented) in the same order, but in a different place, half the point of become is to put any needed drops first before the call, as otherwise it's not in tail position and we can't do the optimisation.

                  So the borrowck can become involved if our become foo(bar, &baz) borrows baz but baz's type impl Drop - the diagnostics aren't great today, but then the feature isn't finished so it's not a priority.

                  • steveklabnik 1 hour ago
                    I was talking about regular old today's Rust, not the specifics about become.
              • tialaramex 3 hours ago
                That Rust was in fact always unsound if it would cause problems to core::mem::drop(a); and the `become` call just drops things so it's the same.

                Safe-but-undesirable outcomes are acceptable. For example maybe our tail call ends up reverting a database transaction and we wish it were otherwise. But if the code did compile but wasn't memory safe as a result of this new drop then it was always unsound and shouldn't have existed.

                Just as the guts of some STL classes are very complicated in order to deliver the promised exception safety promises, the guts of unsafe Rust code are often tricky for similar reasons, you are mandated to deliver safety, it's not up to you to say "That's stupid, don't do that" either ensure it won't compile or safely cope.

      • pjmlp 5 hours ago
        Mostly because they forget Scheme is one of the few languages where TCO is part of the language standard, making it a required feature for any compliant implementation.

        This has always been an issue regarding TCO support across programming languages.

        • pfdietz 4 hours ago
          Well, and also because of the "I've been told in Scheme you should do it this way, so by gum I'm going to do it this way!"
    • groundzeros2015 5 hours ago
      Js really should have it. I think the shift in style from functional and manual prototype chains to Java classes is quite disappointing.
      • webstrand 15 minutes ago
        ES6 class syntax is still mostly just syntax sugar overtop prototypical inheritance.

        JS _does_ still have TCO (called Proper Tail Calls), Safari's JavaScriptCore implements it, and is technically the only conforming interpreter.

      • chuckadams 3 hours ago
        Technically TCO is still in the spec, TC39 deadlocked over modifying it. TC39 really does not fill me with confidence in general.
  • nyeah 6 hours ago
    >That quote is the article, and it's a little surprising that it's buried so far into the content

    Is it really surprising in 2026? Today's online writing style is not primarily designed to communicate. It's designed to keep the reader 'engaged' for as long as possible. The reader's time is a resource to be extracted.

    I'm absolutely not poking this author individually. It's the writing style of the net.

  • swiftcoder 5 hours ago
    > In 2001 Mark Probst implemented tail-call optimization in GCC

    MSVC didn't add tail-call optimisation until sometime in the 2010s, IIRC.

    I distinctly remember sending a tail-recursive C++ program to someone who developed on Windows, and it crashing, in the late mid-to-late 2000s.

    • throw-qqqqq 4 hours ago
      MSVC stands for MicroSoft Visual C++ compiler AFAIK.

      It famously doesn’t support a few features of C99.

      They don’t really seem to care much about regular C support (non-C++).

      • pjmlp 4 hours ago
        They officially saw no need for C support going forward.

        https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an...

        Note,

        "If you really need either of the following.....then we recommend that you consider using a different compiler such as Intel or gcc (short-term) and/or pressure your standards committee representatives to have ISO C++ include more of the C standard (longer-term)."

        Which is kind of why nowadays clang is part of Visual Studio as well.

        However, after Satya got into the whole Microsoft <3 FOSS, this changed a bit,

        https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-...

        There are a few blogs after that, so at least up to C17 minus the optional parts from C11, the support is there.

        It remains to be seen if anything C23 or later will ever come into MSVC, and then again, clang is part of VS installer.

        • throw-qqqqq 2 hours ago
          TIL, thanks for updating me on this!

          I read Herb Sutter’s post many years ago, but didn’t know they had picked up the work again.

          I see that VLAs are still not supported, which is a shame IMO, but the C-support seems much better than it used to be at least.

  • steveklabnik 2 hours ago
    (2025)
  • messe 6 hours ago
    > In 2001 Mark Probst implemented tail-call optimization in GCC with a separate calling convention; he lists the limitations of the then-existing tail-call optimization in GCC in section 6.4, among them: "It cannot handle indirect calls" (which would have been used in tail calls for interpreter dispatch).

    Relatively recent being a quarter of century? Or at least a fifth of a century for indirect calls[1] (GCC 3.4.6 is the earliest I see on Compiler Explorer, released March 2006).

    [1]: https://godbolt.org/z/vvcnn54oM

    • coliveira 4 hours ago
      For people who passed their 30s, everything that happened after their 20th birthday is recent. For me, September 11 is recent memory, as well as the 2008 great recession.
    • derdi 4 hours ago
      Given that GCC was first released in 1987, that would mean that tail call optimization, including of indirect calls, has been around for more than half of GCC's lifetime. So it's indeed fair for the parent article to say that "[GCC has] had tail-call optimizations for most of [its] existence".
      • adrian_b 1 hour ago
        That is not exactly true, because for a long time tail call optimizations had a lot of restrictions in gcc, so they could be used only seldom.

        What is said in TFA is correct in the sense that only in recent years the support for tail call optimization became good enough to be able to rely on it, if you use appropriate compilation options.

  • hnfvovpje4 4 hours ago
    Clear, useful, done