Forum: Ruby-core [ruby-trunk - Bug #9114][Open] InstructionSequence.compile w/tailcall_optimization: true, trace_inst

8838005371ab9c0b1d40f0504bf8832a?d=identicon&s=25 Gary Weaver (garysweaver)
on 2013-11-15 18:38
(Received via mailing list)
Issue #9114 has been reported by garysweaver (Gary Weaver).

----------------------------------------
Bug #9114: InstructionSequence.compile w/tailcall_optimization: true,
trace_instruction: false not working as expected
https://bugs.ruby-lang.org/issues/9114

Author: garysweaver (Gary Weaver)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin11.4.2]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


Code to reproduce is a recursive sort I wrote; I was trying to compile
RubyVM::InstructionSequence with tailcall_optimization: true,
trace_instruction: false, which has worked before, but not for this
case.

method_string = <<RUBY
def recursively_sort(obj, in_sort_by=false)
 if obj.respond_to?(:sort_by)
   obj.sort_by{|*args|args.map{|v|recursively_sort(v, true)}}
 end rescue nil
 in_sort_by ? (obj.to_s rescue obj.inspect rescue nil) : obj
end
RUBY

RubyVM::InstructionSequence.new(method_string, nil, nil, nil,
tailcall_optimization: true, trace_instruction: false).eval

a = eval '[{b:['*1000 + '2,1' + '],a:1}]'*1000

recursively_sort a

results in error for latest releases of Ruby 1.9.3 and 2.0.0:

1.9.3-p448 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!

2.0.0p247 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!

I had assumed that since the blocks were defined within the tail call
optimized method that referenced the method compiled with TCO, it would
still be tail call optimized.

Did I do something wrong/is there a suggested workaround?

Thanks for looking at this!
8838005371ab9c0b1d40f0504bf8832a?d=identicon&s=25 Gary Weaver (garysweaver)
on 2013-11-15 18:56
(Received via mailing list)
Issue #9114 has been updated by garysweaver (Gary Weaver).


btw- that method is incorrect :) - this case just happened expose the
issue. (Sorry for the bad code example.)

----------------------------------------
Bug #9114: InstructionSequence.compile w/tailcall_optimization: true,
trace_instruction: false not working as expected
https://bugs.ruby-lang.org/issues/9114#change-42968

Author: garysweaver (Gary Weaver)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin11.4.2]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


Code to reproduce is a recursive sort I wrote; I was trying to compile
RubyVM::InstructionSequence with tailcall_optimization: true,
trace_instruction: false, which has worked before, but not for this
case.

method_string = <<RUBY
def recursively_sort(obj, in_sort_by=false)
 if obj.respond_to?(:sort_by)
   obj.sort_by{|*args|args.map{|v|recursively_sort(v, true)}}
 end rescue nil
 in_sort_by ? (obj.to_s rescue obj.inspect rescue nil) : obj
end
RUBY

RubyVM::InstructionSequence.new(method_string, nil, nil, nil,
tailcall_optimization: true, trace_instruction: false).eval

a = eval '[{b:['*1000 + '2,1' + '],a:1}]'*1000

recursively_sort a

results in error for latest releases of Ruby 1.9.3 and 2.0.0:

1.9.3-p448 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!

2.0.0p247 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!

I had assumed that since the blocks were defined within the tail call
optimized method that referenced the method compiled with TCO, it would
still be tail call optimized.

Did I do something wrong/is there a suggested workaround?

Thanks for looking at this!
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 drbrain (Eric Hodel) (Guest)
on 2013-11-15 22:54
(Received via mailing list)
Issue #9114 has been updated by drbrain (Eric Hodel).


Which call is in tail position for this method?  I'm not seeing it.
----------------------------------------
Bug #9114: InstructionSequence.compile w/tailcall_optimization: true,
trace_instruction: false not working as expected
https://bugs.ruby-lang.org/issues/9114#change-42970

Author: garysweaver (Gary Weaver)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin11.4.2]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


Code to reproduce is a recursive sort I wrote; I was trying to compile
RubyVM::InstructionSequence with tailcall_optimization: true,
trace_instruction: false, which has worked before, but not for this
case.

method_string = <<RUBY
def recursively_sort(obj, in_sort_by=false)
 if obj.respond_to?(:sort_by)
   obj.sort_by{|*args|args.map{|v|recursively_sort(v, true)}}
 end rescue nil
 in_sort_by ? (obj.to_s rescue obj.inspect rescue nil) : obj
end
RUBY

RubyVM::InstructionSequence.new(method_string, nil, nil, nil,
tailcall_optimization: true, trace_instruction: false).eval

a = eval '[{b:['*1000 + '2,1' + '],a:1}]'*1000

recursively_sort a

results in error for latest releases of Ruby 1.9.3 and 2.0.0:

1.9.3-p448 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!

2.0.0p247 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!

I had assumed that since the blocks were defined within the tail call
optimized method that referenced the method compiled with TCO, it would
still be tail call optimized.

Did I do something wrong/is there a suggested workaround?

Thanks for looking at this!
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 drbrain (Eric Hodel) (Guest)
on 2013-11-16 01:47
(Received via mailing list)
Issue #9114 has been updated by drbrain (Eric Hodel).


=begin
Here's an update where a call is in tail position, but from the output
you'll see only the initial call uses the tail call optimization.

  source = <<-SOURCE
  def fact n, acc = 1
    if n.zero?
      acc
    else
      fact n - 1, acc * n
    end
  end

  fact 10000
  SOURCE

  i_seq = RubyVM::InstructionSequence.new source, nil, nil, nil,
    tailcall_optimization: false

  puts i_seq.disasm

  begin
    value = i_seq.eval

    p value
  rescue SystemStackError => e
    puts e
  end

  i_seq = RubyVM::InstructionSequence.new source, nil, nil, nil,
    tailcall_optimization: true

  puts i_seq.disasm

  begin
    value = i_seq.eval

    p value
  rescue SystemStackError => e
    puts e
  end

=end
----------------------------------------
Bug #9114: InstructionSequence.compile w/tailcall_optimization: true,
trace_instruction: false not working as expected
https://bugs.ruby-lang.org/issues/9114#change-42971

Author: garysweaver (Gary Weaver)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin11.4.2]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


Code to reproduce is a recursive sort I wrote; I was trying to compile
RubyVM::InstructionSequence with tailcall_optimization: true,
trace_instruction: false, which has worked before, but not for this
case.

method_string = <<RUBY
def recursively_sort(obj, in_sort_by=false)
 if obj.respond_to?(:sort_by)
   obj.sort_by{|*args|args.map{|v|recursively_sort(v, true)}}
 end rescue nil
 in_sort_by ? (obj.to_s rescue obj.inspect rescue nil) : obj
end
RUBY

RubyVM::InstructionSequence.new(method_string, nil, nil, nil,
tailcall_optimization: true, trace_instruction: false).eval

a = eval '[{b:['*1000 + '2,1' + '],a:1}]'*1000

recursively_sort a

results in error for latest releases of Ruby 1.9.3 and 2.0.0:

1.9.3-p448 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!

2.0.0p247 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!

I had assumed that since the blocks were defined within the tail call
optimized method that referenced the method compiled with TCO, it would
still be tail call optimized.

Did I do something wrong/is there a suggested workaround?

Thanks for looking at this!
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 drbrain (Eric Hodel) (Guest)
on 2013-11-18 22:11
(Received via mailing list)
Issue #9114 has been updated by drbrain (Eric Hodel).

Status changed from Open to Assigned
Assignee set to ko1 (Koichi Sasada)


----------------------------------------
Bug #9114: InstructionSequence.compile w/tailcall_optimization: true,
trace_instruction: false not working as expected
https://bugs.ruby-lang.org/issues/9114#change-43003

Author: garysweaver (Gary Weaver)
Status: Assigned
Priority: Normal
Assignee: ko1 (Koichi Sasada)
Category:
Target version:
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin11.4.2]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


Code to reproduce is a recursive sort I wrote; I was trying to compile
RubyVM::InstructionSequence with tailcall_optimization: true,
trace_instruction: false, which has worked before, but not for this
case.

method_string = <<RUBY
def recursively_sort(obj, in_sort_by=false)
 if obj.respond_to?(:sort_by)
   obj.sort_by{|*args|args.map{|v|recursively_sort(v, true)}}
 end rescue nil
 in_sort_by ? (obj.to_s rescue obj.inspect rescue nil) : obj
end
RUBY

RubyVM::InstructionSequence.new(method_string, nil, nil, nil,
tailcall_optimization: true, trace_instruction: false).eval

a = eval '[{b:['*1000 + '2,1' + '],a:1}]'*1000

recursively_sort a

results in error for latest releases of Ruby 1.9.3 and 2.0.0:

1.9.3-p448 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!

2.0.0p247 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!

I had assumed that since the blocks were defined within the tail call
optimized method that referenced the method compiled with TCO, it would
still be tail call optimized.

Did I do something wrong/is there a suggested workaround?

Thanks for looking at this!
8838005371ab9c0b1d40f0504bf8832a?d=identicon&s=25 Gary Weaver (garysweaver)
on 2013-11-27 23:43
(Received via mailing list)
Issue #9114 has been updated by garysweaver (Gary Weaver).


Eric,

My apologizes as I probably wasted your time with that. The problem with
the code you posted is that for TCO you still have to specify
trace_instruction: false. If you execute the following, it is fine in
Ruby 2.0.0 at least, unless it doesn't work in trunk/master:

source = <<-SOURCE
def fact n, acc = 1
  if n.zero?
    acc
  else
    fact n - 1, acc * n
  end
end

fact 10000
SOURCE

i_seq = RubyVM::InstructionSequence.new source, nil, nil, nil,
  tailcall_optimization: true, trace_instruction: false

puts i_seq.disasm

begin
  value = i_seq.eval

  p value
rescue SystemStackError => e
  puts e
end


---

I wasn't doing a tail call, which was my problem I think :( , so can
close.
----------------------------------------
Bug #9114: InstructionSequence.compile w/tailcall_optimization: true,
trace_instruction: false not working as expected
https://bugs.ruby-lang.org/issues/9114#change-43216

Author: garysweaver (Gary Weaver)
Status: Assigned
Priority: Normal
Assignee: ko1 (Koichi Sasada)
Category:
Target version:
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin11.4.2]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


Code to reproduce is a recursive sort I wrote; I was trying to compile
RubyVM::InstructionSequence with tailcall_optimization: true,
trace_instruction: false, which has worked before, but not for this
case.

method_string = <<RUBY
def recursively_sort(obj, in_sort_by=false)
 if obj.respond_to?(:sort_by)
   obj.sort_by{|*args|args.map{|v|recursively_sort(v, true)}}
 end rescue nil
 in_sort_by ? (obj.to_s rescue obj.inspect rescue nil) : obj
end
RUBY

RubyVM::InstructionSequence.new(method_string, nil, nil, nil,
tailcall_optimization: true, trace_instruction: false).eval

a = eval '[{b:['*1000 + '2,1' + '],a:1}]'*1000

recursively_sort a

results in error for latest releases of Ruby 1.9.3 and 2.0.0:

1.9.3-p448 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!

2.0.0p247 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!

I had assumed that since the blocks were defined within the tail call
optimized method that referenced the method compiled with TCO, it would
still be tail call optimized.

Did I do something wrong/is there a suggested workaround?

Thanks for looking at this!
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 drbrain (Eric Hodel) (Guest)
on 2013-12-02 05:44
(Received via mailing list)
Issue #9114 has been updated by drbrain (Eric Hodel).

Status changed from Assigned to Rejected

No time was wasted, I wanted to be sure your use-case was understood.

As you requested, this issue is now closed.
----------------------------------------
Bug #9114: InstructionSequence.compile w/tailcall_optimization: true,
trace_instruction: false not working as expected
https://bugs.ruby-lang.org/issues/9114#change-43340

Author: garysweaver (Gary Weaver)
Status: Rejected
Priority: Normal
Assignee: ko1 (Koichi Sasada)
Category:
Target version:
ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674)
[x86_64-darwin11.4.2]
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN


Code to reproduce is a recursive sort I wrote; I was trying to compile
RubyVM::InstructionSequence with tailcall_optimization: true,
trace_instruction: false, which has worked before, but not for this
case.

method_string = <<RUBY
def recursively_sort(obj, in_sort_by=false)
 if obj.respond_to?(:sort_by)
   obj.sort_by{|*args|args.map{|v|recursively_sort(v, true)}}
 end rescue nil
 in_sort_by ? (obj.to_s rescue obj.inspect rescue nil) : obj
end
RUBY

RubyVM::InstructionSequence.new(method_string, nil, nil, nil,
tailcall_optimization: true, trace_instruction: false).eval

a = eval '[{b:['*1000 + '2,1' + '],a:1}]'*1000

recursively_sort a

results in error for latest releases of Ruby 1.9.3 and 2.0.0:

1.9.3-p448 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!

2.0.0p247 :014 >   recursively_sort a
SystemStackError: stack level too deep
  from
/Users/gary/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!

I had assumed that since the blocks were defined within the tail call
optimized method that referenced the method compiled with TCO, it would
still be tail call optimized.

Did I do something wrong/is there a suggested workaround?

Thanks for looking at this!
This topic is locked and can not be replied to.