diff options
author | Eric Wong <e@80x24.org> | 2015-06-30 02:38:55 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2015-06-30 02:46:02 +0000 |
commit | 0d1ec2d1b864f3d39b4bf67fa6ac6c4aedc18136 (patch) | |
tree | d3ebbf2b0c4b1081d32ec6cdc82142e59e007ea5 /lib/yahns/http_response.rb | |
parent | baaebed4d7c50a3f14fbada767bada6fc2f0c8b5 (diff) | |
download | yahns-0d1ec2d1b864f3d39b4bf67fa6ac6c4aedc18136.tar.gz |
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.
Diffstat (limited to 'lib/yahns/http_response.rb')
-rw-r--r-- | lib/yahns/http_response.rb | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb index b70491d..f50c9a1 100644 --- a/lib/yahns/http_response.rb +++ b/lib/yahns/http_response.rb @@ -23,13 +23,7 @@ module Yahns::HttpResponse # :nodoc: end # avoid GC overhead for frequently used-strings: - CONN_KA = "Connection: keep-alive\r\n\r\n" - CONN_CLOSE = "Connection: close\r\n\r\n" - Z = "" CCC_RESPONSE_START = [ 'HTTP', '/1.1 ' ] - RESPONSE_START = CCC_RESPONSE_START.join - REQUEST_METHOD = "REQUEST_METHOD" - HEAD = "HEAD" # no point in using one without the other, these have been in Linux # for ages @@ -46,7 +40,7 @@ module Yahns::HttpResponse # :nodoc: end def response_start - @hs.response_start_sent ? Z : RESPONSE_START + @hs.response_start_sent ? ''.freeze : 'HTTP/1.1 '.freeze end def response_wait_write(rv) @@ -115,7 +109,7 @@ module Yahns::HttpResponse # :nodoc: end def kv_str(key, value) - if value =~ /\n/ + if value.include?("\n".freeze) # avoiding blank, key-only cookies with /\n+/ value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" }.join else @@ -124,7 +118,7 @@ module Yahns::HttpResponse # :nodoc: end def have_more?(value) - value.to_i > 0 && @hs.env[REQUEST_METHOD] != HEAD + value.to_i > 0 && @hs.env['REQUEST_METHOD'] != 'HEAD'.freeze end # writes the rack_response to socket as an HTTP response @@ -163,7 +157,8 @@ module Yahns::HttpResponse # :nodoc: buf << kv_str(key, value) end end - buf << (alive ? CONN_KA : CONN_CLOSE) + buf << (alive ? "Connection: keep-alive\r\n\r\n".freeze + : "Connection: close\r\n\r\n".freeze) case rv = kgio_syssend(buf, flags) when nil # all done, likely break |