[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Stack level too deep

marcus

2/28/2006 7:16:00 AM

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



7 Answers

Meinrad Recheis

2/28/2006 7:27:00 AM

0

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)


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

Robert Klemme

2/28/2006 8:50:00 AM

0

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

robert

Meinrad Recheis

2/28/2006 10:51:00 AM

0

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.
-- henon

Robert Klemme

2/28/2006 11:29:00 AM

0

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. :-)

robert

marcus

2/28/2006 4:14:00 PM

0

Robert Klemme skrev:
> 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. :-)
>
> 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




Logan Capaldo

3/1/2006 2:04:00 AM

0


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...

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.

Eric Christensen

3/1/2006 2:44:00 AM

0

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-....