# Stack level too deep

**URL:** <https://rubytalk.org/t/stack-level-too-deep/25281>\
**Category:** ruby-talk\
**Created:** [28 February 2006 07:15 UTC](https://rubytalk.org/t/stack-level-too-deep/25281 "2006-02-28T07:15:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcus\_Andersson](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Marcus\_Andersson](https://rubytalk.org/u/Marcus_Andersson)\
**Post date:** [28 February 2006 07:15 UTC](https://rubytalk.org/t/stack-level-too-deep/25281/1 "2006-02-28T07:15:57Z")

</div>

I have a tree that I do recursion over the nodes (and then some recursion inside the nodes...). Two problems:

1) Stack level too deep (error message)  
2) Speed get really really lousy when the tree is deep. With a small shallow tree the speed is nice. I get that the overall time get long but the handling of each node is slow in a deep tree.

Any way around these issues? I should say that it's running in a Rails environment on Win, Ruby 1.8.2

/Marcus

---

<div class="post-metadata">

**Author:** ![Meinrad\_Recheis1](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Meinrad\_Recheis1](https://rubytalk.org/u/Meinrad_Recheis1)\
**Post date:** [28 February 2006 07:26 UTC](https://rubytalk.org/t/stack-level-too-deep/25281/2 "2006-02-28T07:26:39Z")

</div>

what is the maximum depth of the tree? and how do you traverse it?  
-- henon

> **···**
>
> On 2/28/06, marcus \<m-lists@bristav.se\> wrote:
> 
> > I have a tree that I do recursion over the nodes (and then some  
> > recursion inside the nodes...). Two problems:
> > 
> > 1) Stack level too deep (error message)

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [28 February 2006 08:53 UTC](https://rubytalk.org/t/stack-level-too-deep/25281/3 "2006-02-28T08:53:43Z")

</div>

marcus wrote:

> I have a tree that I do recursion over the nodes (and then some  
> recursion inside the nodes...). Two problems:
> 
> 1) Stack level too deep (error message)  
> 2) Speed get really really lousy when the tree is deep. With a small  
> shallow tree the speed is nice. I get that the overall time get long  
> but the handling of each node is slow in a deep tree.
> 
> Any way around these issues? I should say that it's running in a Rails  
> environment on Win, Ruby 1.8.2

The easiest change is to use BFS instead of DFS - if that's possible in  
your scenario. Alternatives:

- implement a DFS with your own kind of stack

- change the way you store things to avoid such a deep recursion

Also, I guess you made sure that there is no loop, i.e. the structure is  
actually a tree...

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Eric\_Christensen](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@Eric\_Christensen](https://rubytalk.org/u/Eric_Christensen)\
**Post date:** [1 March 2006 02:43 UTC](https://rubytalk.org/t/stack-level-too-deep/25281/4 "2006-03-01T02:43:40Z")

</div>

marcus wrote:

> 1) Stack level too deep (error message)

You can change the stack size of the Ruby executable with editbin. It's  
part of Visual Studio.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Meinrad\_Recheis1](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Meinrad\_Recheis1](https://rubytalk.org/u/Meinrad_Recheis1)\
**Post date:** [28 February 2006 10:50 UTC](https://rubytalk.org/t/stack-level-too-deep/25281/5 "2006-02-28T10:50:55Z")

</div>

[...]

i now remember that i had a similar problem with a deep recursion and stack  
depth. i solved  
it by converting the recursive function into a while loop. you can transform  
every recursion  
into a loop, but it will not be as easy to read anymore.  
-- henon

> **···**
>
> On 2/28/06, Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > - change the way you store things to avoid such a deep recursion

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [28 February 2006 11:33 UTC](https://rubytalk.org/t/stack-level-too-deep/25281/6 "2006-02-28T11:33:37Z")

</div>

Meinrad Recheis wrote:

> **···**
>
> > On 2/28/06, Robert Klemme \<bob.news@gmx.net\> wrote:
> > 
> > > - change the way you store things to avoid such a deep recursion
> > 
> > [...]
> > 
> > i now remember that i had a similar problem with a deep recursion and  
> > stack depth. i solved  
> > it by converting the recursive function into a while loop. you can  
> > transform every recursion  
> > into a loop, but it will not be as easy to read anymore.
> 
> That's exactly the other recommendation I gave. 🙂
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Marcus\_Andersson](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Marcus\_Andersson](https://rubytalk.org/u/Marcus_Andersson)\
**Post date:** [28 February 2006 16:14 UTC](https://rubytalk.org/t/stack-level-too-deep/25281/7 "2006-02-28T16:14:15Z")

</div>

Robert Klemme skrev:

> Meinrad Recheis wrote:
> 
> > > - change the way you store things to avoid such a deep recursion
> > 
> > [...]
> > 
> > i now remember that i had a similar problem with a deep recursion and  
> > stack depth. i solved  
> > it by converting the recursive function into a while loop. you can  
> > transform every recursion  
> > into a loop, but it will not be as easy to read anymore.
> 
> That's exactly the other recommendation I gave. 🙂
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

I haven't received the other mail(s) that is talked about. However:

I'm implementing the composite pattern using acts\_as\_tree and STI in ActiveRecord. The thing that is modeled is nodes in a web site structure that is kind of tree structured (isn't all web sites?). So I try to loop over the nodes to do various operations (generate navigations, publish, distribute etc).

I'm sure that there are ways around this but then you have to code around stuff because of the language implementation and that doesn't feel good at all...

/Marcus

> **···**
>
> > > On 2/28/06, Robert Klemme \<bob.news@gmx.net\> wrote:

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [1 March 2006 02:03 UTC](https://rubytalk.org/t/stack-level-too-deep/25281/8 "2006-03-01T02:03:54Z")

</div>

Well maybe you can take smaller steps to coding around the implementation, first code a tail-recursive version and then convert that into an iterative version.

> **···**
>
> On Feb 28, 2006, at 11:14 AM, marcus wrote:
> 
> > I'm sure that there are ways around this but then you have to code around stuff because of the language implementation and that doesn't feel good at all...
