From d3f28e0fa44318f9af4409fc9b865d5094ccc1ee Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 30 Jun 2015 02:54:26 +0000 Subject: http_response: reduce bytecode size This saves around 200 bytes on x86-64 and potentially improves CPU cache performance. This does not reduce inline method cache overhead as String#<< already has optimized dispatch support (opt_ltlt in insns.def in Ruby 1.9+) --- lib/yahns/http_response.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb index f50c9a1..19075bb 100644 --- a/lib/yahns/http_response.rb +++ b/lib/yahns/http_response.rb @@ -108,12 +108,12 @@ module Yahns::HttpResponse # :nodoc: end end - def kv_str(key, value) + def kv_str(buf, key, value) if value.include?("\n".freeze) # avoiding blank, key-only cookies with /\n+/ - value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" }.join + value.split(/\n+/).each { |v| buf << "#{key}: #{v}\r\n" } else - "#{key}: #{value}\r\n" + buf << "#{key}: #{value}\r\n" end end @@ -144,17 +144,17 @@ module Yahns::HttpResponse # :nodoc: offset = $1.to_i count = $2.to_i - offset + 1 end - buf << kv_str(key, value) + kv_str(buf, key, value) when %r{\AConnection\z}i # allow Rack apps to tell us they want to drop the client alive = false if value =~ /\bclose\b/i when %r{\AContent-Length\z}i flags |= MSG_MORE if have_more?(value) - buf << kv_str(key, value) + kv_str(buf, key, value) when "rack.hijack" hijack = value else - buf << kv_str(key, value) + kv_str(buf, key, value) end end buf << (alive ? "Connection: keep-alive\r\n\r\n".freeze -- cgit v1.2.3-24-ge0c7