From: Peter Rabbitson Date: Wed, 20 Feb 2013 10:12:35 +0000 (+0100) Subject: Optimize the HRI-direct collapser even more X-Git-Tag: v0.08241~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7596ddca6b7b39d56f7d5526be65855d80c81c4b;p=dbsrgits%2FDBIx-Class.git Optimize the HRI-direct collapser even more Since we know that we will never output null-branches in the case of HRI, we can wrap the related assemblers in a definedness condition to never fire on a null branch, and skip entirely the calculation of non-NULL %cur_row_ids. --- diff --git a/Changes b/Changes index 783f789..3915b79 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,8 @@ Revision history for DBIx::Class - Revert to passing the original (pre-0.08240) arguments to inflate_result() and remove the warning about ResultClass inheritance. + - Optimize the generated rowparsers even more - no user-visible + changes. 0.08240-TRIAL (EXPERIMENTAL BETA RELEASE) 2013-02-14 05:56 (UTC) * New Features / Changes diff --git a/lib/DBIx/Class/ResultSource/RowParser/Util.pm b/lib/DBIx/Class/ResultSource/RowParser/Util.pm index 48d2b08..4d833d3 100644 --- a/lib/DBIx/Class/ResultSource/RowParser/Util.pm +++ b/lib/DBIx/Class/ResultSource/RowParser/Util.pm @@ -127,11 +127,12 @@ sub assemble_collapsing_parser { my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1; - $top_node_key_assembler = sprintf '$cur_row_ids{%d} = (%s);', - $virtual_column_idx, - "\n" . join( "\n or\n", @path_parts, qq{"\0\$rows_pos\0"} ); + $top_node_key = "{'\xFF__IDVALPOS__${virtual_column_idx}__\xFF'}"; - $top_node_key = sprintf '{$cur_row_ids{%d}}', $virtual_column_idx; + $top_node_key_assembler = sprintf "'\xFF__IDVALPOS__%d__\xFF' = (%s);", + $virtual_column_idx, + "\n" . join( "\n or\n", @path_parts, qq{"\0\$rows_pos\0"} ) + ; $args->{collapse_map} = { %{$args->{collapse_map}}, @@ -145,11 +146,18 @@ sub assemble_collapsing_parser { my ($data_assemblers, $stats) = __visit_infmap_collapse ($args); - my $list_of_idcols = join(', ', sort { $a <=> $b } keys %{ $stats->{idcols_seen} } ); + my @idcol_args = $args->{hri_style} ? ('', '') : ( + '%cur_row_ids, ', # only declare the variable if we'll use it + + sprintf( <<'EOS', join ', ', sort { $a <=> $b } keys %{ $stats->{idcols_seen} } ), + $cur_row_ids{$_} = defined($cur_row_data->[$_]) ? $cur_row_data->[$_] : "\0NULL\xFF$rows_pos\xFF$_\0" + for (%s); +EOS + ); - my $parser_src = sprintf (<<'EOS', $list_of_idcols, $top_node_key, $top_node_key_assembler||'', join( "\n", @{$data_assemblers||[]} ) ); + my $parser_src = sprintf (<<'EOS', @idcol_args, $top_node_key_assembler||'', $top_node_key, join( "\n", @{$data_assemblers||[]} ) ); ### BEGIN LITERAL STRING EVAL - my ($rows_pos, $result_pos, $cur_row_data, %%cur_row_ids, @collapse_idx, $is_new_res) = (0,0); + my ($rows_pos, $result_pos, $cur_row_data,%1$s @collapse_idx, $is_new_res) = (0,0); # this loop is a bit arcane - the rationale is that the passed in # $_[0] will either have only one row (->next) or will have all # rows already pulled in (->all and/or unordered). Given that the @@ -161,24 +169,25 @@ sub assemble_collapsing_parser { || ($_[1] and $_[1]->()) ) { + # this code exists only when we are *not* assembling direct to HRI + # # due to left joins some of the ids may be NULL/undef, and # won't play well when used as hash lookups # we also need to differentiate NULLs on per-row/per-col basis # (otherwise folding of optional 1:1s will be greatly confused - $cur_row_ids{$_} = defined $cur_row_data->[$_] ? $cur_row_data->[$_] : "\0NULL\xFF$rows_pos\xFF$_\0" - for (%1$s); + %2$s - # maybe(!) cache the top node id calculation + # in the case of an underdefined root - calculate the virtual id (otherwise no code at all) %3$s - $is_new_res = ! $collapse_idx[0]%2$s and ( + $is_new_res = ! $collapse_idx[0]%4$s and ( $_[1] and $result_pos and (unshift @{$_[2]}, $cur_row_data) and last ); # the rel assemblers -%4$s +%5$s - $_[0][$result_pos++] = $collapse_idx[0]%2$s + $_[0][$result_pos++] = $collapse_idx[0]%4$s if $is_new_res; } @@ -189,7 +198,7 @@ EOS # !!! note - different var than the one above # change the quoted placeholders to unquoted alias-references $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex; - $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /"\$cur_row_ids{$1}"/gex; + $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /$args->{hri_style} ? "\$cur_row_data->[$1]" : "\$cur_row_ids{$1}" /gex; $parser_src = " { use strict; use warnings FATAL => 'all';\n$parser_src\n }"; } @@ -292,16 +301,16 @@ sub __visit_infmap_collapse { if ($args->{hri_style}) { - $src[$rel_src_pos] = sprintf( '%s and %s', - "( defined '\xFF__VALPOS__${first_distinct_child_idcol}__\xFF' )", - $src[$rel_src_pos], - ); - - splice @src, $rel_src_pos + 1, 0, sprintf ( '%s{%s} ||= %s;', + # start of wrap of the entire chain in a conditional + splice @src, $rel_src_pos, 0, sprintf "( ! defined %s )\n ? %s{%s} = %s\n : do {", + "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'", $node_idx_slot, perlstring($rel), - $relinfo->{-is_single} ? 'undef' : '[]', - ); + $relinfo->{-is_single} ? 'undef' : '[]' + ; + + # end of wrap + push @src, '};' } else { diff --git a/t/resultset/rowparser_internals.t b/t/resultset/rowparser_internals.t index 1317bb3..5bdebac 100644 --- a/t/resultset/rowparser_internals.t +++ b/t/resultset/rowparser_internals.t @@ -268,7 +268,7 @@ is_same_src ( collapse => 1, hri_style => 1, }), - ' my($rows_pos, $result_pos, $cur_row_data, %cur_row_ids, @collapse_idx, $is_new_res) = (0, 0); + ' my($rows_pos, $result_pos, $cur_row_data, @collapse_idx, $is_new_res) = (0, 0); while ($cur_row_data = ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; undef } ) @@ -276,48 +276,45 @@ is_same_src ( ( $_[1] and $_[1]->() ) ) { - $cur_row_ids{$_} = defined $cur_row_data->[$_] ? $cur_row_data->[$_] : "\0NULL\xFF$rows_pos\xFF$_\0" - for (0, 1, 3, 4, 5); - # a present cref in $_[1] implies lazy prefetch, implies a supplied stash in $_[2] $_[1] and $result_pos and unshift(@{$_[2]}, $cur_row_data) and last - if ( $is_new_res = ! $collapse_idx[0]{$cur_row_ids{4}}{$cur_row_ids{5}} ); + if ( $is_new_res = ! $collapse_idx[0]{$cur_row_data->[4]}{$cur_row_data->[5]} ); # the rowdata itself for root node - $collapse_idx[0]{$cur_row_ids{4}}{$cur_row_ids{5}} ||= { artist => $cur_row_data->[5], title => $cur_row_data->[4], year => $cur_row_data->[2] }; + $collapse_idx[0]{$cur_row_data->[4]}{$cur_row_data->[5]} ||= { artist => $cur_row_data->[5], title => $cur_row_data->[4], year => $cur_row_data->[2] }; # prefetch data of single_track (placed in root) - $collapse_idx[0]{$cur_row_ids{4}}{$cur_row_ids{5}}{single_track} ||= $collapse_idx[1]{$cur_row_ids{1}}{$cur_row_ids{4}}{$cur_row_ids{5}} - if defined $cur_row_data->[1]; - $collapse_idx[0]{$cur_row_ids{4}}{$cur_row_ids{5}}{single_track} ||= undef; + (! defined($cur_row_data->[1]) ) ? $collapse_idx[0]{$cur_row_data->[4]}{$cur_row_data->[5]}{single_track} = undef : do { + $collapse_idx[0]{$cur_row_data->[4]}{$cur_row_data->[5]}{single_track} ||= $collapse_idx[1]{$cur_row_data->[1]}{$cur_row_data->[4]}{$cur_row_data->[5]}; - # prefetch data of cd (placed in single_track) - $collapse_idx[1]{$cur_row_ids{1}}{$cur_row_ids{4}}{$cur_row_ids{5}}{cd} ||= $collapse_idx[2]{$cur_row_ids{1}}{$cur_row_ids{4}}{$cur_row_ids{5}}; + # prefetch data of cd (placed in single_track) + $collapse_idx[1]{$cur_row_data->[1]}{$cur_row_data->[4]}{$cur_row_data->[5]}{cd} ||= $collapse_idx[2]{$cur_row_data->[1]}{$cur_row_data->[4]}{$cur_row_data->[5]}; - # prefetch data of artist ( placed in single_track->cd) - $collapse_idx[2]{$cur_row_ids{1}}{$cur_row_ids{4}}{$cur_row_ids{5}}{artist} ||= $collapse_idx[3]{$cur_row_ids{1}}{$cur_row_ids{4}}{$cur_row_ids{5}} ||= { artistid => $cur_row_data->[1] }; + # prefetch data of artist ( placed in single_track->cd) + $collapse_idx[2]{$cur_row_data->[1]}{$cur_row_data->[4]}{$cur_row_data->[5]}{artist} ||= $collapse_idx[3]{$cur_row_data->[1]}{$cur_row_data->[4]}{$cur_row_data->[5]} ||= { artistid => $cur_row_data->[1] }; - # prefetch data of cds (if available) - ( defined $cur_row_data->[3] ) - and - (! $collapse_idx[4]{$cur_row_ids{1}}{$cur_row_ids{3}}{$cur_row_ids{4}}{$cur_row_ids{5}} ) - and - push @{$collapse_idx[3]{$cur_row_ids{1}}{$cur_row_ids{4}}{$cur_row_ids{5}}{cds}}, ( - $collapse_idx[4]{$cur_row_ids{1}}{$cur_row_ids{3}}{$cur_row_ids{4}}{$cur_row_ids{5}} = { cdid => $cur_row_data->[3] } - ); - $collapse_idx[3]{$cur_row_ids{1}}{$cur_row_ids{4}}{$cur_row_ids{5}}{cds} ||= []; + # prefetch data of cds (if available) + (! defined $cur_row_data->[3] ) ? $collapse_idx[3]{$cur_row_data->[1]}{$cur_row_data->[4]}{$cur_row_data->[5]}{cds} = [] : do { - # prefetch data of tracks (if available) - ( defined $cur_row_data->[0] ) - and - (! $collapse_idx[5]{$cur_row_ids{0}}{$cur_row_ids{1}}{$cur_row_ids{3}}{$cur_row_ids{4}}{$cur_row_ids{5}} ) - and - push @{$collapse_idx[4]{$cur_row_ids{1}}{$cur_row_ids{3}}{$cur_row_ids{4}}{$cur_row_ids{5}}{tracks}}, ( - $collapse_idx[5]{$cur_row_ids{0}}{$cur_row_ids{1}}{$cur_row_ids{3}}{$cur_row_ids{4}}{$cur_row_ids{5}} = { title => $cur_row_data->[0] } - ); - $collapse_idx[4]{$cur_row_ids{1}}{$cur_row_ids{3}}{$cur_row_ids{4}}{$cur_row_ids{5}}{tracks} ||= []; + (! $collapse_idx[4]{$cur_row_data->[1]}{$cur_row_data->[3]}{$cur_row_data->[4]}{$cur_row_data->[5]} ) + and + push @{$collapse_idx[3]{$cur_row_data->[1]}{$cur_row_data->[4]}{$cur_row_data->[5]}{cds}}, ( + $collapse_idx[4]{$cur_row_data->[1]}{$cur_row_data->[3]}{$cur_row_data->[4]}{$cur_row_data->[5]} = { cdid => $cur_row_data->[3] } + ); - $_[0][$result_pos++] = $collapse_idx[0]{$cur_row_ids{4}}{$cur_row_ids{5}} + # prefetch data of tracks (if available) + ( ! defined $cur_row_data->[0] ) ? $collapse_idx[4]{$cur_row_data->[1]}{$cur_row_data->[3]}{$cur_row_data->[4]}{$cur_row_data->[5]}{tracks} = [] : do { + + (! $collapse_idx[5]{$cur_row_data->[0]}{$cur_row_data->[1]}{$cur_row_data->[3]}{$cur_row_data->[4]}{$cur_row_data->[5]} ) + and + push @{$collapse_idx[4]{$cur_row_data->[1]}{$cur_row_data->[3]}{$cur_row_data->[4]}{$cur_row_data->[5]}{tracks}}, ( + $collapse_idx[5]{$cur_row_data->[0]}{$cur_row_data->[1]}{$cur_row_data->[3]}{$cur_row_data->[4]}{$cur_row_data->[5]} = { title => $cur_row_data->[0] } + ); + }; + }; + }; + + $_[0][$result_pos++] = $collapse_idx[0]{$cur_row_data->[4]}{$cur_row_data->[5]} if $is_new_res; } splice @{$_[0]}, $result_pos; @@ -638,7 +635,7 @@ is_same_src ( collapse => 1, hri_style => 1, }), - ' my($rows_pos, $result_pos, $cur_row_data, %cur_row_ids, @collapse_idx, $is_new_res) = (0, 0); + ' my($rows_pos, $result_pos, $cur_row_data, @collapse_idx, $is_new_res) = (0, 0); while ($cur_row_data = ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; undef } ) @@ -646,11 +643,8 @@ is_same_src ( ( $_[1] and $_[1]->() ) ) { - $cur_row_ids{$_} = defined $$cur_row_data[$_] ? $$cur_row_data[$_] : "\0NULL\xFF$rows_pos\xFF$_\0" - for (0, 2, 3, 4, 8); - # cache expensive set of ops in a non-existent rowid slot - $cur_row_ids{10} = ( + $cur_row_data->[10] = ( ( ( defined $cur_row_data->[0] ) && (join "\xFF", q{}, $cur_row_data->[0], q{} )) or ( ( defined $cur_row_data->[2] ) && (join "\xFF", q{}, $cur_row_data->[2], q{} )) @@ -660,47 +654,46 @@ is_same_src ( # a present cref in $_[1] implies lazy prefetch, implies a supplied stash in $_[2] $_[1] and $result_pos and unshift(@{$_[2]}, $cur_row_data) and last - if ( $is_new_res = ! $collapse_idx[0]{$cur_row_ids{10}} ); + if ( $is_new_res = ! $collapse_idx[0]{$cur_row_data->[10]} ); - $collapse_idx[0]{$cur_row_ids{10}} ||= { year => $$cur_row_data[1] }; + $collapse_idx[0]{$cur_row_data->[10]} ||= { year => $$cur_row_data[1] }; - (defined $cur_row_data->[0]) - and - $collapse_idx[0]{$cur_row_ids{10}}{single_track} ||= ($collapse_idx[1]{$cur_row_ids{0}} ||= { trackid => $$cur_row_data[0] }); - $collapse_idx[0]{$cur_row_ids{10}}{single_track} ||= undef; + (! defined $cur_row_data->[0] ) ? $collapse_idx[0]{$cur_row_data->[10]}{single_track} = undef : do { - $collapse_idx[1]{$cur_row_ids{0}}{cd} ||= $collapse_idx[2]{$cur_row_ids{0}}; + $collapse_idx[0]{$cur_row_data->[10]}{single_track} ||= ($collapse_idx[1]{$cur_row_data->[0]} ||= { trackid => $$cur_row_data[0] }); - $collapse_idx[2]{$cur_row_ids{0}}{artist} ||= ($collapse_idx[3]{$cur_row_ids{0}} ||= { artistid => $$cur_row_data[6] }); + $collapse_idx[1]{$cur_row_data->[0]}{cd} ||= $collapse_idx[2]{$cur_row_data->[0]}; - (defined $cur_row_data->[4]) - and - (! $collapse_idx[4]{$cur_row_ids{0}}{$cur_row_ids{4}} ) - and - push @{$collapse_idx[3]{$cur_row_ids{0}}{cds}}, ( - $collapse_idx[4]{$cur_row_ids{0}}{$cur_row_ids{4}} = { cdid => $$cur_row_data[4], genreid => $$cur_row_data[7], year => $$cur_row_data[5] } - ); - $collapse_idx[3]{$cur_row_ids{0}}{cds} ||= []; + $collapse_idx[2]{$cur_row_data->[0]}{artist} ||= ($collapse_idx[3]{$cur_row_data->[0]} ||= { artistid => $$cur_row_data[6] }); - (defined $cur_row_data->[8]) - and - (! $collapse_idx[5]{$cur_row_ids{0}}{$cur_row_ids{4}}{$cur_row_ids{8}} ) - and - push @{$collapse_idx[4]{$cur_row_ids{0}}{$cur_row_ids{4}}{tracks}}, ( - $collapse_idx[5]{$cur_row_ids{0}}{$cur_row_ids{4}}{$cur_row_ids{8}} = { title => $$cur_row_data[8] } - ); - $collapse_idx[4]{$cur_row_ids{0}}{$cur_row_ids{4}}{tracks} ||= []; + (! defined $cur_row_data->[4] ) ? $collapse_idx[3]{$cur_row_data->[0]}{cds} = [] : do { - (defined $cur_row_data->[2]) - and - (! $collapse_idx[6]{$cur_row_ids{2}}{$cur_row_ids{3}} ) - and - push @{$collapse_idx[0]{$cur_row_ids{10}}{tracks}}, ( - $collapse_idx[6]{$cur_row_ids{2}}{$cur_row_ids{3}} = { cd => $$cur_row_data[2], title => $$cur_row_data[3] } - ); - $collapse_idx[0]{$cur_row_ids{10}}{tracks} ||= []; + (! $collapse_idx[4]{$cur_row_data->[0]}{$cur_row_data->[4]} ) + and + push @{$collapse_idx[3]{$cur_row_data->[0]}{cds}}, ( + $collapse_idx[4]{$cur_row_data->[0]}{$cur_row_data->[4]} = { cdid => $$cur_row_data[4], genreid => $$cur_row_data[7], year => $$cur_row_data[5] } + ); - $_[0][$result_pos++] = $collapse_idx[0]{$cur_row_ids{10}} + (! defined $cur_row_data->[8] ) ? $collapse_idx[4]{$cur_row_data->[0]}{$cur_row_data->[4]}{tracks} = [] : do { + + (! $collapse_idx[5]{$cur_row_data->[0]}{$cur_row_data->[4]}{$cur_row_data->[8]} ) + and + push @{$collapse_idx[4]{$cur_row_data->[0]}{$cur_row_data->[4]}{tracks}}, ( + $collapse_idx[5]{$cur_row_data->[0]}{$cur_row_data->[4]}{$cur_row_data->[8]} = { title => $$cur_row_data[8] } + ); + }; + }; + }; + + (! defined $cur_row_data->[2] ) ? $collapse_idx[0]{$cur_row_data->[10]}{tracks} = [] : do { + (! $collapse_idx[6]{$cur_row_data->[2]}{$cur_row_data->[3]} ) + and + push @{$collapse_idx[0]{$cur_row_data->[10]}{tracks}}, ( + $collapse_idx[6]{$cur_row_data->[2]}{$cur_row_data->[3]} = { cd => $$cur_row_data[2], title => $$cur_row_data[3] } + ); + }; + + $_[0][$result_pos++] = $collapse_idx[0]{$cur_row_data->[10]} if $is_new_res; }