Project

General

Profile

Bug #14605

Remove `original_iseq` from `rb_iseq_constant_body`

Added by tenderlovemaking (Aaron Patterson) 9 months ago. Updated 8 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:86117]

Description

I've attached a patch that removes original_iseq from the rb_iseq_constant_body definition. In order to do this, I had to replace rb_iseq_original_iseq with a function that calls a callback along with the decoded instructions. The decoded instructions should be kept alive on the stack, and will automatically get garbage collected when we're done with them. I think this makes it a little harder to access the decoded instructions, but we don't use the encoded instructions very often, and this patch 1) ensures that the decoded instructions get GC'd, and 2) reduces the size of rb_iseq_constant_body.

Here is a script to demonstrate:

require 'objspace'

def foo
  puts "hello"
end

2.times do |i|
  puts "Decode number #{i}"

  iseq = RubyVM::InstructionSequence.of method(:foo)
  x = ObjectSpace.reachable_objects_from(iseq).last
  p ObjectSpace.reachable_objects_from(x)
  iseq.to_a
  p ObjectSpace.reachable_objects_from(x)
end

If you run this with trunk, the output is this:

Decode number 0
["hello", "foo", ["thing.rb", "/Users/aaron/git/ruby/thing.rb"]]
["hello", #<InternalObject:0x00007f80d30072c8 T_STRING>, "foo", ["thing.rb", "/Users/aaron/git/ruby/thing.rb"]]
Decode number 1
["hello", #<InternalObject:0x00007f80d30072c8 T_STRING>, "foo", ["thing.rb", "/Users/aaron/git/ruby/thing.rb"]]
["hello", #<InternalObject:0x00007f80d30072c8 T_STRING>, "foo", ["thing.rb", "/Users/aaron/git/ruby/thing.rb"]]

The first time the instructions are decoded, they get cached in the iseq, and never go away.

With my patch, the output is this:

Decode number 0
["hello", "foo", ["thing.rb", "/Users/aaron/git/ruby/thing.rb"]]
["hello", "foo", ["thing.rb", "/Users/aaron/git/ruby/thing.rb"]]
Decode number 1
["hello", "foo", ["thing.rb", "/Users/aaron/git/ruby/thing.rb"]]
["hello", "foo", ["thing.rb", "/Users/aaron/git/ruby/thing.rb"]]

The diff is kind of large, but I'm mostly moving things around to accommodate the callback.

History

#1 [ruby-core:86352] Updated by ko1 (Koichi Sasada) 9 months ago

but we don't use the encoded instructions very often, and this patch 1) ensures that the decoded instructions get GC'd, and 2) reduces the size of rb_iseq_constant_body.

"we don't use the decoded instructions very often"?

#2 [ruby-core:86409] Updated by tenderlovemaking (Aaron Patterson) 9 months ago

ko1 (Koichi Sasada) wrote:

but we don't use the encoded instructions very often, and this patch 1) ensures that the decoded instructions get GC'd, and 2) reduces the size of rb_iseq_constant_body.

"we don't use the decoded instructions very often"?

I don't think it's a bottleneck, so no reason to cache it in the struct. I don't understand why we would cache this in the struct besides performance?

#3 [ruby-core:86446] Updated by ko1 (Koichi Sasada) 8 months ago

On my comment, I want to make clear that is it a typo of "encoded" -> "decoded" or not.

I don't understand why we would cache this in the struct besides performance?

Maybe it is a historical reason. Ruby 1.9 has an encoded iseq and an original iseq because I didn't have an idea to get an original iseq from encoded iseq.

#4 [ruby-core:86485] Updated by tenderlovemaking (Aaron Patterson) 8 months ago

ko1 (Koichi Sasada) wrote:

On my comment, I want to make clear that is it a typo of "encoded" -> "decoded" or not.

I don't understand why we would cache this in the struct besides performance?

Maybe it is a historical reason. Ruby 1.9 has an encoded iseq and an original iseq because I didn't have an idea to get an original iseq from encoded iseq.

Oops sorry! Yes, it's a typo. :) I mean we don't use the decoded iseq (iseq without direct threading translation) often

Also available in: Atom PDF