From: Peter Rabbitson Date: Mon, 14 Dec 2015 09:18:17 +0000 (+0100) Subject: Microoptimize various "is this string empty" checks X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d52c4a75538bd15d1fa531a63c6ec45b8943dfd0;p=dbsrgits%2FDBIx-Class.git Microoptimize various "is this string empty" checks Checking the truthiness of the separately tracked string length is vastly faster than doing actual string comparison Working set identified via: grep -nPr "\b(ne|eq)\b\s*(\"\"|''|q{}|q())" lib --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index e8b710c..be4a69e 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -442,7 +442,7 @@ sub search_rs { $call_cond = shift; } # fish out attrs in the ($condref, $attr) case - elsif (@_ == 2 and ( ! defined $_[0] or (ref $_[0]) ne '') ) { + elsif (@_ == 2 and ( ! defined $_[0] or length ref $_[0] ) ) { ($call_cond, $call_attrs) = @_; } elsif (@_ % 2) { @@ -456,7 +456,7 @@ sub search_rs { for my $i (0 .. $#_) { next if $i % 2; $self->throw_exception ('All keys in condition key/value pairs must be plain scalars') - if (! defined $_[$i] or ref $_[$i] ne ''); + if (! defined $_[$i] or length ref $_[$i] ); } $call_cond = { @_ }; diff --git a/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm b/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm index c471bf8..7756df7 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase/ASE.pm @@ -683,7 +683,8 @@ sub _remove_blob_cols { } else { $fields->{$col} = \"''"; - $blob_cols{$col} = $blob_val unless $blob_val eq ''; + $blob_cols{$col} = $blob_val + if length $blob_val; } } } @@ -709,7 +710,7 @@ sub _remove_blob_cols_array { else { $data->[$j][$i] = \"''"; $blob_cols[$j][$i] = $blob_val - unless $blob_val eq ''; + if length $blob_val; } } } diff --git a/lib/DBIx/Class/Storage/DBIHacks.pm b/lib/DBIx/Class/Storage/DBIHacks.pm index c129b1e..14410b7 100644 --- a/lib/DBIx/Class/Storage/DBIHacks.pm +++ b/lib/DBIx/Class/Storage/DBIHacks.pm @@ -1102,7 +1102,7 @@ sub _collapse_cond { for (sort keys %$chunk) { # Match SQLA 1.79 behavior - if ($_ eq '') { + unless( length $_ ) { is_literal_value($chunk->{$_}) ? carp 'Hash-pairs consisting of an empty string with a literal are deprecated, use -and => [ $literal ] instead' : $self->throw_exception("Supplying an empty left hand side argument is not supported in hash-pairs") @@ -1120,7 +1120,7 @@ sub _collapse_cond { # Match SQLA 1.79 behavior $self->throw_exception("Supplying an empty left hand side argument is not supported in array-pairs") - if $where_is_anded_array and (! defined $chunk or $chunk eq ''); + if $where_is_anded_array and (! defined $chunk or ! length $chunk); push @pairs, $chunk, shift @pieces; } @@ -1315,7 +1315,7 @@ sub _collapse_cond_unroll_pairs { while (@$pairs) { my ($lhs, $rhs) = splice @$pairs, 0, 2; - if ($lhs eq '') { + if (! length $lhs) { push @conds, $self->_collapse_cond($rhs); } elsif ( $lhs =~ /^\-and$/i ) { diff --git a/lib/DBIx/Class/Storage/TxnScopeGuard.pm b/lib/DBIx/Class/Storage/TxnScopeGuard.pm index 392e354..841337f 100644 --- a/lib/DBIx/Class/Storage/TxnScopeGuard.pm +++ b/lib/DBIx/Class/Storage/TxnScopeGuard.pm @@ -26,7 +26,7 @@ sub new { # There got to be a saner way of doing this... if (is_exception $@) { weaken( - $guard->{existing_exception_ref} = (ref($@) eq '') ? \$@ : $@ + $guard->{existing_exception_ref} = (length ref $@) ? $@ : \$@ ); } @@ -71,7 +71,7 @@ sub DESTROY { ( ! defined $self->{existing_exception_ref} or - refaddr( ref($@) eq '' ? \$@ : $@ ) != refaddr($self->{existing_exception_ref}) + refaddr( (length ref $@) ? $@ : \$@ ) != refaddr($self->{existing_exception_ref}) ) ); diff --git a/lib/DBIx/Class/_Util.pm b/lib/DBIx/Class/_Util.pm index 98da93c..362bb35 100644 --- a/lib/DBIx/Class/_Util.pm +++ b/lib/DBIx/Class/_Util.pm @@ -158,7 +158,10 @@ sub is_exception ($) { { local $@; eval { - $not_blank = ($e ne '') ? 1 : 0; + # The ne() here is deliberate - a plain length($e), or worse "$e" ne + # will entirely obviate the need for the encolsing eval{}, as the + # condition we guard against is a missing fallback overload + $not_blank = ( $e ne '' ); 1; } or $suberror = $@; } @@ -185,7 +188,7 @@ sub is_exception ($) { )); # workaround, keeps spice flowing - $not_blank = ("$e" ne '') ? 1 : 0; + $not_blank = !!( length $e ); } else { # not blessed yet failed the 'ne'... this makes 0 sense...