Feature #14605
openRemove `original_iseq` from `rb_iseq_constant_body`
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