Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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 fa
    • 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 (

      • 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.

        • 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) @ 333

    • 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 [cpan.org], if I understood it correctly.
      • > 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".

        • 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) {

  • 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)
    • (note, they may not be faster, but I'd like to see comparative speeds)
    • 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

      • 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.