SlideShare a Scribd company logo
Tail-call Optimization
(TCO)
What is tail-call optimization?
A compiler optimization that allows

the efficient use of procedure calls in
the tail position
What’s a procedure?
Blocks of callable code
 Functions

 Methods
 Subroutines

 Routines
What does it mean for a procedure
call to be in tail position?
A procedure call is in tail position if it’s
the last thing to happen before

returning
int a = bar():
return a + 1;
}

Example 1

public static void foo1(){
int a = bar():
return a;
}

Example 2

public static void foo2(){
Where does the inefficiency
come from?
Call stack
Container for storing procedure calls
Grows upward
Last in, first out (LIFO)
Composed of stack frames
Tail call optimization (TCO) - Lecture
Keeping with current analogies…
 Each pancake represents some procedure to be
stepped through (some computation).
 The plate represents boilerplate used by the runtime
system to return from procedures once they’ve run to
completion.
 Boilerplate might consist of return addresses, formal
parameters, etc.
What’s in a stack frame?
Each stack frame is made of up of:
 Some computation to run (a pancake)
 Some boilerplate used for returning (a plate)

So our stack really looks more like this…
Tail call optimization (TCO) - Lecture
Specifically…

↵

b()
a()
main()
Fine-tuning the analogy…
Remember, we consider a pancake
some amount of computation/work that
needs to be performed before a
procedure can return.
So we would expect the pancake to
vary in size over the course of its lifetime.
Concretely…

;

b()
a()
↵
main()
What would the stack look like if
our functions made use of tail
calls?
Probably something like this…

b()

↵

a()
main()
So where’s the inefficiency?
Oh no, stack overflow!
Our stack has to exist in memory, so
it has a finite size.

If we have too many stack frames,
eventually we’ll run out of room
and get a stack overflow.
A solution approaches
 If procedure calls are in tail position, we can
pop their remaining stack frame, since the
plate is all that’s left.

 Since stack frames aren’t something the
programmer deals with explicitly, we let the
compiler take care of the details.
 Thus we have tail call elimination/optimization.
(TCE/TCO)
A comparison
Without TCO

With TCO

b()
a()

main()

b()
main()
Why should I care about this?
Benefits of TCO
You can solve problems efficiently using
recursion.

You can make efficient use of function
calls within procedures.
More stack space means more room for
the procedures that aren’t able to make
use of tail calls.
More examples:
https://gist.github.com/numberten/4acb8e7e8988caba8b5d
Related Wikipedia pages
 http://en.wikipedia.org/wiki/Call_stack
 http://en.wikipedia.org/wiki/Tail_call
 http://en.wikipedia.org/wiki/Subroutines
 http://en.wikipedia.org/wiki/Mutual_recursion
 http://en.wikipedia.org/wiki/Structured_programming

More Related Content

PPTX
Compiler optimization techniques
Hardik Devani
 
PPTX
Peephole Optimization
Meghaj Mallick
 
PDF
Assembly language part I
Mohammed A. Imran
 
PDF
A Simple Communication System Design Lab #4 with MATLAB Simulink
Jaewook. Kang
 
PDF
A peek on numerical programming in perl and python e christopher dyken 2005
Jules Krdenas
 
PDF
A Simple Communication System Design Lab #3 with MATLAB Simulink
Jaewook. Kang
 
PPS
Aae oop xp_06
Niit Care
 
PDF
A Simple Communication System Design Lab #2 with MATLAB Simulink
Jaewook. Kang
 
Compiler optimization techniques
Hardik Devani
 
Peephole Optimization
Meghaj Mallick
 
Assembly language part I
Mohammed A. Imran
 
A Simple Communication System Design Lab #4 with MATLAB Simulink
Jaewook. Kang
 
A peek on numerical programming in perl and python e christopher dyken 2005
Jules Krdenas
 
A Simple Communication System Design Lab #3 with MATLAB Simulink
Jaewook. Kang
 
Aae oop xp_06
Niit Care
 
A Simple Communication System Design Lab #2 with MATLAB Simulink
Jaewook. Kang
 

What's hot (20)

PDF
A Simple Communication System Design Lab #1 with MATLAB Simulink
Jaewook. Kang
 
PPT
Lecture 1
guest6c6268
 
PDF
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Peng Cheng
 
PPTX
190111 tf2 preview_jwkang_pub
Jaewook. Kang
 
PDF
The low level awesomeness of Go
Jean-Bernard Jansen
 
PDF
Taming OpenBSD Network Stack Dragons by Martin Pieuchot
eurobsdcon
 
PDF
Concurrency in Python4k
Rodolfo Carvalho
 
DOCX
project_2
Tanya Srivastava
 
PPTX
Recursion
Ashish Ranjan
 
PPTX
Tensorflow in practice by Engineer - donghwi cha
Donghwi Cha
 
ZIP
なぜ検索しなかったのか
N Masahiro
 
PDF
An evaluation of LLVM compiler for SVE with fairly complicated loops
Linaro
 
PDF
Arm tools and roadmap for SVE compiler support
Linaro
 
PPT
GEM - GNU C Compiler Extensions Framework
Alexey Smirnov
 
PDF
Parallel Programming
Roman Okolovich
 
PDF
Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...
Yusuke Izawa
 
PDF
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Yusuke Izawa
 
PDF
Return Oriented Programming
UTD Computer Security Group
 
A Simple Communication System Design Lab #1 with MATLAB Simulink
Jaewook. Kang
 
Lecture 1
guest6c6268
 
Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021
Peng Cheng
 
190111 tf2 preview_jwkang_pub
Jaewook. Kang
 
The low level awesomeness of Go
Jean-Bernard Jansen
 
Taming OpenBSD Network Stack Dragons by Martin Pieuchot
eurobsdcon
 
Concurrency in Python4k
Rodolfo Carvalho
 
project_2
Tanya Srivastava
 
Recursion
Ashish Ranjan
 
Tensorflow in practice by Engineer - donghwi cha
Donghwi Cha
 
なぜ検索しなかったのか
N Masahiro
 
An evaluation of LLVM compiler for SVE with fairly complicated loops
Linaro
 
Arm tools and roadmap for SVE compiler support
Linaro
 
GEM - GNU C Compiler Extensions Framework
Alexey Smirnov
 
Parallel Programming
Roman Okolovich
 
Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...
Yusuke Izawa
 
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Yusuke Izawa
 
Return Oriented Programming
UTD Computer Security Group
 
Ad

Similar to Tail call optimization (TCO) - Lecture (6)

PPTX
Tail Recursion in data structure
Rumman Ansari
 
PPTX
Tail recursion
Rumman Ansari
 
PPTX
2.0 Stacks.pptx
MuhammadShajid1
 
PPTX
Tail recursion
SOURAVBHUNIA11
 
PPTX
class-Stacks.pptx
ssuser2efca7
 
PDF
C++ Tail Recursion Using 64-bit variables
PVS-Studio
 
Tail Recursion in data structure
Rumman Ansari
 
Tail recursion
Rumman Ansari
 
2.0 Stacks.pptx
MuhammadShajid1
 
Tail recursion
SOURAVBHUNIA11
 
class-Stacks.pptx
ssuser2efca7
 
C++ Tail Recursion Using 64-bit variables
PVS-Studio
 
Ad

Recently uploaded (20)

PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
CDH. pptx
AneetaSharma15
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 

Tail call optimization (TCO) - Lecture

  • 2. What is tail-call optimization? A compiler optimization that allows the efficient use of procedure calls in the tail position
  • 3. What’s a procedure? Blocks of callable code  Functions  Methods  Subroutines  Routines
  • 4. What does it mean for a procedure call to be in tail position? A procedure call is in tail position if it’s the last thing to happen before returning
  • 5. int a = bar(): return a + 1; } Example 1 public static void foo1(){
  • 6. int a = bar(): return a; } Example 2 public static void foo2(){
  • 7. Where does the inefficiency come from?
  • 8. Call stack Container for storing procedure calls Grows upward Last in, first out (LIFO) Composed of stack frames
  • 10. Keeping with current analogies…  Each pancake represents some procedure to be stepped through (some computation).  The plate represents boilerplate used by the runtime system to return from procedures once they’ve run to completion.  Boilerplate might consist of return addresses, formal parameters, etc.
  • 11. What’s in a stack frame? Each stack frame is made of up of:  Some computation to run (a pancake)  Some boilerplate used for returning (a plate) So our stack really looks more like this…
  • 14. Fine-tuning the analogy… Remember, we consider a pancake some amount of computation/work that needs to be performed before a procedure can return. So we would expect the pancake to vary in size over the course of its lifetime.
  • 16. What would the stack look like if our functions made use of tail calls?
  • 17. Probably something like this… b() ↵ a() main()
  • 18. So where’s the inefficiency?
  • 19. Oh no, stack overflow! Our stack has to exist in memory, so it has a finite size. If we have too many stack frames, eventually we’ll run out of room and get a stack overflow.
  • 20. A solution approaches  If procedure calls are in tail position, we can pop their remaining stack frame, since the plate is all that’s left.  Since stack frames aren’t something the programmer deals with explicitly, we let the compiler take care of the details.  Thus we have tail call elimination/optimization. (TCE/TCO)
  • 21. A comparison Without TCO With TCO b() a() main() b() main()
  • 22. Why should I care about this?
  • 23. Benefits of TCO You can solve problems efficiently using recursion. You can make efficient use of function calls within procedures. More stack space means more room for the procedures that aren’t able to make use of tail calls.
  • 25. Related Wikipedia pages  http://en.wikipedia.org/wiki/Call_stack  http://en.wikipedia.org/wiki/Tail_call  http://en.wikipedia.org/wiki/Subroutines  http://en.wikipedia.org/wiki/Mutual_recursion  http://en.wikipedia.org/wiki/Structured_programming