Stack level too deep


#1

Hi,

Is there any way for me to increase the stack size? My stack is huge:

playback.rb:38:in `play_back’: stack level too deep (SystemStackError)

Cheers,

-John


#2

By the way, I’m sure it’s not infinite recursion. It’s just the nature
of
my algorithm:

  class PlaybackBase
     def record(&block)
        return PlaybackChain.new(self, block)
     end
  end

  class PlaybackNil < PlaybackBase
     def initialize(root)
        @root = root
     end

     def play_back(&block)
        block.call(@root)
     end
  end

  class PlaybackChain < PlaybackBase
     def initialize(tail, block)
        @tail = tail
        @block = block
     end

     def play_back(&block)
        @block.call(proc { @tail.play_back(&block) })
     end
  end

And it’s better that I use the system stack rather than my own because
it
preserves nice behaviours with blocks

$array1 = [1, 2, 3]
$array2 = [‘a’, ‘b’, ‘c’]

$tail = PlaybackNil.new(nil)

$tail = $tail.record do |block|
$array1.each do |value|
$value1 = value
block.call
end
end

$tail = $tail.record do |block|
$array2.each do |value|
$value2 = value
block.call
end
end

$tail.play_back do
puts “#{$value1}, #{$value2}”
end

Above prints

1, a
2, a
3, a
1, b
2, b
3, b
1, c
2, c
3, c

I build long playback chains like this to replay different combinations
of
things.

-John


#3

Hi John,

Just a friendly suggestion, but this and your previous query about
blocks make me wonder if you shouldn’t be using a functional language?
Ruby has very convenient functional syntax but doesn’t (at least not
to my knowledge) do the same sorts of optimizations that more purely
functional languages will do.

The other option (as I’m watching PragDave’s RubyConf talk on 1.9) is
to use Fibers. Watch the bit of his talk on Fibers at
http://rubyconf2008.confreaks.com/ruby-19-what-to-expect.html
starting about 18 min in.

Cheers,

Josh


#4

John Ky wrote:

Hi,

Is there any way for me to increase the stack size? My stack is huge:

playback.rb:38:in `play_back’: stack level too deep (SystemStackError)

ulimit -s? [there’s also a patch coming soon for 1.8.7 to help with
stack size–stay tuned, I guess–discussed in
http://www.ruby-forum.com/topic/170608#new]
Cheers!
-=R


#5

Sadly, it didn’t work. Instead of getting a system stack error at 3467
levels, I’m now getting the same at 61 levels. Do I not get a new stack
with a new thread?

Not with 1.8. I might query the ruby core group and see if they have
any insight.
-=R


#6

Hi Josh,

Fibres do sound interesting.

Anyhow, I’m stuck with 1.8 at the moment. I had this idea that I could
spawn another thread with a fresh stack and continue the excution on
that.
Sadly, it didn’t work. Instead of getting a system stack error at 3467
levels, I’m now getting the same at 61 levels. Do I not get a new stack
with a new thread?

Thanks

-John


#7

Hi Roger,

It looks as if I create my threads earlier when the stack isn’t so full
gives me threads that have more stack space. So now, I create a thread
pool
at the beginning of my program and use those threads rather than create
the
threads at the point where I know I am running out of stack space.

-John