Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Think about Loose Coupling
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2014-04-19 03:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (476 votes), past polls