I have a tree that I do recursion over the nodes (and then some
recursion inside the nodes…). Two problems:
Stack level too deep (error message)
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
I have a tree that I do recursion over the nodes (and then some
recursion inside the nodes…). Two problems:
Stack level too deep (error message)
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…
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
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…
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.
group :development, :test do
gem ‘rspec-rails’, ‘>= 2.0.0.beta.19’
gem ‘capybara’
end
I am not sure if this is correct or what to do next. I tried “bundle
install” and then “rspec spec/requests”. Capybara was installed but I
still got the stack level too deep error. Could you tell me what to do
or point me to an online resource where this is documented?
or point me to an online resource where this is documented?
Did you remove webrat from the Gemfile?
That won’t help with beta.19, which has a hard dependency on webrat.
beta.20 does not, so you can choose between webrat and capybara.
LayoutLinks should have a Home page at ‘/’
Failure/Error: response.should have_selector(‘title’, :content =>
“Home”)
undefined method `has_selector?’ for
#ActionDispatch::TestResponse:0x7f0f4ca18cb0
Have selector is a webrat matcher. If you’re not using webrat you
can’t use it. Capybara’s matcher is have_css. I suggest you take a
look at capybara docs.
Have selector is a webrat matcher. If you’re not using webrat you
can’t use it. Capybara’s matcher is have_css. I suggest you take a
look at capybara docs.