Optimizing component lookup for the simple, common case and falling back on regexps...
Balint Szilakszi [Mon, 8 Mar 2010 06:48:51 +0000 (06:48 +0000)]
Changes
lib/Catalyst.pm

diff --git a/Changes b/Changes
index 19bc2c5..ead2c17 100644 (file)
--- 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
index 3c7dd25..3a43f54 100644 (file)
@@ -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<< <rs@474.at> >>
 
 sky: Arthur Bergman
 
+szbalint: Balint Szilakszi <szbalint@cpan.org>
+
 t0m: Tomas Doran <bobtfish@bobtfish.net>
 
 Ulf Edvinsson