Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Using CORE:: with autobox

by bennymack (Pilgrim)
on Feb 19, 2007 at 18:24 UTC ( [id://600908]=perlquestion: print w/replies, xml ) Need Help??

bennymack has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm investigating the possibility of using autobox in my day-to-day coding. The first thing I did was look into the speed penalty imposed by the use of this module.

The preliminary results seem to indicate that a lot of the additional overhead is coming from the subroutine call when using autobox vs when using the CORE:: routines directly.

#!/usr/bin/perl use strict; use warnings; use autobox; sub SCALAR::uc { CORE::uc($_[0]); } sub autobox1 { return "Hello, World\n"->uc; } no autobox; sub primitive1 { return CORE::uc "Hello, World\n"; } package main; use Benchmark; cmpthese( -1, { 'autobox1' => sub { autobox1() }, 'primitive1' => sub { primitive1() }, }); cmpthese( -1, { 'autobox2' => sub { return SCALAR::uc( "Hello, World\n" ); }, 'primitive2' => sub { return CORE::uc( "Hello, World\n" ); }, }); __END__ Rate autobox1 primitive1 autobox1 183794/s -- -78% primitive1 835106/s 354% -- Rate autobox2 primitive2 autobox2 513912/s -- -89% primitive2 4797406/s 834% --

So, I'm wondering what the feasibility of just having autobox use CORE::uc directly to do its magic rather than wrapping it in a sub and incurring the corresponding overhead.

I've tried using differnt combinations of import options as well as a sub class and hacked up version of autobox.pm thus far but to no avail.

Thanks everyone!

Replies are listed 'Best First'.
Re: Using CORE:: with autobox
by ysth (Canon) on Feb 19, 2007 at 19:29 UTC
    CORE:: is really a parse-time construct; most builtins aren't actually subs, so there's no way to take a reference to them to alias them (ala *SCALAR::uc = \&CORE::uc).

    Update: just saw ferreira's response; no, those don't work. They will try to call an undefined sub CORE::uc, not the builtin.

Re: Using CORE:: with autobox
by stvn (Monsignor) on Feb 19, 2007 at 22:53 UTC
    The preliminary results seem to indicate that a lot of the additional overhead is coming from the subroutine call when using autobox vs when using the CORE:: routines directly.

    Actually, I would suspect that autobox's overhead is more likely coming from a combination of calling something as a method (which tends to be slower then subs, and certainly much slower than builtins) and the overhead of having to search the {SCALAR,ARRAY,HASH}:: namespace(s) for an applicable method.

    Certainly the additional call levels are playing a part, but in more real-world usage of autobox (which wasn't just simple uses of CORE:: subs) I would suspect it would be much less relevant.

    -stvn
      Certainly the additional call levels are playing a part, but in more real-world usage of autobox (which wasn't just simple uses of CORE:: subs) I would suspect it would be much less relevant.

      Thanks stvn. I whole-heartedly agree with you on this point. I would like to at least optimize the core operations if possible (read easy). I feel it's something that others might also be interested in as far as lowering the entry point/barrier to using autobox speed-wise so we can get on with the more interesting uses.

      I'm open to solutions that sub-class and/or hack the Perl portion of autobox and maybe even the XS part if chocolateboy is listening ;)

      Of course, if it can be incontrovertibly shown that there is no optimizations to be had, then thanks and nevermind :)

        I feel it's something that others might also be interested in as far as lowering the entry point/barrier to using autobox speed-wise so we can get on with the more interesting uses.

        Well, as far as the speed is concerned, you can't get rid of the method calling overhead, but it might be possible to improve the method lookup through some creative caching. However, Perl is such a dynamic language, that you would have to be careful about this so that people can still get away with dirty hacks like adding to the SCALAR, ARRAY, HASH, etc. symbol tables. There is an XS accessible variable that tells you when someone has messed with the symbol tables, but IIRC it is a global thing and not per-package, but that could still be very useful. You could also look into handling some of the builtins at the XS level, though I have no idea if that would help or not.

        Now as for the "more interesting uses" bit. While there are many cool things that you can do with autobox, it has some serious limits.

        You cannot subclass and extend the "built in" classes (such as ARRAY and HASH) and get ARRAY and HASH like behavior (as you will be able to do in Perl 6). This is because the operators associated with hash and array access are not overloadable, so they can't be assocaited with your ARRAY and HASH packages. You would basically be stuck with $array->[0] always working in the same way, but $array->push(1) could have a different extended behavior. And I tend to doubt that autobox would work well with tie, so that couldn't help.

        The next issue that I found with autobox is that all your hashes and arrays must be references (despite what the POD docs seem to indicate). While this is not bad if you are always doing method calls, it tends to make more idiomatic Perl kinda ugly since you must constantly de-reference and en-reference things in order to maintin the autobox-ness.

        And then the last problem I have with it is that the pragma is not lexically scoped. The last time I talked to chocolateboy, he mentioned that he wanted to make it lexical once the lexical pragma patch is solid in the core, but this is still fairly bleedding edge at this point I think. Not being lexical means that everyone who wants to take advantage of your autoboxing needs to have use autobox within their package/file scope, which can get quite invasive. Of course you could also argue that this is a good thing because it keeps the magic contained and makes it less likely that autobox will create unintended things to happen deep within someone else's code.

        I'm open to solutions that sub-class and/or hack the Perl portion of autobox and maybe even the XS part if chocolateboy is listening ;)

        I dont think chocolateboy hangs out here, but I have discussed autobox with him through email (you can get the address from his CPAN directory) a few times and we was very helpful and informative.

        -stvn
Re: Using CORE:: with autobox
by ferreira (Chaplain) on Feb 19, 2007 at 19:27 UTC

    I don't know the source code of autobox, but as it is a non-standard module, things like $s.uc will probably be slower than uc $s always. But if the additional overhead comes from subroutine call, you can try something like:

    BEGIN { *SCALAR::uc = \&CORE::uc }
    instead of
    sub SCALAR::uc { CORE::uc($_[0]); }
    or the optimized
    sub SCALAR::uc { goto &CORE::uc; }

    (The BEGIN is important here to make it behave as a compile-time function declaration.)

    If it works, you will have less one frame in the call stack, which may or may not speed things up. This suggestion is untested and may not work: There is some weirdness associated to the core functions that don't behave identically to the ordinary subs.

      You can't take references to CORE:: functions and goto() is slower than just leaving the call frame on the stack.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://600908]
Approved by BrowserUk
Front-paged by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found