From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.9 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: yahns-public@yhbt.net Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id B8A1B63383F; Tue, 30 Jun 2015 03:02:05 +0000 (UTC) From: Eric Wong To: yahns-public@yhbt.net Cc: e@80x24.org Subject: [PATCH 3/3] http_response: reduce bytecode size Date: Tue, 30 Jun 2015 03:01:53 +0000 Message-Id: <1435633313-30223-4-git-send-email-e@80x24.org> In-Reply-To: <1435633313-30223-1-git-send-email-e@80x24.org> References: <1435633313-30223-1-git-send-email-e@80x24.org> List-Id: 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(-) 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 -- EW