Created
January 21, 2022 16:36
-
-
Save kddnewton/4dcb5e8653ba61d4383947be3442a1f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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