2006/5/23, Ola Bini <Ola.Bini / ki.se>:
> Hi!
>
> Thank you for the help.
> I had forgot about the builtin benchmark.rb, actually. But what I was
> looking for was more of the kind of information where I can read about
> this, instead of trying different ways to get what I want. The big problem
> is that most of the code doing stuff (like the Class#===) is distributed in
> many places, and if I'm going to replace it, it would be good to find out
> beforehand if it's worth it, especially when just replacing one point won't
> show that much difference in the performance.
> Actually, replacing the Class#=== with predicates on classes gave me about
> 5% in performance, in this limited domain.
>
> The class comparisons are right now done, because it's an LL(1) parser, and
> the lookahead checks the type of the next token to determine which action
> to take.

I think Eric is right in that you should rethink your design.  Usually
in OO languages you use method overloading to get proper behavior.  A
fast alternative in your case might be to use a hash with blocks.

action[ next_token.class ].call( whatever, arguments )

Note though that this has slightly different semantics as === will
check for subclasses also while the hash approach will only tackle
individual classes - unless you add some magic to the hash like this:

>> action = Hash.new {|h,cl| cl ? h[cl.ancestors[1]] : lambda {"fallback"} }
=> {}
>> action[Enumerable] = lambda { "foo" }
=> #<Proc:0x100d0948@(irb):68>
>> action[String].call
=> "foo"
>> action[Fixnum].call
=> "fallback"

Cheers

robert