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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl object memory overhead
by tobyink (Canon) on Mar 31, 2014 at 11:44 UTC | |
by BrowserUk (Patriarch) on Mar 31, 2014 at 13:44 UTC | |
by tobyink (Canon) on Mar 31, 2014 at 16:54 UTC |