From: Peter Rabbitson Date: Wed, 24 Apr 2013 03:23:43 +0000 (+0200) Subject: Stop instantiating a cursor on single() use X-Git-Tag: v0.08250~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=commitdiff_plain;h=735f190ce9798b79ae0608b62087cdd1d3c0b47e Stop instantiating a cursor on single() use This minor pessimization snuck in all the way back in 4e9fc3f3. I am actually not entirely sure whether this isn't a bug - after all this way Cursor::Cached and friends are bypassed. However 0.08210 has an identical behavior, thus sod it --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 3aa5d23..4e4103a 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1299,21 +1299,24 @@ sub _construct_results { $attrs->{_order_is_artificial} = 1; } - my $cursor = $self->cursor; - # this will be used as both initial raw-row collector AND as a RV of # _construct_results. Not regrowing the array twice matters a lot... # a surprising amount actually my $rows = delete $self->{_stashed_rows}; + my $cursor; # we may not need one at all + my $did_fetch_all = $fetch_all; if ($fetch_all) { # FIXME SUBOPTIMAL - we can do better, cursor->next/all (well diff. methods) should return a ref - $rows = [ ($rows ? @$rows : ()), $cursor->all ]; + $rows = [ ($rows ? @$rows : ()), $self->cursor->all ]; } elsif( $attrs->{collapse} ) { + # a cursor will need to be closed over in case of collapse + $cursor = $self->cursor; + $attrs->{_ordered_for_collapse} = ( ( $attrs->{order_by} @@ -1339,6 +1342,7 @@ sub _construct_results { if (! $did_fetch_all and ! @{$rows||[]} ) { # FIXME SUBOPTIMAL - we can do better, cursor->next/all (well diff. methods) should return a ref + $cursor ||= $self->cursor; if (scalar (my @r = $cursor->next) ) { $rows = [ \@r ]; } diff --git a/t/search/preserve_original_rs.t b/t/search/preserve_original_rs.t index 15fdb8d..a87fe9a 100644 --- a/t/search/preserve_original_rs.t +++ b/t/search/preserve_original_rs.t @@ -96,6 +96,7 @@ for my $s (qw/a2a artw cd artw_back/) { is ($fresh->count({ cdid => 1}), 1 ); is ($fresh->count_rs({ cdid => 1})->next, 1 ); + ok (! exists $fresh->{cursor}, 'Still no cursor on fresh rs'); ok (! exists $fresh->{_attrs}{_sqlmaker_select_args}, 'select args did not leak through' ); }