Parent article: Python, tail calls, and performance
Actually tail calls successful C person not been astir forever. The C calling normal has been that the callee does not region immoderate worldly the caller has put connected the stack. The caller could spot the declaration int f();, the existent telephone could person n>0 arguments, and the existent usability could person m≤n parameters. That would not ever activity if the callee removed the arguments.
So the caller had to region the arguments betwixt the telephone and the pursuing return, turning the telephone into a non-tail call.
When I looked successful 1994 astatine the C compilers of the day, they did not execute tail-call optimization for the benignant of usage shown successful the article. In 2001 Mark Probst implemented tail-call optimization successful GCC pinch a abstracted calling convention; he lists the limitations of the then-existing tail-call optimization successful GCC successful conception 6.4, among them: "It cannot grip indirect calls" (which would person been utilized successful tail calls for expert dispatch).
I person not looked astatine the rumor since past (GCC's goto * was bully capable (well, mostly)), and I had not overmuch logic for assuming that thing had changed wrt to GCC support for tail-calls (although 1 merchandise statement mentioned sibcalls, and I retrieve reasoning that I should beryllium checking that out.
Anyway, past twelvemonth I publication the insubstantial connected "Copy-and-Patch Compilation" by Xu and Kjolstad, and they usage tail-call optimization. In immoderate case, aft reference that paper, I made immoderate tests if gcc and clang tin do tail-call optimization for the benignant of tail calls shown successful the article. And it works. And Xu and Kjolstad study that they usage 100,000 codification snippets, whereas we limit ourselves successful Gforth to <2000 (for VM instructions, stack caching variations thereof, fixed superinstructions etc.). Being capable to do 100,000 would let america to usage techniques that request excessively galore different codification snippets to beryllium usable successful a goto *-based system.
We person not gotten astir to putting this into Gforth yet, truthful congratulations to the Python organization for being location first.
The LWN tract is presently nether precocious scraper load, truthful comment show has been suppressed for anonymous users. If you are a human, you whitethorn publication the comments by clicking the fastener below:
Note: you tin debar this measurement successful the early by logging into your LWN account.
English (US) ·
Indonesian (ID) ·