ruby-changes:26524
From: nobu <ko1@a...>
Date: Sun, 23 Dec 2012 17:57:38 +0900 (JST)
Subject: [ruby-changes:26524] nobu:r38575 (trunk): profiler.rb: Wrapper for BasicObject
nobu 2012-12-23 17:57:27 +0900 (Sun, 23 Dec 2012) New Revision: 38575 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38575 Log: profiler.rb: Wrapper for BasicObject * lib/profiler.rb (Profiler__::Wrapper): support calling singleton methods of an instance of BasicObject. Modified files: trunk/ChangeLog trunk/lib/profiler.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 38574) +++ ChangeLog (revision 38575) @@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Sun Dec 23 17:57:19 2012 Nobuyoshi Nakada <nobu@r...> +Sun Dec 23 17:57:25 2012 Nobuyoshi Nakada <nobu@r...> + + * lib/profiler.rb (Profiler__::Wrapper): support calling singleton + methods of an instance of BasicObject. * lib/profiler.rb (Profiler__::PROFILE_PROC): use TracePoint. Index: lib/profiler.rb =================================================================== --- lib/profiler.rb (revision 38574) +++ lib/profiler.rb (revision 38575) @@ -58,8 +58,22 @@ https://github.com/ruby/ruby/blob/trunk/lib/profiler.rb#L58 # 0.00 0.19 0.00 1 0.00 190.00 #toplevel module Profiler__ + class Wrapper < Struct.new(:defined_class, :method_id, :hash) # :nodoc: + private :defined_class=, :method_id=, :hash= + + def initialize(klass, mid) + super(klass, mid, nil) + self.hash = Struct.instance_method(:hash).bind(self).call + end + + def to_s + "#{defined_class.inspect}#".sub(/\A\#<Class:(.*)>#\z/, '\1.') << method_id.to_s + end + alias inspect to_s + end + # internal values - @@start = @@stack = @@map = @@array = nil + @@start = @@stack = @@map = nil PROFILE_PROC = TracePoint.new(:call, :c_call, :return, :c_return) {|tp| case tp.event when :call, :c_call @@ -67,13 +81,9 @@ module Profiler__ https://github.com/ruby/ruby/blob/trunk/lib/profiler.rb#L81 @@stack.push [now, 0.0] when :return, :c_return now = Process.times[0] - key = [tp.defined_class, tp.method_id] + key = Wrapper.new(tp.defined_class, tp.method_id) if tick = @@stack.pop - data = begin - @@map[key] ||= [0, 0.0, 0.0, key] - rescue NoMethodError - @@array.find{|i| i[3] == key} || (@@array << [0, 0.0, 0.0, key])[-1] - end + data = (@@map[key] ||= [0, 0.0, 0.0, key]) data[0] += 1 cost = now - tick[0] data[1] += cost @@ -87,7 +97,6 @@ module_function https://github.com/ruby/ruby/blob/trunk/lib/profiler.rb#L97 @@start = Process.times[0] @@stack = [] @@map = {} - @@array = [] PROFILE_PROC.enable end def stop_profile @@ -97,7 +106,7 @@ module_function https://github.com/ruby/ruby/blob/trunk/lib/profiler.rb#L106 stop_profile total = Process.times[0] - @@start if total == 0 then total = 0.01 end - data = @@map.values + @@array + data = @@map.values data = data.sort_by{|x| -x[2]} sum = 0 f.printf " %% cumulative self self total\n" @@ -105,22 +114,9 @@ module_function https://github.com/ruby/ruby/blob/trunk/lib/profiler.rb#L114 for d in data sum += d[2] f.printf "%6.2f %8.2f %8.2f %8d ", d[2]/total*100, sum, d[2], d[0] - f.printf "%8.2f %8.2f %s\n", d[2]*1000/d[0], d[1]*1000/d[0], get_name(*d[3]) + f.printf "%8.2f %8.2f %s\n", d[2]*1000/d[0], d[1]*1000/d[0], d[3] end f.printf "%6.2f %8.2f %8.2f %8d ", 0.0, total, 0.0, 1 # ??? f.printf "%8.2f %8.2f %s\n", 0.0, total*1000, "#toplevel" # ??? end - def get_name(klass, id) - name = klass.to_s || "" - if klass.kind_of? Class - name += "#" - else - name += "." - end - name + id.id2name - rescue NoMethodError => e - name = e.message.slice(/#<.*?:0x[0-9a-f]+>/) || "" - name + "." + id.id2name - end - private :get_name end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/