in reply to Using CORE:: with autobox

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

Replies are listed 'Best First'.
Re^2: Using CORE:: with autobox
by bennymack (Pilgrim) on Feb 19, 2007 at 23:43 UTC
    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
        Hi, guys.
        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).
        Yes, the receiver must be a scalar i.e. (by default) a string, number or reference. If there's somewhere in the docs that makes this unclear or ambiguous, please let me know and I'll fix it. Note that there is a module by Scott Walters, Perl6::Contexts, that allows methods to be called on plain (i.e. non-reference) hashes and arrays.
        And then the last problem I have with it is that the pragma is not lexically scoped.
        I apologise if I gave that impression. It's always been lexically scoped and works independently of the lexical pragma. There are a bunch of tests that verify its "lexical correctness" in the test suite.

        scope1.pl:

        01: $| = 1; 02: 03: sub SCALAR::uc { uc $_[0] } 04: 05: { 06: use autobox; 07: print "hello, world!"->uc; 08: } 09: 10: print "hello, world!"->uc;

        > perl -l scope1.pl

        HELLO, WORLD!

        Can't locate object method "uc" via package "hello, world!" (perhaps you forgot to load "hello, world!"?) at scope1.pl line 10.

        scope2.pl:
        01: $| = 1; 02: 03: sub SCALAR::uc { uc $_[0] } 04: sub test { $_[0]->uc } 05: 06: { 07: use autobox; 08: print "hello, world!"->uc; 09: test("hello, world!"); 10: }

        > perl -l scope2.pl

        HELLO, WORLD!

        Can't locate object method "uc" via package "hello, world!" (perhaps you forgot to load "hello, world!"?) at scope2.pl line 4.

        scope3.pl:
        01: $| = 1; 02: 03: sub SCALAR::uc { uc $_[0] } 04: 05: { 06: use autobox; 07: print "hello, world!"->uc; 08: 09: { 10: no autobox; 11: print "hello, world!"->uc; 12: } 13: }

        > perl -l scope3.pl

        HELLO, WORLD!

        Can't locate object method "uc" via package "hello, world!" (perhaps you forgot to load "hello, world!"?) at scope3.pl line 11.

        At any rate, don't trust my poorly and hastily written emails; trust my poorly and hastily written docs and tests :-)

        chocolateboy