<commit_before>#    This file is part of Metasm, the Ruby assembly manipulation suite
#    Copyright (C) 2006-2009 <NAME>
#
#    Licence is LGPL, see LICENCE in the top-level directory

# A script to help finding performance bottlenecks:
# ruby-prof myscript.rb
#  => String#+ gets called 50k times and takes 30s
# ruby -r log_caller -e 'log_caller(String, :+)' myscript.rb
#  => String#+ called 40k times from:
#      stuff.rb:42 in Myclass#uglymethod from
#      stuff.rb:32 in Myclass#initialize
# now you know what to rewrite


def log_caller(cls, meth, histlen=-1)
	eval <<EOS
class #{cls}
 alias #{meth}_log_caller #{meth}
 def #{meth}(*a, &b)
  $log_caller_#{meth}[caller[0..#{histlen}] += 1
  #{meth}_log_caller(*a, &b)
 end
end

$log_caller_#{meth} = Hash.new(0)
at_exit { puts " callers of #{cls} #{meth}:", $log_caller_#{meth}.sort_by { |k, v| -v }[0, 4].map { |k, v| ["\#{v} times from", k, ''] } }
EOS
end
<commit_msg>fix misc/bottleneck<commit_after>#    This file is part of Metasm, the Ruby assembly manipulation suite
#    Copyright (C) 2006-2009 <NAME>
#
#    Licence is LGPL, see LICENCE in the top-level directory

# A script to help finding performance bottlenecks:
# ruby-prof myscript.rb
#  => String#+ gets called 50k times and takes 30s
# LOGCALLER='String#+' ruby -r log_caller myscript.rb
#  => String#+ called 40k times from:
#      stuff.rb:42 in Myclass#uglymethod from
#      stuff.rb:32 in Myclass#initialize
# now you know what to rewrite


def log_caller(cls, meth, histlen=-1)
	malias = meth.to_s.gsub(/[^a-z0-9_]/i, '') + '_log_caller'
	mcntr = '$' + meth.to_s.gsub(/[^a-z0-9_]/i, '') + '_counter'
	eval <<EOS
class #{cls}
 alias #{malias} #{meth}
 def #{meth}(*a, &b)
  #{mcntr}[caller[0..#{histlen}]] += 1
  #{malias}(*a, &b)
 end
end

#{mcntr} = Hash.new(0)
at_exit { puts " callers of #{cls} #{meth}:", #{mcntr}.sort_by { |k, v| -v }[0, 4].map { |k, v| ["\#{v} times from", k, ''] } }
EOS
end

if ENV['LOGCALLER'] =~ /^(.*)#(.*)$/
	cls, meth = $1, $2.to_sym
	cls = cls.split('::').inject(Object) { |o, cst| o.const_get(cst) }
	log_caller(cls, meth)
end
{'fraction_non_alphanumeric': 0.18307839388145317, 'fraction_numerical': 0.02581261950286807, 'mean_word_length': 3.9714964370546317, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7013882', 'n_tokens_mistral': 892, 'n_tokens_neox': 845, 'n_words': 236}