Click here to get back home

Inside-out objects are slow! (or how to accelerate OO Perl?)

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Inside-out objects are slow! (or how to accelerate OO Perl?) Koszalek Opalek 03-17-2008
Posted by Koszalek Opalek on March 17, 2008, 3:44 am
Please log in for more thread options
I was about to redesign my class to use inside-out objects.
I was hoping that except for a cleaner design it will also result in
some performance boost.
(I thought that 10 hashes with approx. 1000 keys will be faster than
1000 hashes with 10 keys.)

However, a simple experiment reveals that the opposite is true.
Inside-out objects are approximately 3 times slower in this example --
and it gets worse as the number of object grows.


bash-3.2$ time ./makeregularobj.pl
real 0m0.156s
user 0m0.093s
sys 0m0.000s

bash-3.2$ time ./makeinsideoutobj.pl
real 0m0.437s
user 0m0.358s
sys 0m0.015s

I attach the two files below. Any comments?

Apart from inside-out objects what other techniques could be used to
accelerate OO Perl?
I looked at the fields module but it has been removed from Perl 5.10.



#------ makeregularobj.pl

#!/usr/bin/perl
use strict;

my $no_obj = $ARGV[0] || 10_000;

{
package P;

sub new
{
        my $_class = shift;

        my %self;

        $self++;
        $self++;
        $self++;
        $self++;
        $self++;
        $self++;
        $self++;
        $self++;
        $self++;
        $self++;
        bless \%self, $_class;
}


};

my @objs;
for (1 .. $no_obj) {
        push @objs, P->new();
};

print "Created $no_obj objects (blessed hashes), data stored in ten
fields inside a hash.\n";



#------ makeinsideoutobj.pl
#!/usr/bin/perl
use strict;

my $no_obj = $ARGV[0] || 10_000;

{
my %field0;
my %field1;
my %field2;
my %field3;
my %field4;
my %field5;
my %field6;
my %field7;
my %field8;
my %field9;
{
package P;


sub new
{
        my $_class = shift;

        my $self = 1;

        $field0++;
        $field1++;
        $field2++;
        $field3++;
        $field4++;
        $field5++;
        $field6++;
        $field7++;
        $field8++;
        $field9++;
        bless $self, $_class;
};
};
};

P->import();

my @objs;
for (1 .. $no_obj) {
        push @objs, P->new();
};

print "Created $no_obj objects (blessed scalars), data stored in ten
inside-out hashes\n";



Posted by david on March 17, 2008, 4:41 am
Please log in for more thread options
> I was about to redesign my class to use inside-out objects.
> I was hoping that except for a cleaner design it will also result in
> some performance boost.
> (I thought that 10 hashes with approx. 1000 keys will be faster than
> 1000 hashes with 10 keys.)
>
> However, a simple experiment reveals that the opposite is true.
> Inside-out objects are approximately 3 times slower in this example --
> and it gets worse as the number of object grows.
>
> bash-3.2$ time ./makeregularobj.pl
> real 0m0.156s
> user 0m0.093s
> sys 0m0.000s
>
> bash-3.2$ time ./makeinsideoutobj.pl
> real 0m0.437s
> user 0m0.358s
> sys 0m0.015s
>
> I attach the two files below. Any comments?
>
> Apart from inside-out objects what other techniques could be used to
> accelerate OO Perl?
> I looked at the fields module but it has been removed from Perl 5.10.
>
> #------ makeregularobj.pl
>
> #!/usr/bin/perl
> use strict;
>
> my $no_obj = $ARGV[0] || 10_000;
>
> {
> package P;
>
> sub new
> {
> my $_class = shift;
>
> my %self;
>
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> bless \%self, $_class;
>
> }
> };
>
> my @objs;
> for (1 .. $no_obj) {
> push @objs, P->new();
>
> };
>
> print "Created $no_obj objects (blessed hashes), data stored in ten
> fields inside a hash.\n";
>
> #------ makeinsideoutobj.pl
> #!/usr/bin/perl
> use strict;
>
> my $no_obj = $ARGV[0] || 10_000;
>
> {
> my %field0;
> my %field1;
> my %field2;
> my %field3;
> my %field4;
> my %field5;
> my %field6;
> my %field7;
> my %field8;
> my %field9;
> {
> package P;
>
> sub new
> {
> my $_class = shift;
>
> my $self = 1;
>
> $field0++;
> $field1++;
> $field2++;
> $field3++;
> $field4++;
> $field5++;
> $field6++;
> $field7++;
> $field8++;
> $field9++;
> bless $self, $_class;
>
> };
> };
> };
>
> P->import();
>
> my @objs;
> for (1 .. $no_obj) {
> push @objs, P->new();
>
> };
>
> print "Created $no_obj objects (blessed scalars), data stored in ten
> inside-out hashes\n";

I think that the point of inside-out objects is not to make OO safer
and not faster, The real question is why do you need to make perl oo
faster. In most cases it is fast enough. There are pathological cases
where you have to construct billions of objects. In this case you may
choose a non oo solution (This is also true for compiled languages).
The most important lesson I learned as programmer is "don;t optimize,
profile". It can be that the bottle neck is in a place you never
thought.

Best regards,
David

P.S. In insisde out object you have to write a destructor to clean the
hashes

Posted by Koszalek Opalek on March 17, 2008, 1:33 pm
Please log in for more thread options

> I think that the point of inside-out objects is not to make OO safer
> and not faster, The real question is why do you need to make perl oo
> faster. In most cases it is fast enough.

So far, it has always been so for me... :-)

> There are pathological cases
> where you have to construct billions of objects. In this case you may
> choose a non oo solution (This is also true for compiled languages).

The thing is what is pathological for Perl?
Looks like this is thousands, not billions.

My module takes approx 1 sec to execute (on
the largest input). It has to create a few
thousand objects in the process. There is

1) Tree (only one)
2) Nodes (approximately 2ooo in the tree, and
then ~2ooo outside the Tree)
3) Streams (basically arrays holding references to Nodes
with some convenience methods, also a few thousands).

1 second is OK, 2 seconds would also be OK
but only just; so I am afraid that gives
me a very little safety margin for the future.

The thing is I never expected performance to be
a problem in the first place!

> The most important lesson I learned as programmer is "don;t optimize,
> profile". It can be that the bottle neck is in a place you never
> thought.

Sure, I ran the code through the profiler, I fixed some
brain-damage in the algorithm. That cut the execution
time by half. I can earn more by doing a few ugly things
like replacing $node->getproto() with $node-> but
it looks like I'll never be able to go down to say 0.2 sec.

K.

Posted by xhoster on March 17, 2008, 11:44 am
Please log in for more thread options
> I was about to redesign my class to use inside-out objects.
> I was hoping that except for a cleaner design it will also result in
> some performance boost.
> (I thought that 10 hashes with approx. 1000 keys will be faster than
> 1000 hashes with 10 keys.)

10 hashes with 100_000 keys might be more memory efficient than
100_000 hashes with 10 keys each, but I see no reason to think they
would be faster.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Koszalek Opalek on March 17, 2008, 1:14 pm
Please log in for more thread options
On Mar 17, 4:44 pm, xhos...@gmail.com wrote:

> 10 hashes with 100_000 keys might be more memory efficient than
> 100_000 hashes with 10 keys each,

Yes, that's what I read here:
http://www.perlfoundation.org/perl5/index.cgi?inside_out_object

> but I see no reason to think they
> would be faster.

That was just a shot in the dark, I was hoping for some
less-memory -> fewer-cache-misses -> better-overall-speed
effect.


Koszalek

Similar ThreadsPosted
At what (if any) point does InsideOut become OO-overkill? December 5, 2006, 2:41 pm
Accessing a container objects state from aggregated objects August 14, 2006, 8:50 pm
why is perl::ssh so slow ? January 5, 2006, 11:38 am
perl and mysql: slow inserts with innodb October 4, 2006, 11:37 am
Objects/Structures in Perl July 26, 2007, 5:00 pm
Perl + Objects = Winning combination April 9, 2007, 7:55 pm
Using "Perl Best Practices" inside-out objects May 15, 2007, 3:17 pm
Perl Standard for Remote Objects. June 28, 2007, 8:14 am
Exange of informations and objects between PERL and JAVA August 23, 2004, 4:48 am
[perl-python] 20050124 classes & objects January 24, 2005, 7:54 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap