# Memory allocation in recursion

**URL:** <https://discourse.julialang.org/t/memory-allocation-in-recursion/111929>\
**Category:** Performance\
**Tags:** recursion\
**Created:** [March 21, 2024, 1:00am UTC](https://discourse.julialang.org/t/memory-allocation-in-recursion/111929 "2024-03-21T01:00:50Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [March 21, 2024, 10:37am UTC](https://discourse.julialang.org/t/memory-allocation-in-recursion/111929/5 "2024-03-21T10:37:19Z")

</div>

> [@Recursion in Julia — bad idea?](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/11):
>
> Next level: perfect to learn about tail recursion and it’s optimization.

I was about to say “but julia doesn’t do TCO”. But you’re absolutely right, it is both educational and more readable / honest / transparent to write

```julia-auto
function foo(x,y,x)
  #...
    return foo(a,b,c) # tail-call
  #...
end

```

as

```julia-auto
function foo(x,y,z)
  @label entry
  #...
    x,y,z = a,b,c
    @goto entry
  #...
end

```

> [@PetrKryslUCSD](#):
>
> I see a ton of memory allocation in a recursive quicksort…

I would not be surprised if the recursion stumped escape analysis and caused views to be allocated instead of SROA-ed.

---

_[View the full topic](https://discourse.julialang.org/t/memory-allocation-in-recursion/111929)._
