[CDBI] Make name_lc go fast. Benchmarks

Michael G Schwern schwern at pobox.com
Tue Apr 10 13:02:38 BST 2007


Brad Bowman wrote:
> Michael G Schwern wrote:
>> FWIW name_lc() often pops up high in my profiles.  Its called as part
>> of the
>> column stringification override which is called very, very often.  So
>> yes, it
>> would be nice to optimize it away.
>>
>> There's probably no harm in simply changing the stringification sub to
>> be simply:
>>
>>     sub { lc shift->name };
>>
>> That would remove a layer of method calls.

You could remove yet another layer of method calls, and an overloaded operator
call, by changing all uses of a column as a string inside CDBI to a method
call.  Overloading is slow, nearly as bad as tying.

              Rate overload   method
overload  965480/s       --     -50%
method   1949590/s     102%       --

A simple way to find them all is to remove the operator overload from
CDBI::Column and run the test suite.  Then fill in the blanks.  Alternatively,
add a carp() to the string overload.
-------------- next part --------------
#!/usr/bin/perl -w

use strict;

{
	package Parent;
	sub new { bless \$_[1], $_[0] }
}

{
	package Foo;
	use base qw(Parent);
	use overload q[""] => sub { ${$_[0]} };
}

{
	package Bar;
	use base qw(Parent);
	sub string { ${$_[0]} }
}


use Benchmark ':all';

my $overload = Foo->new("Something");
my $method   = Bar->new("Something");
cmpthese( shift || -3, {
	overload => sub { "$overload" },
	method   => sub { $method->string },
});



More information about the ClassDBI mailing list