From 1eddd7ee690f967c913d8c14505f4db994876568 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 3 May 2009 13:51:05 -0700 Subject: http_request: avoid StringIO.new for GET/HEAD requests 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. --- lib/unicorn/http_request.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'lib') 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 -- cgit v1.2.3-24-ge0c7