From: "David A. Black" Date: 2008-04-23T05:44:45+09:00 Subject: Re: Performance on method dispatch for methods defined via define_method Hi -- On Wed, 23 Apr 2008, Robert Dober wrote: > On Tue, Apr 22, 2008 at 8:46 PM, Paul Brannan wrote: >> On Wed, Apr 23, 2008 at 12:39:29AM +0900, Robert Dober wrote: >> > to my dispair (because it makes traits slow) method lookup is about >> > 50% slower for methods defined via define_method vs. def. >> > If this is completely stupid a question just say so and sorry for >> > having wasted your time, but here I go. >> >> Why do you believe it is method lookup that is slower? Your benchmark >> tests both lookup and dispatch (I say this because I suspect it is the >> dispatch that is slow). > Completely correct forgive my sloppy language here I was preciser below. >> >> >> > Would there be any chance to get the same speed for method >> > lookup/invocation for methods defined via define_method? >> > Right now we have this: >> >> Method dispatch to a method defined with define_method is not much less >> than the cost of calling a proc, which is essentially what's happening >> under the hood: >> > Well spotted, I was thinking about this for some time and of course > only *after* posting this I guess I had an idea of what is going on, > as you said it is the proc that is called for methods defined with > define_method and this is slower. > However, if and only if, the block in the define_method does not use > the closure we could really inline the block as the code of a method > defined with def. > Can this be determined at compile time? I have a strong feeling that > yes, well I guess that might be an ultimate optimization item, > hopefully. > > E.g. > module M > x = 42 > define_method :a do x end # Cannot be inlined > define_method :b do some_call; @x end # Can be inlined as if def were used > end > > Did I miss something else? class C x = 100 define_method(:a) { x } define_method(:b){ puts eval(gets.chomp)} end David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates!