http://qs321.pair.com?node_id=11128133


in reply to Memory overhead of blessed hashes

I tried the four variants on my Linux box (uncomment the one you want):
#!/usr/bin/perl use strict; use warnings; my @objs; for my $i (1 .. 1_000_000) { # push @objs, { id => $i, name => "Object of class", number => int + rand 10 }; # push @objs, bless { id => $i, name => "Object of class", number +=> int rand 10 }, 'My::Class'; # push @objs, [$i, 'Object of class', int rand 10]; push @objs, bless [$i, 'Object of class', int rand 10], 'My::Class +'; } print `ps -v | grep ^$$`;

It seems bless doesn't really change anything, but a hash versus array does.

23235 pts/0 S+ 0:00 0 2020 371111 359628 1.1 /usr/bin/pe +rl ./1.pl 23225 pts/0 S+ 0:00 0 2020 261035 249668 0.7 /usr/bin/pe +rl ./1.pl

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Memory overhead of blessed hashes
by LanX (Saint) on Feb 09, 2021 at 16:21 UTC
    Well that's what I was expecting (tho I hoped for factor 10 gain for arrays).

    I was asking because I couldn't be sure if the runtime is not secretly doing any speed-for-memory bargain to improve method calls...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      I know that method lookups are cached, but I am quite certain (from the last time I read the documentation) that that cache is associated with the package STASH, rather than each object, since it logically applies to the class (package) and is shared between all objects of that class.

      I would have to dig into the code to be sure, but the memory overhead for bless appears to be very minimal — once an SV has been upgraded high enough to carry any kind of magic, it also has a STASH pointer, and AV and HV structures are complex enough that they always have those fields.

      Arrays in Perl do have considerably smaller overhead than hashes, although not a factor of 10. If you have a large number of some class of object, enough to cause memory usage problems, changing the internal representation to arrays and using constant to name the fields is likely to help. If encapsulation has been respected, this will require no changes outside of the object's implementation class.

        > I know that method lookups are cached,

        I remembered hearing people talking about this. That was the only possible theory left.

        > but I am quite certain (from the last time I read the documentation) that that cache is associated with the package STASH, rather than each object, since it logically applies to the class (package) and is shared between all objects of that class.

        Cool, that's what I needed to know! :)

        > Arrays in Perl do have considerably smaller overhead than hashes, although not a factor of 10.

        I did some tests for different sizes, yes it's not that dramatic - a 50% win is a very optimistic scenario.°

        > changing the internal representation to arrays and using constant to name the fields is likely to help.

        Well it's not my project anyway and some of my colleagues have weird strategies when it comes to optimization.

        Personally I'm thinking rather in O() categories, and 50% best case wouldn't convince me to refactor.

        I'd rather give Toby's suggestion for Insight-Out-Objects a shot.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        °) Though I'm not too sure if I got the Powershell and Windows right, since all the spared "keys" should cause a bigger impact. Will need to dig deeper. :)

        but I am quite certain (from the last time I read the documentation) that that cache is associated with the package STASH, rather than each object, since it logically applies to the class (package) and is shared between all objects of that class.

        That is the case.

        $ perl -M5.010 -e' package Foo { sub f { } } package Bar { our @ISA = "Foo"; } say for keys %{ $::{"Bar::"} }; say "---"; Bar->f(); say for keys %{ $::{"Bar::"} }; ' ISA --- ISA f

        Seeking work! You can reach me at ikegami@adaelis.com