Forum: Ruby Stack level too deep

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John Ky (Guest)
on 2008-12-03 05:40
(Received via mailing list)
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
John Ky (Guest)
on 2008-12-03 05:58
(Received via mailing list)
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
Joshua B. (Guest)
on 2008-12-03 06:15
(Received via mailing list)
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-...
  starting about 18 min in.

Cheers,

Josh
Roger P. (Guest)
on 2008-12-03 19:45
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
John Ky (Guest)
on 2008-12-05 10:18
(Received via mailing list)
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
Roger P. (Guest)
on 2008-12-05 15:42
> 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
John Ky (Guest)
on 2008-12-07 05:47
(Received via mailing list)
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
This topic is locked and can not be replied to.