Skip to content

Instantly share code, notes, and snippets.

@kddnewton
Created January 21, 2022 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kddnewton/4dcb5e8653ba61d4383947be3442a1f3 to your computer and use it in GitHub Desktop.
Save kddnewton/4dcb5e8653ba61d4383947be3442a1f3 to your computer and use it in GitHub Desktop.
def count_constant_refs(iseq)
cached = false
iseq.last.each_with_object([]) do |insn, counts|
case insn
in [:opt_getinlinecache, *]
cached = true
counts << 0
in [:getconstant, *]
counts[counts.length - 1] += 1 if cached
in [:opt_setinlinecache, *]
cached = false
in [*, ["YARVInstructionSequence/SimpleDataFormat", *] => child, *]
counts.concat(count_constant_refs(child))
else
end
end
end
[].tap do |counts|
Dir["**/*.rb"].each do |filepath|
iseq = RubyVM::InstructionSequence.compile_file(filepath).to_a
counts << count_constant_refs(iseq)
rescue SyntaxError
end
n, d = counts.sum(&:length), counts.length
puts "Average const refs per file: #{n}/#{d} = #{(n.to_f / d).round(2)}"
n, d = counts.sum(&:sum), n
puts "Average const ref depth: #{n}/#{d} = #{(n.to_f / d).round(2)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment