about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-05-03 13:51:05 -0700
committerEric Wong <normalperson@yhbt.net>2009-05-03 13:51:05 -0700
commit1eddd7ee690f967c913d8c14505f4db994876568 (patch)
treeedea7a4867a15fd0be209c4b43653d67d3486e36
parent362d01204be6e0b7c04e8891499b4fe4e12fd535 (diff)
downloadunicorn-1eddd7ee690f967c913d8c14505f4db994876568.tar.gz
Since the vast majority of web traffic is GET/HEAD
requests without bodies, avoid creating a StringIO
object for every single request that comes in.
-rw-r--r--lib/unicorn/http_request.rb14
1 files changed, 10 insertions, 4 deletions
diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb
index 424a54f..a51e029 100644
--- a/lib/unicorn/http_request.rb
+++ b/lib/unicorn/http_request.rb
@@ -25,6 +25,9 @@ module Unicorn
        "SERVER_SOFTWARE" => "Unicorn #{Const::UNICORN_VERSION}".freeze
      }.freeze
 
+    # Optimize for the common case where there's no request body
+    # (GET/HEAD) requests.
+    NULL_IO = StringIO.new
     LOCALHOST = '127.0.0.1'.freeze
 
     # Being explicitly single-threaded, we have certain advantages in
@@ -39,10 +42,13 @@ module Unicorn
     end
 
     def reset
-      PARAMS[Const::RACK_INPUT].close rescue nil
-      PARAMS[Const::RACK_INPUT].close! rescue nil
-      PARSER.reset
+      input = PARAMS[Const::RACK_INPUT]
+      if input != NULL_IO
+        input.close rescue nil
+        input.close! rescue nil
+      end
       PARAMS.clear
+      PARSER.reset
     end
 
     # Does the majority of the IO processing.  It has been written in
@@ -99,7 +105,7 @@ module Unicorn
       content_length = PARAMS[Const::CONTENT_LENGTH].to_i
 
       if content_length == 0 # short circuit the common case
-        PARAMS[Const::RACK_INPUT] = StringIO.new
+        PARAMS[Const::RACK_INPUT] = NULL_IO.closed? ? NULL_IO.reopen : NULL_IO
         return PARAMS.update(DEF_PARAMS)
       end