Project

General

Profile

Actions

Feature #14605

open

Remove `original_iseq` from `rb_iseq_constant_body`

Added by tenderlovemaking (Aaron Patterson) over 6 years ago. Updated about 5 years ago.

Status:
Open
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.


Files

Updated by ko1 (Koichi Sasada) over 6 years 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"?

Updated by tenderlovemaking (Aaron Patterson) over 6 years 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?

Updated by ko1 (Koichi Sasada) over 6 years 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.

Updated by tenderlovemaking (Aaron Patterson) over 6 years 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

Actions #5

Updated by jeremyevans0 (Jeremy Evans) about 5 years ago

  • Tracker changed from Bug to Feature
  • Backport deleted (2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0