From fb2f10e1d7a72e6787720003342a21f11b879614 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 30 Jun 2015 22:19:20 +0000 Subject: reduce constants and optimize for Ruby 2.2 Ruby (MRI) 2.1 optimizes allocations away on String#freeze with literal strings. Furthermore, Ruby 2.2 optimizes away literal string allocations when they are used as arguments to Hash#[] and Hash#[]= Thus we can avoid expensive constant lookups and cache overhead by taking advantage of advancements in Ruby. Since Ruby 2.2 has been out for 7 months, now; it ought to be safe to introduce minor performance regressions for folks using older Rubies (1.9.3+ remains supported) to benefit folks on the latest Ruby. This should recover the performance lost in the "reflect changes in Rack::Utils::HTTP_STATUS_CODES" change in synthetic benchmarks. --- lib/unicorn/http_response.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lib/unicorn/http_response.rb') diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb index a42303e..ec8b7c8 100644 --- a/lib/unicorn/http_response.rb +++ b/lib/unicorn/http_response.rb @@ -10,8 +10,6 @@ # is the job of Rack, with the exception of the "Date" and "Status" header. module Unicorn::HttpResponse - CRLF = "\r\n" - # internal API, code will always be common-enough-for-even-old-Rack def err_response(code, response_start_sent) "#{response_start_sent ? '' : 'HTTP/1.1 '}" \ @@ -39,7 +37,7 @@ module Unicorn::HttpResponse # key in Rack < 1.5 hijack = value else - if value =~ /\n/ + if value.include?("\n".freeze) # avoiding blank, key-only cookies with /\n+/ buf << value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" }.join else @@ -47,7 +45,7 @@ module Unicorn::HttpResponse end end end - socket.write(buf << CRLF) + socket.write(buf << "\r\n".freeze) end if hijack -- cgit v1.2.3-24-ge0c7