Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: A Scalar::Util::refaddr Oddity

by adrianh (Chancellor)
on Sep 26, 2005 at 16:04 UTC ( [id://495154]=note: print w/replies, xml ) Need Help??


in reply to Re^4: A Scalar::Util::refaddr Oddity
in thread A Scalar::Util::refaddr Oddity

That would work - but pulling in overload seems overkill when we can just use Scalar::Util::blessed to see whether the reblessing hack is necessary.

Replies are listed 'Best First'.
Re^6: A Scalar::Util::refaddr Oddity
by xdg (Monsignor) on Sep 26, 2005 at 17:26 UTC

    You're the benchmark master. ;-) One-time hit to require overload and then a single function call, or always check blessed, store the original package and bless twice?

    Or how about this -- so we only use it if something else has loaded overload and the object is actually overloaded?

    sub refaddr_pp { return if not ref $_[0]; if ( exists $INC{'overload.pm'} && overload::Overloaded($_[0]) ) { overload::StrVal($_[0]) =~ /0x(\w+)/; return hex $1; } else { return 0 + $_[0]; } }

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      You're the benchmark master. ;-) One-time hit to require overload and then a single function call, or always check blessed, store the original package and bless twice?

      No idea :-) I was thinking of memory footprint rather than speed myself... but you're right. Capital mistake to theorise ahead of data and all that.

        I'm trying to remember to benchmark more myself. Some quick benchmarking I did suggests they're all within the margin of error. Rest deleted

        Update: Error is in front of keyboard. There's a big difference if I actually write my benchmark code correctly.

        (Each loop did a refaddr on one object, one hash ref, and one ref to a constant.)

        # overload used on object Rate bless_regex strval_0+ bless_0+ bless_regex 12921/s -- -34% -37% strval_0+ 19557/s 51% -- -5% bless_0+ 20495/s 59% 5% -- # no overloading ( and overload not loaded ) Rate bless_regex bless_0+ strval_0+ bless_regex 13504/s -- -35% -93% bless_0+ 20766/s 54% -- -90% strval_0+ 206767/s 1431% 896% -- # bless_regex: fixed PP version from Scalar::Util to store package usi +ng PP blessed, and only rebless if reference is blessed # bless_0+: same as above, but use 0+$ref instead of regex extraction # strval_0+: if overload loaded and reference is overloaded, use StrVa +l to extract with regex, otherwise, use 0+$ref

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://495154]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found