Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


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.

In reply to Re: Perl object memory overhead by BrowserUk
in thread Perl object memory overhead by Ineffectual

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [LanX]: wait... I /ignored NodeRapist

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (10)
    As of 2018-02-23 16:53 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (303 votes). Check out past polls.