From: Balint Szilakszi Date: Mon, 8 Mar 2010 06:48:51 +0000 (+0000) Subject: Optimizing component lookup for the simple, common case and falling back on regexps... X-Git-Tag: 5.80025~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=13311c16a8aebc88a393baed64f3344e4c9a4229 Optimizing component lookup for the simple, common case and falling back on regexps otherwise. Noting the optimization in Changes. Also, adding myself to contributors. --- diff --git a/Changes b/Changes index 19bc2c5..ead2c17 100644 --- a/Changes +++ b/Changes @@ -4,11 +4,13 @@ - Log an extra line in debug mode with the response status code, the content type and content length if available. - Refactoring: + Refactoring / optimizations: - Display of the end of hit debug messages has been factored out into log_headers, log_request and log_response methods which all call $c->dump_these so that there is a unified point from which to hook in parameter filtering (for example). + - $c->model/view/controller have become a lot faster for non-regexp names + by using direct hash lookup instead of looping. Bug fixed: - DispatchType::Index's uri_for_action only returns for actions registered diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 3c7dd25..3a43f54 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -640,7 +640,13 @@ If you want to search for controllers, pass in a regexp as the argument. sub controller { my ( $c, $name, @args ) = @_; + my $appclass = ref($c) || $c; if( $name ) { + unless ( ref($name) ) { # Direct component hash lookup to avoid costly regexps + my $comps = $c->components; + my $check = $appclass."::Controller::".$name; + return $c->_filter_component( $comps->{$check}, @args ) if exists $comps->{$check}; + } my @result = $c->_comp_search_prefixes( $name, qw/Controller C/ ); return map { $c->_filter_component( $_, @args ) } @result if ref $name; return $c->_filter_component( $result[ 0 ], @args ); @@ -674,6 +680,11 @@ sub model { my ( $c, $name, @args ) = @_; my $appclass = ref($c) || $c; if( $name ) { + unless ( ref($name) ) { # Direct component hash lookup to avoid costly regexps + my $comps = $c->components; + my $check = $appclass."::Model::".$name; + return $c->_filter_component( $comps->{$check}, @args ) if exists $comps->{$check}; + } my @result = $c->_comp_search_prefixes( $name, qw/Model M/ ); return map { $c->_filter_component( $_, @args ) } @result if ref $name; return $c->_filter_component( $result[ 0 ], @args ); @@ -728,6 +739,11 @@ sub view { my $appclass = ref($c) || $c; if( $name ) { + unless ( ref($name) ) { # Direct component hash lookup to avoid costly regexps + my $comps = $c->components; + my $check = $appclass."::View::".$name; + return $c->_filter_component( $comps->{$check}, @args ) if exists $comps->{$check}; + } my @result = $c->_comp_search_prefixes( $name, qw/View V/ ); return map { $c->_filter_component( $_, @args ) } @result if ref $name; return $c->_filter_component( $result[ 0 ], @args ); @@ -3122,6 +3138,8 @@ Robert Sedlacek C<< >> sky: Arthur Bergman +szbalint: Balint Szilakszi + t0m: Tomas Doran Ulf Edvinsson