Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#! /bin/ruby
def main argv
n = (argv[0] || 2).to_i rescue 2
m = (argv[1] || 20).to_i rescue 20
g = n_gram n
g.
enum_for(:each_pair).
lazy.
sort_by {|k, v| -v }.
first(m).
each do |k, v|
printf("%7d %s\n", v, k.join(' -> '))
end
end
def n_gram n
gram = Hash.new 0
each_iseq_of all_sources do |i|
each_sequence i.to_a do |a|
insns = a.select{|j| Array === j }.map{|j| j[0] }
insns.each_cons n do |c|
gram[c] += 1
end
end
end
return gram
end
def all_sources
return $LOADED_FEATURES.grep(/\.rb$/)
end
def each_iseq_of files
files.each do |f|
begin
i = RubyVM::InstructionSequence.compile_file f, false
yield i
rescue Errno::ENOENT => e
STDERR.printf "%s\n", e.message
end
end
end
def each_sequence a
return unless a
return unless a = a[13]
a.each do |i|
next unless Array === i
j = case i[0]
when :putiseq then 1
when :once then 1
when :defineclass then 2
when :send then 3
when :invokesuper then 3
else next
end
each_sequence i[j] do |b|
yield b
end
end
yield a
end
main ARGV
@shyouhei

This comment has been minimized.

Copy link

shyouhei commented Jul 13, 2016

Ruby's N-gram generator. Usage:

% RUBYOPT=-vW0 RAILS_ENV=production bin/rails runner gistfile1.rb 2 40
ruby 2.4.0dev (2016-07-13 trunk 55664) [x86_64-darwin15]
  30602 send -> pop
  22812 getlocal -> send
  16157 putnil -> getconstant
  15307 pop -> putspecialobject
  14522 putobject -> putiseq
  14522 putiseq -> send
  14071 putspecialobject -> putobject
  14031 send -> leave
  13948 send -> send
  12480 putobject -> send
  11734 putself -> send
  11568 branchunless -> jump
   8980 pop -> putself
   8601 send -> branchunless
   8098 getlocal -> getlocal
   7135 jump -> getlocal
   6932 putstring -> send
   6269 putself -> getlocal
   6078 send -> setlocal
   5694 putself -> putobject
   5482 getlocal -> putobject
   5213 getconstant -> getconstant
   5201 getconstant -> send
   4718 setlocal -> getlocal
   4672 putobject -> putobject
   4633 jump -> putself
   4478 putspecialobject -> putnil
   4339 putobject -> getlocal
   4287 pop -> getlocal
   4035 jump -> putnil
   3886 send -> getlocal
   3633 putnil -> defineclass
   3429 send -> dup
   3357 defineclass -> leave
   3264 getconstant -> getlocal
   3156 send -> jump
   3130 tostring -> putobject
   2990 send -> putobject
   2972 putself -> putstring
   2909 pop -> jump
@shyouhei

This comment has been minimized.

Copy link

shyouhei commented Jul 14, 2016

N-gram against redmine, on 2.3 because ActiveSupport does not work on trunk yet.

% RUBYOPT=-vW0 RAILS_ENV=production bin/rails runner gistfile1.rb
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
No such file or directory @ rb_sysopen - thread.rb
  41060 send -> pop
  34709 getlocal -> send
  25012 send -> send
  23233 putobject -> putstring
  21515 putstring -> putobject
  20703 putnil -> getconstant
  20186 putobject -> send
  19741 branchunless -> jump
  18607 pop -> putspecialobject
  17734 putiseq -> send
  17734 putobject -> putiseq
  17223 send -> leave
  17004 putspecialobject -> putobject
  16622 putself -> send
  15613 send -> branchunless
  13243 putstring -> send
  13001 jump -> getlocal
  12650 getlocal -> getlocal
  11435 pop -> putself
  10426 send -> setlocal
@shyouhei

This comment has been minimized.

Copy link

shyouhei commented Jul 14, 2016

3-gram and 4-gram, also against redmine

% RUBYOPT=-vW0 RAILS_ENV=production bin/rails runner gistfile1.rb 3
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
No such file or directory @ rb_sysopen - thread.rb
  20688 putstring -> putobject -> putstring
  20449 putobject -> putstring -> putobject
  17734 putobject -> putiseq -> send
  17454 send -> pop -> putspecialobject
  16162 putspecialobject -> putobject -> putiseq
  15603 send -> branchunless -> jump
  15428 putiseq -> send -> pop
  14732 pop -> putspecialobject -> putobject
   9404 send -> pop -> putself
   7582 getlocal -> getlocal -> send
   7345 getlocal -> send -> send
   7167 branchunless -> jump -> getlocal
   6996 getlocal -> putobject -> send
   5532 putnil -> getconstant -> send
   5432 send -> setlocal -> getlocal
   5372 putobject -> send -> pop
   4994 send -> pop -> getlocal
   4976 putself -> send -> send
   4965 getlocal -> send -> leave
   4810 putself -> getlocal -> send
% RUBYOPT=-vW0 RAILS_ENV=production bin/rails runner gistfile1.rb 4
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
No such file or directory @ rb_sysopen - thread.rb
  20289 putobject -> putstring -> putobject -> putstring
  19993 putstring -> putobject -> putstring -> putobject
  16162 putspecialobject -> putobject -> putiseq -> send
  15428 putobject -> putiseq -> send -> pop
  14693 pop -> putspecialobject -> putobject -> putiseq
  14573 send -> pop -> putspecialobject -> putobject
  13355 putiseq -> send -> pop -> putspecialobject
   5515 send -> branchunless -> jump -> getlocal
   3871 getlocal -> send -> branchunless -> jump
   3710 putobject -> send -> branchunless -> jump
   3532 send -> pop -> putself -> putobject
   3305 send -> branchunless -> jump -> putself
   2980 putspecialobject -> putnil -> defineclass -> leave
   2942 send -> send -> branchunless -> jump
   2775 putself -> putstring -> send -> pop
   2485 putself -> putobject -> send -> pop
   2307 putstring -> putstring -> putstring -> putstring
   2287 putobject -> putiseq -> send -> leave
   2264 putstring -> send -> pop -> putself
   2134 putobject -> send -> pop -> putself
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.