Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
for reasons lost to history, someone used "$self" (hey, it's unique, right?) as cheaper than generating a unique ID

Unfortunately it turns out that it isn't very cheap at all (speed wise). In fact it's just about the worse possible choice :-) On my perl 5.8.7 this basic benchmark:

#! /usr/bin/perl use strict; use warnings; { package Base; use Devel::Size (); sub make_n { my ($class, $n) = @_; my @objects; push @objects, $class->new for 1..$n; return \@objects; } sub hidden_stuff { return } sub size_of_n { my ($class, $n) = @_; my $objects = $class->make_n( $n ); my $stuff = [ $objects, $class->hidden_stuff ]; return( Devel::Size::total_size( $stuff ), $objects ); } sub do_something { my $class = shift; my $self = $class->new; $self->set_a( $self->get_a + 1 ) for ( 1 .. 100 ); die unless $self->get_a == 101; } } { package BlessedHash; use base qw( Base ); sub new { bless { a => 1, b => 2, c => 3, d => 4 }, shift; } sub get_a { $_[0]->{a} }; sub set_a { $_[0]->{a} = $_[1] }; } { package RefaddrCached; use Scalar::Util (); use base qw( Base ); my (%a, %b, %c, %d); sub new { my $self = bless \my $_tmp, shift; $$self = Scalar::Util::refaddr( $self ); $a{ $$self } = 1; $b{ $$self } = 2; $c{ $$self } = 3; $d{ $$self } = 4; return $self; } sub get_a { $a{ ${$_[0]} } }; sub set_a { $a{ ${$_[0]} } = $_[1] }; sub DESTROY { my $self = shift; delete $a{ $$self }; delete $b{ $$self }; delete $c{ $$self }; delete $d{ $$self }; } sub hidden_stuff { return [ \%a, \%b, \%c, \%d ] } } { package RefaddrCall; use Scalar::Util qw( refaddr ); use base qw( Base ); my (%a, %b, %c, %d); sub new { my $self = bless \my $_tmp, shift; $a{ refaddr $self } = 1; $b{ refaddr $self } = 2; $c{ refaddr $self } = 3; $d{ refaddr $self } = 4; return $self; } sub get_a { $a{ refaddr $_[0] } }; sub set_a { $a{ refaddr $_[0] } = $_[1] }; sub DESTROY { my $self = shift; delete $a{ refaddr $self }; delete $b{ refaddr $self }; delete $c{ refaddr $self }; delete $d{ refaddr $self }; } sub hidden_stuff { return [ \%a, \%b, \%c, \%d ] } } { package SelfAsIndex; use Scalar::Util qw( refaddr ); use base qw( Base ); my (%a, %b, %c, %d); sub new { my $self = bless \my $_tmp, shift; $a{ $self } = 1; $b{ $self } = 2; $c{ $self } = 3; $d{ $self } = 4; return $self; } sub get_a { $a{ $_[0] } }; sub set_a { $a{ $_[0] } = $_[1] }; sub DESTROY { my $self = shift; delete $a{ $self }; delete $b{ $self }; delete $c{ $self }; delete $d{ $self }; } sub hidden_stuff { return [ \%a, \%b, \%c, \%d ] } } { package NumSelfAsIndex; use Scalar::Util qw( refaddr ); use base qw( Base ); my (%a, %b, %c, %d); sub new { my $self = bless \my $_tmp, shift; $a{ 0+$self } = 1; $b{ 0+$self } = 2; $c{ 0+$self } = 3; $d{ 0+$self } = 4; return $self; } sub get_a { $a{ 0+$_[0] } }; sub set_a { $a{ 0+$_[0] } = $_[1] }; sub DESTROY { my $self = shift; delete $a{ 0+$self }; delete $b{ 0+$self }; delete $c{ 0+$self }; delete $d{ 0+$self }; } sub hidden_stuff { return [ \%a, \%b, \%c, \%d ] } } { package ClassStd; use base qw( Base ); use Class::Std; my %a : ATTR( :get<a> :set<a> ); my (%b, %c, %d) :ATTR; sub BUILD { my ($self, $id) = @_; $a{ $id } = 1; $b{ $id } = 2; $c{ $id } = 3; $d{ $id } = 4; } # just a guess - maybe more hidden behind the scenes sub hidden_stuff { return [ \%a, \%b, \%c, \%d ] } } use Devel::Symdump; my @classes = sort grep { eval { $_->isa( 'Base' ) } && $_ ne 'Base' } + Devel::Symdump->rnew->packages; my $n = 10000; my @objects; foreach my $class (@classes) { my ($size, $objects) = $class->size_of_n( $n ); # we keep the objects around just in case having heavily populated # attribute hashes affects the performance of the inside-out objec +ts push @objects, $objects; print "$class x $n\t= $size bytes\n"; }; print "\n"; use Benchmark qw /cmpthese/; cmpthese( -1, { map { my $class = $_; $class => sub { $class->do_something } } @classes});

Gives me:

BlessedHash x 10000 = 2265688 bytes ClassStd x 10000 = 2222948 bytes NumSelfAsIndex x 10000 = 2219534 bytes RefaddrCached x 10000 = 2300888 bytes RefaddrCall x 10000 = 2226816 bytes SelfAsIndex x 10000 = 2436816 bytes Rate SelfAsIndex ClassStd NumSelfAsIndex RefaddrCall +RefaddrCached BlessedHash SelfAsIndex 1000/s -- -9% -12% -44% + -57% -59% ClassStd 1100/s 10% -- -3% -38% + -53% -55% NumSelfAsIndex 1131/s 13% 3% -- -36% + -52% -54% RefaddrCall 1778/s 78% 62% 57% -- + -24% -27% RefaddrCached 2349/s 135% 114% 108% 32% + -- -4% BlessedHash 2443/s 144% 122% 116% 37% + 4% --

with a plain $self index coming in a lot worse than the faster alternatives.


In reply to Re^3: Threads and fork and CLONE, oh my! by adrianh
in thread Threads and fork and CLONE, oh my! by xdg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found