eval { $thing->isa($class) }

Ovid on 2008-01-10T16:00:20

I've seen and happily used the eval { $thing->($class) } idiom, but I need raw performance for one application as that function can be called hundreds or thousands of times.

use strict;
use warnings;

use Benchmark 'timethese';
use Scalar::Util 'blessed';

my $scalar = 3;
my $cgi    = bless {} => 'CGI';
my $cgi2   = bless {} => 'CGI::Subclass';
my $other  = bless {} => 'Other';
@CGI::Subclass::ISA = 'CGI';

timethese(
    100000,
    {
        blessed => sub {
            foreach my $var ($scalar, $cgi, $cgi2, $other) {
                if (blessed($var) && $var->isa('CGI')) {}
            }
        },
        eval => sub {
            foreach my $var ($scalar, $cgi, $cgi2,$other) {
                if (eval { $var->isa('CGI') }) {}
            }
        },
    }
);

Results:

Benchmark: timing 100000 iterations of blessed, eval...
   blessed:  0 wallclock secs ( 0.70 usr +  0.00 sys =  0.70 CPU) @ 142857.14/s (n=100000)
      eval:  0 wallclock secs ( 1.21 usr +  0.07 sys =  1.28 CPU) @ 78125.00/s (n=100000)

Looks like I need to type it out longhand.


Find the invariant

jplindstrom on 2008-01-10T17:32:26

I assume it's to avoid calling the method on undef values.

So try to find (or establish) the boundary where $var is always known to not be undef. Test it once there, and not in an inner loop. If possible, it may very well not be.

If that's not feasible, how about introducing null objects instead of undef values?

Re: squeezing out performance. Do you need the overhead of calling blessed? If $var contains a string instead of an object, $var->isa is going to work as a class method anyway, right? (and return false) So it's enough to check for trueness of $var before calling isa(). One less sub call.

Re:Find the invariant

Ovid on 2008-01-10T20:18:10

At the present time, the possible values of $var are strings, arrayrefs, hashrefs and regular expressions. That's why the blessed call is in there. However, there is one naughty trick I can do. The class I'm actually testing for creates logic variables. Since I control that class, if I declare that class as final and since I don't override isa, I can use UNIVERSAL::isa() as a function call:

Benchmark: timing 200000 iterations of blessed, eval, universal...
   blessed:  1 wallclock secs ( 0.85 usr +  0.00 sys =  0.85 CPU) @ 235294.12/s (n=200000)
      eval:  2 wallclock secs ( 1.72 usr +  0.00 sys =  1.72 CPU) @ 116279.07/s (n=200000)
universal:  1 wallclock secs ( 0.64 usr +  0.01 sys =  0.65 CPU) @ 307692.31/s (n=200000)

That's a huge performance gain and in this limited case, it's safe.

Re:Find the invariant

chromatic on 2008-01-10T20:32:12

If you were really evil, you could avoid the sub call, stringify the possibly-a-ref, and check the first n characters against the name of your class.

Re:Find the invariant

Ovid on 2008-01-10T20:47:12

Benchmark: timing 200000 iterations of blessed, eval, stringify, universal, unpack...
   blessed:  1 wallclock secs ( 0.85 usr +  0.00 sys =  0.85 CPU) @ 235294.12/s (n=200000)
      eval:  2 wallclock secs ( 1.71 usr +  0.00 sys =  1.71 CPU) @ 116959.06/s (n=200000)
stringify:  1 wallclock secs ( 1.08 usr +  0.01 sys =  1.09 CPU) @ 183486.24/s (n=200000)
universal:  1 wallclock secs ( 0.60 usr +  0.00 sys =  0.60 CPU) @ 333333.33/s (n=200000)
    unpack:  2 wallclock secs ( 1.35 usr +  0.00 sys =  1.35 CPU) @ 148148.15/s (n=200000)

I also expect nastiness with Pointer could be thrown into the mix.

Re:Find the invariant

bart on 2008-01-10T20:33:39

I assume it's to avoid calling the method on undef values.
Not quite. It's to avoid calling a method on an unblessed reference. People used to use

UNIVERSAL::isa($value, $class)
for this reason, but lately this has fallen into disgrace, mainly for proper supporting mock objects, if I understood it correctly.

Re:Find the invariant

Alias on 2008-01-11T00:12:09

> mainly for proper supporting mock objects [cpan.org], if I understood it correctly.

Also anything else that needs custom isa values, asa.pm, Class::Adapter, Class::Decorator, and others.

The only legitimate use for the UNIVERSAL functions directly that I'm aware of is the UNIVERSAL::can($foo, 'can') which is a highly back-compatible method for testing "is $foo an object".

Re:Find the invariant

bart on 2008-01-11T11:06:53

The only legitimate use for the UNIVERSAL functions directly that I'm aware of is the UNIVERSAL::can($foo, 'can') which is a highly back-compatible method for testing "is $foo an object".
So, maybe we could use this then:

UNIVERSAL::can($thing, 'isa') && $thing->isa($class)
or, as you seem to prefer:

UNIVERSAL::can($thing, 'can') && $thing->isa($class)
:)

No joke, I extended the original benchmarks with

isa => sub {
    foreach my $var ($scalar, $cgi, $cgi2,$other) {
        if (UNIVERSAL::can($var, 'isa') && $var->isa('CGI')) {}
    }
},
can => sub {
    foreach my $var ($scalar, $cgi, $cgi2,$other) {
        if (UNIVERSAL::can($var, 'can') && $var->isa('CGI')) {}
    }
},
and got these results:

Benchmark: timing 100000 iterations of blessed, can, eval, isa...
   blessed:  1 wallclock secs ( 0.67 usr +  0.00 sys =  0.67 CPU) @ 148809.52/s (n=100000)
       can:  1 wallclock secs ( 0.58 usr +  0.00 sys =  0.58 CPU) @ 173010.38/s (n=100000)
      eval:  1 wallclock secs ( 1.63 usr +  0.00 sys =  1.63 CPU) @ 61538.46/s (n=100000)
       isa: -1 wallclock secs ( 0.64 usr +  0.00 sys =  0.64 CPU) @ 156250.00/s (n=100000)

Well... My code definitely is the fastest. I am surprised at the speed difference between 'can' and 'isa'. 'can' is the winner!

Params::Util and UNIVERSAL::can

Alias on 2008-01-11T00:14:35

Some more variations for you to include in the mix...

Params::Util::_INSTANCE has long done Scalar::Util::blessed($foo) and $foo->isa('bar'), I'd be interested to see how much overhead the extra function call adds.

Also, try substituting UNIVERSAL::can($foo, 'can') for blessed($foo)

Re:Params::Util and UNIVERSAL::can

Alias on 2008-01-11T00:14:56

(note, they may not be faster, but I'd like to see comparative speeds)

Re:Params::Util and UNIVERSAL::can

Ovid on 2008-01-11T07:48:55

So you want a fully qualified call to blessed instead of my importing it? Or did you miss that I had called blessed? UNIVERSAL::can('isa') is interestesting, but I'm sure that won't be much faster. I've added both of those (the fully qualified Scalar::Util::blessed() is called 'fqblessed', of course).

Benchmark: timing 200000 iterations of blessed, can, eval, fqblessed, stringify, universal, unpack...
   blessed:  1 wallclock secs ( 0.86 usr +  0.00 sys =  0.86 CPU) @ 232558.14/s (n=200000)
       can:  1 wallclock secs ( 0.86 usr +  0.00 sys =  0.86 CPU) @ 232558.14/s (n=200000)
      eval:  2 wallclock secs ( 1.72 usr +  0.01 sys =  1.73 CPU) @ 115606.94/s (n=200000)
fqblessed:  1 wallclock secs ( 0.86 usr +  0.00 sys =  0.86 CPU) @ 232558.14/s (n=200000)
stringify:  1 wallclock secs ( 1.09 usr +  0.00 sys =  1.09 CPU) @ 183486.24/s (n=200000)
universal:  1 wallclock secs ( 0.60 usr +  0.00 sys =  0.60 CPU) @ 333333.33/s (n=200000)
    unpack:  1 wallclock secs ( 1.35 usr +  0.00 sys =  1.35 CPU) @ 148148.15/s (n=200000)

So for my special case use, I can use UNIVERSAL::can and simply declare the class final (but it's an internal class, so that's OK). Otherwise, blessed plus isa is the fastest and most correct out of the alternatives I've tried.

Re:Params::Util and UNIVERSAL::can

Alias on 2008-01-12T11:11:26

No, I meant you should include the overhead of doing it the fastest way (which Params::Util does) but with an extra function call of calling Params::Util::_INSTANCE itself.