http://www.perlmonks.org?node_id=1080032


in reply to Perl object memory overhead

If you have 3 million hashes, then you need to be able to refer to them, so, you've probably got them, or rather, references to them, stored in an Array (an @AoH):

@AoH = map{ a=>1, b=>2, c=>3 }, 1 .. 3e6;; say total_size( \@AoH );; 936000209

To turn a hashref into an object, you need to bless it. The effect on memory is:

bless $_, 'OO' for @AoH;; say total_size( \@AoH );; 936000209

Zilch!

However, then you need to manipulate the contents of those 'objects'.

Manipulate those 3e6 objects via the inconvenience of object notation and accessors:

sub OO::a{ $_[0]->{a}=$_[1] if defined $_[1]; $_[0]->{a} };; $t = time; $_->a( $_->a() +1 ) for @AoH; say time - $t;;; 4.60931897163391

Now perform that same manipulation on those very same "objects" the easy way:

$t = time; ++$_->{a} for @AoH; say time() - $t;; 0.931627988815308

Of course, that wasn't 'proper OO' above because my methods manipulated the object properties directly, so let's fix that:

sub OO::get_a{ $_[0]->{a} }; sub OO::set_a{ $_[0]->{a} = $_[1] };; sub OO::get_b{ $_[0]->{b} }; sub OO::set_b{ $_[0]->{b} = $_[1] };; sub OO::get_c{ $_[0]->{c} }; sub OO::set_c{ $_[0]->{c} = $_[1] };; sub OO::adjust_a{ $_[0]->set_a( $_[0]->get_a() + 1 ) };; $t = time; $_->adjust_a() for @AoH; say time() - $t;; 4.95501685142517

But you're still not 'doing it properly' because you didn't name your arguments:

sub OO::get_a{ my( $o ) = @_; $o->{a} }; sub OO::set_a{ my( $o, $v ) = + @_; $o->{a} = $v };; sub OO::get_b{ my( $o ) = @_; $o->{b} }; sub OO::set_b{ my( $o, $v ) = + @_; $o->{b} = $v };; sub OO::get_c{ my( $o ) = @_; $o->{c} }; sub OO::set_c{ my( $o, $v ) = + @_; $o->{c} = $v };; sub OO::adjust_a{ my( $o ) = @_; $o->set_a( $o->get_a() + 1 ) };; $t = time; $_->adjust_a() for @AoH; say time() - $t;; 6.20793199539185

Still wrong! I didn't check my arguments:

sub OO::get_a{ my( $o ) = @_; die unless ref( $o ) eq 'OO'; $o->{a} }; + sub OO::set_a{ my( $o, $v ) = @_; die unless ref( $o ) eq 'OO'; die un +less looks_like_number( $v ); $o->{a} = $v };; sub OO::get_b{ my( $o ) = @_; die unless ref( $o ) eq 'OO'; $o->{b} }; + sub OO::set_b{ my( $o, $v ) = @_; die unless ref( $o ) eq 'OO'; die un +less looks_like_number( $v ); $o->{b} = $v };; sub OO::get_c{ my( $o ) = @_; die unless ref( $o ) eq 'OO'; $o->{c} }; + sub OO::set_c{ my( $o, $v ) = @_; die unless ref( $o ) eq 'OO'; die un +less looks_like_number( $v ); $o->{c} = $v };; $t = time; $_->adjust_a() for @AoH; say time() - $t;; 8.23622608184814

Of course, writing all those setters and getters manually is just so 'Legacy Perl'; there are packages that'll do that for me:

use Moose; ...

Best we don't look. The price of modern convenience!


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Perl object memory overhead
by tobyink (Canon) on Mar 31, 2014 at 11:44 UTC

    Indeed; writing all those getters and setters manually is inconvenient.

    Rate OO MOOSE OO 3.47/s -- -47% MOOSE 6.56/s 89% --
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

      Ooh! Do I spy a benchmark special case like the ones that compiler makers used to add to get good scores? :)

      isa => 'Int', traits => ['Counter'], handles => { adjust_a => 'inc' },

      What if you changed the increment to +33?

      Also, it would be really useful to see the timing of adjust_a() run on the 3e6 objects, along with memory consumption?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        There's this:

        handles => { adjust_a => [ 'inc', 33 ] },

        It's not as fast because the arrayref in handles results in a curried coderef, but not unacceptably slow either.

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name