4 # So you wrote a new mk_hash implementation which passed all tests (particularly
5 # t/68inflate_resultclass_hashrefinflator) and would like to see how it holds up
6 # against older versions of the same. Just add your coderef to the HRI::Bench
7 # namespace and add a name/ref pair to the %bench_list hash. Happy testing.
9 package DBIx::Class::ResultClass::HashRefInflator::Bench;
15 $current_mk_hash = sub {
16 if (ref $_[0] eq 'ARRAY') { # multi relationship
17 return [ map { $current_mk_hash->(@$_) || () } (@_) ];
21 # the main hash could be an undef if we are processing a skipped-over join
22 $_[0] ? %{$_[0]} : (),
24 # the second arg is a hash of arrays for each prefetched relation
26 { $_ => $current_mk_hash->( @{$_[1]->{$_}} ) }
27 ( $_[1] ? (keys %{$_[1]}) : () )
30 # if there is at least one defined column consider the resultset real
31 # (and not an emtpy has_many rel containing one empty hashref)
33 return $hash if defined $_;
40 # the (incomplete, fails a test) implementation before svn:4760
45 # $me is the hashref of cols/data from the immediate resultsource
46 # $rest is a deep hashref of all the data from the prefetched
49 # to avoid emtpy has_many rels contain one empty hashref
50 return undef if (not keys %$me);
54 foreach (values %$me) {
60 return undef unless $def;
65 ref($rest->{$_}[0]) eq 'ARRAY'
66 ? [ grep defined, map $old_mk_hash->(@$_), @{$rest->{$_}} ]
67 : $old_mk_hash->( @{$rest->{$_}} )
75 current_implementation => $current_mk_hash,
76 old_implementation => $old_mk_hash,
81 package benchmark_hashrefinflator;
87 use lib ("$FindBin::Bin/../lib", "$FindBin::Bin/../t/lib");
89 use Benchmark qw/timethis cmpthese/;
92 chdir ("$FindBin::Bin/..");
93 my $schema = DBICTest->init_schema();
96 my $rs_hashrefinf = $schema->resultset ('Artist')->search ({}, {
97 prefetch => { cds => 'tracks' },
99 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator::Bench');
100 my @stuff = $rs_hashrefinf->all;
105 for my $b (keys %DBIx::Class::ResultClass::HashRefInflator::Bench::bench_list) {
107 print "Timing $b... ";
109 # switch the inflator
110 no warnings qw/redefine once/;
112 local *DBIx::Class::ResultClass::HashRefInflator::Bench::inflate_result = sub {
113 return $DBIx::Class::ResultClass::HashRefInflator::Bench::bench_list{$b}->(@_[2,3]);
116 $results->{$b} = timethis (-2, $test_sub);