diff options
author | Eric Wong <e@80x24.org> | 2015-02-09 09:12:10 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2015-03-01 05:33:48 +0000 |
commit | ae5757cdb0be1d512c4b0c3c599e681f8bd3b5fe (patch) | |
tree | e07c8c5f71b9172d523669034da595fdfaa1cabd /lib/unicorn/http_server.rb | |
parent | 5fd3b3166a4ef9c0c5e4321c1b8b840840cbf24b (diff) | |
download | unicorn-ae5757cdb0be1d512c4b0c3c599e681f8bd3b5fe.tar.gz |
Literal String#freeze avoids allocations since Ruby 2.1 via the opt_str_freeze instruction, so we can start relying on it in some places as Ruby 2.1 adoption increases. The 100-continue handling is a good place to start since it is an uncommonly-used code path which benefits from size reduction and the negative performance impact is restricted to a handful of users. HTTP_RESPONSE_START can safely live in http_request.rb as its usage does not cross namespace boundaries The goal is to eventually eliminate Unicorn::Const entirely.
Diffstat (limited to 'lib/unicorn/http_server.rb')
-rw-r--r-- | lib/unicorn/http_server.rb | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb index 683eb82..77345d2 100644 --- a/lib/unicorn/http_server.rb +++ b/lib/unicorn/http_server.rb @@ -543,12 +543,15 @@ class Unicorn::HttpServer rescue end - def expect_100_response - if @request.response_start_sent - Unicorn::Const::EXPECT_100_RESPONSE_SUFFIXED - else - Unicorn::Const::EXPECT_100_RESPONSE - end + def e100_response_write(client, env) + # We use String#freeze to avoid allocations under Ruby 2.1+ + # Not many users hit this code path, so it's better to reduce the + # constant table sizes even for 1.9.3-2.0 users who'll hit extra + # allocations here. + client.write(@request.response_start_sent ? + "100 Continue\r\n\r\nHTTP/1.1 ".freeze : + "HTTP/1.1 100 Continue\r\n\r\n".freeze) + env.delete('HTTP_EXPECT'.freeze) end # once a client is accepted, it is processed in its entirety here @@ -558,8 +561,7 @@ class Unicorn::HttpServer return if @request.hijacked? if 100 == status.to_i - client.write(expect_100_response) - env.delete(Unicorn::Const::HTTP_EXPECT) + e100_response_write(client, env) status, headers, body = @app.call(env) return if @request.hijacked? end |