Tail Call Optimization

Поделиться
HTML-код
  • Опубликовано: 13 июл 2024
  • We hear about tail calls from time to time in the world of programming, and how they can be optimized for performance. What exactly is that, and how would such optimization work? Today, we take a closer look at tail calls, in addition to a common use case - Tail call recursion!
    = Contents Page =
    0:00 Introduction
    0:42 Contents Page
    1:13 Foundation: Code and RAM
    1:59 Functions and RAM
    2:22 The "Return" Problem
    2:57 The Call Stack
    4:06 Introduction to Tail Calls
    4:39 Need for Tail Call Optimization
    5:27 Tail Call Optimization
    6:14 Tail Call Recursion
    7:14 Benefits of Tail Calls
    8:02 Conclusion
    = 0612 TV =
    0612 TV, a sub-project of NERDfirst.net, is an educational RUclips channel. Started in 2008, we have now covered a wide range of topics, from areas such as Programming, Algorithms and Computing Theories, Computer Graphics, Photography, and Specialized Guides for using software such as FFMPEG, Deshaker, GIMP and more!
    Enjoy your stay, and don't hesitate to drop me a comment or a personal message to my inbox =) If you like my work, don't forget to subscribe!
    Like what you see? Buy me a coffee → www.nerdfirst.net/donate/
    0612 TV Official Writeup: nerdfirst.net/0612tv
    More about me: about.me/lcc0612
    Official Twitter: / 0612tv
    = NERDfirst =
    NERDfirst is a project allowing me to go above and beyond RUclips videos into areas like app and game development. It will also contain the official 0612 TV blog and other resources.
    Watch this space, and keep your eyes peeled on this channel for more updates! nerdfirst.net/
    -----
    Disclaimer: Please note that any information is provided on this channel in good faith, but I cannot guarantee 100% accuracy / correctness on all content. Contributors to this channel are not to be held responsible for any possible outcomes from your use of the information.

Комментарии • 38

  • @shoshonu7
    @shoshonu7 4 месяца назад

    Unsung YT hero! ✨You're a legend, mate! 👑 One of my favourite videos of yours: Priceless for anyone interested in optimization as long as TCO is implemented in your programming language of choice.

    • @NERDfirst
      @NERDfirst  4 месяца назад

      Hello and thank you very much for your comment! Glad you liked the video =)

  • @cameronmackinnon9073
    @cameronmackinnon9073 Год назад +3

    This is near enough the only effective introduction to this topic on the internet. Thank you!

    • @NERDfirst
      @NERDfirst  Год назад

      Hello and thank you very much for your comment! Very happy to be of help :)

  • @blackswan2020
    @blackswan2020 2 года назад +2

    Most underrated programming channel I have come across so far! Super clear explanations -- thank you!

    • @NERDfirst
      @NERDfirst  2 года назад

      Hello and thank you so much for your comment! Glad you like the video and the rest of my work =)

  • @user-dp5ek6yc9u
    @user-dp5ek6yc9u 4 месяца назад +2

    This is the only video I found that best explain the topic!!!
    Thx A LOT

    • @NERDfirst
      @NERDfirst  4 месяца назад

      Hello and thank you very much for your comment and for the super thanks! Very happy to have been of help =)

  • @lilitghazaryan3851
    @lilitghazaryan3851 3 года назад +3

    Thank you veeeery much for this video!!!! I’ve been seraching all this stuff anywhere, but only your video was that best one which could explain what I wanted exactly. Such a short but very meaningful video, excellent, thank you again:)

    • @NERDfirst
      @NERDfirst  3 года назад

      You're welcome! Very happy to be of help :)

  • @Jan_Jan_
    @Jan_Jan_ 3 месяца назад

    Great explanation - thank you very much.

    • @NERDfirst
      @NERDfirst  3 месяца назад +1

      You're welcome! Very happy to be of help :)

  • @ballafolife19
    @ballafolife19 3 года назад

    Just saw your vid from 2014 about serialization. Very concise, and direct, and informative. I originally was searching for what pickling is in Python. You made serialization so easy to understand, that I was able to extract the info and relate it to Python pickle function even though you did not even mention it. Excellent job!

    • @NERDfirst
      @NERDfirst  3 года назад

      Hello and thank you very much for your comment! Glad you liked my work =D

  • @okay1904
    @okay1904 3 года назад +1

    excellent - Interesting. A good teacher with a good understanding of the topic. Never heard of it but now fully understood.

    • @NERDfirst
      @NERDfirst  3 года назад

      Hello and thank you very much for your comment! Glad you liked the video =)

  • @bywavy
    @bywavy Год назад

    Explanation was very clear. Props to you.

    • @NERDfirst
      @NERDfirst  Год назад +1

      Hello and thank you very much for your comment! Very happy to be of help =)

  • @s1n7ax
    @s1n7ax Год назад

    Bruh. This is a fabulous expiration

    • @NERDfirst
      @NERDfirst  Год назад

      Hello and thank you very much for your comment! Glad you liked the video =)

  • @SandeepKumar16
    @SandeepKumar16 Год назад

    Thanks for this awesome content. Great illustrations. Simple is beautiful. All the best. 😊

    • @NERDfirst
      @NERDfirst  Год назад

      Hello and thank you very much for your comment! Glad you liked the video =)

  • @shivatejamulaveesala5491
    @shivatejamulaveesala5491 Год назад

    Tq for clear explanation.

    • @NERDfirst
      @NERDfirst  Год назад

      You're welcome! Very happy to be of help =)

  • @shimadabr
    @shimadabr 10 месяцев назад

    That`s the best explanation on the subject i`ve seen so far. I was watching a couple of videos and yours have the best bottom-up explanation. Only thing i found lacking was some examples with popular recursive functions (like factorial or fibonacci sequence)

    • @NERDfirst
      @NERDfirst  10 месяцев назад

      Hello and thank you for your comment! Yeah that's a good point. Maybe I can revisit this topic sometime. General patterns and how to convert a regular recursive function into tail recursion bears further explanation.

  • @saeedmirzaei1
    @saeedmirzaei1 3 года назад

    You are the one. Thank you so much.

    • @NERDfirst
      @NERDfirst  3 года назад

      You're welcome! Very happy to be of help =)

  • @Eyetrauma
    @Eyetrauma 3 года назад

    Thanks for the overview, this is one of those terms I've certainly heard and had a vague familiarity with but this really brought it into focus.
    Given the rising popularity of functional composition I can see why being able to optimize deeply nested calls would be a real boon!

    • @NERDfirst
      @NERDfirst  3 года назад

      Hello and thank you for your comment! Yes, I was in the same position as you, this seemed vaguely familiar but I never looked into it until recently. And yes, it's only because I was delving into functional programming!

  • @jeffg4686
    @jeffg4686 Год назад

    Great video. So, if a recursive function is tc optimized, does that mean that stack allocated memory for the function is only allocated once (and reused for all recursive calls)?

    • @NERDfirst
      @NERDfirst  Год назад +2

      Hello and thank you for your comment! This may be implementation-dependent, but yes, that's the idea - Either repeatedly reuse or construct and destroy one call stack entry - As long as the entries don't stack up and "wait" for each other, you'll reap the benefits of the tail call.

  • @theunbearables
    @theunbearables 3 года назад

    what software do you use to make your animations/powerpoint slides?

    • @NERDfirst
      @NERDfirst  3 года назад +2

      Hello and thank you for your comment! All animations in this video were made in PowerPoint.

  • @samanator19
    @samanator19 Год назад

    What would the memory complexity be?

    • @NERDfirst
      @NERDfirst  Год назад

      Hello and thank you for your comment! This one is not so easy to discuss in a vacuum, without knowing what code was written in the function(s) involved. Having said that, the short answer is - The memory usage should be similar to if the program was written as iteration, using loops.
      For certain algorithms that, say, pass arrays around and duplicate them for each recursive call, this could mean space complexity improvements from O(n) or more to O(1), while certain algorithms may not have any space complexity benefits at all.

  • @nyahhbinghi
    @nyahhbinghi Год назад

    Still unclear to me why you wouldn't have 25 call stacks in memory if you did recursion 25 times..I guess they are just clearing out local variables and the call stack early, is that it?

    • @NERDfirst
      @NERDfirst  Год назад +1

      Hello and thank you for your comment! Assuming that tail call _optimization_ is indeed happening, then yes, the call stack entries can be overwritten or deleted once each instance of the function has finished running. You only need to keep track of how to return to the original code that called the function.