Project

General

Profile

Actions

Feature #18835

open

Add InstructionSequence#type method

Added by tenderlovemaking (Aaron Patterson) about 2 months ago.

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

Description

This method returns a symbol representing the type of the instruction
sequence object.

I'm trying to collect some statistics about instruction sequences for an entire system, but I mostly care about methods and blocks. This feature lets me select only methods and blocks to analyze.

I am using a script like this:

def walk iseq
  case iseq.type
  when :METHOD, :BLOCK
    count = 0
    sends = 0
    iseq.to_a.last.each do |insn|
      count += 1 if insn.is_a?(Array)

      case insn
      in [:opt_send_without_block, _]
        sends += 1
      in [:send, _]
        sends += 1
      in [:invokeblock]
        sends += 1
      else
      end
    end
    p [count, sends]
  end
  iseq.each_child { |n| walk(n) }
end

iseq = RubyVM::InstructionSequence.compile_file(ARGV[0])
walk iseq

Then in my shell I can do this:

$ find ~/git/rails/activerecord/lib -name '*.rb' -exec ./miniruby test.rb {} \;

I'm able to calculate instructions per method as well as number of "sends" per iseq. (Of course this isn't 100% accurate because of metaprogramming etc, but I think it lets us get good estimates)

I made a pull request here and I've attached a patch as well.


Files

0001-Add-InstructionSequence-type-method.patch (2.16 KB) 0001-Add-InstructionSequence-type-method.patch tenderlovemaking (Aaron Patterson), 06/16/2022 10:30 PM

No data to display

Actions

Also available in: Atom PDF