Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up Dancer #1231

Open
KES777 opened this issue Jan 12, 2022 · 0 comments
Open

Speed up Dancer #1231

KES777 opened this issue Jan 12, 2022 · 0 comments

Comments

@KES777
Copy link
Contributor

KES777 commented Jan 12, 2022

It seems Dancer do useless things if $content is returned by $self->execute (send_error)

--- a/lib/Dancer/Route.pm
+++ b/lib/Dancer/Route.pm
@@ -200,7 +200,15 @@ sub run {
         Dancer::Factory::Hook->execute_hooks('on_route_exception', $exception);
         die $exception;
     };
-    my $response = Dancer::SharedData->response;
+
+    if( ref($content) eq 'Dancer::Response' ) {
+        # coerce undef content to empty string to
+        # prevent warnings
+        $content = (defined $content) ? $content : '';
+
+        return $content;
+    }
+
 
     if ( $response && $response->is_forwarded ) {
         my $new_req =
@@ -228,10 +236,6 @@ sub run {
         return Dancer::Renderer->render_error(404);
     }
 
-    # coerce undef content to empty string to
-    # prevent warnings
-    $content = (defined $content) ? $content : '';
-
     my $ct =
       ( defined $response && defined $response->content_type )
       ? $response->content_type()
@@ -247,7 +251,6 @@ sub run {
     push(@$headers, 'Content-Type' => $ct)
       unless grep {/Content-Type/} @$headers;
 
-    return $content if ref($content) eq 'Dancer::Response';
     return Dancer::Response->new(
         status       => $st,
         headers      => $headers,

As second round of speed up we can check $response only once

--- a/lib/Dancer/Route.pm
+++ b/lib/Dancer/Route.pm
@@ -210,7 +210,14 @@ sub run {
     }
 
 
-    if ( $response && $response->is_forwarded ) {
+    my $response = Dancer::SharedData->response   or do{
+        return Dancer::Response->new(
+            status  => 200,
+            content => $content,
+        );
+    };
+
+    if ( $response->is_forwarded ) {
         my $new_req =
             Dancer::Request->forward($request, $response->{forward});
         my $marshalled = Dancer::Handler->handle_request($new_req);
@@ -224,7 +231,7 @@ sub run {
         );
     }
 
-    if ($response && $response->has_passed) {
+    if ($response->has_passed) {
         $response->pass(0);
 
         # find the next matching route and run it
@@ -236,23 +243,23 @@ sub run {
         return Dancer::Renderer->render_error(404);
     }
 
+
+    my $headers = [@{ $response->headers_to_array }];
+
+    if( !grep {/Content-Type/} @$headers ){
         my $ct =
-      ( defined $response && defined $response->content_type )
+          ( defined $response->content_type )
           ? $response->content_type()
           : setting('content_type');
 
-    my $st = defined $response ? $response->status : 200;
-
-    my $headers = [];
-    push @$headers, @{ $response->headers_to_array } if defined $response;
-
         # content type may have already be set earlier
         # (eg: with send_error)
         push(@$headers, 'Content-Type' => $ct)
-      unless grep {/Content-Type/} @$headers;
+
+    }
 
     return Dancer::Response->new(
-        status       => $st,
+        status       => $response->status,
         headers      => $headers,
         content      => $content,
     );

I am not so sure about second optimization, but it must be considered at least.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant