From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id E309B206A7; Fri, 28 Oct 2016 20:15:05 +0000 (UTC) From: Eric Wong To: http_spew-public@bogomips.org Cc: Eric Wong Subject: [PATCH 3/5] use frozen string literals for Ruby 2.1+ Date: Fri, 28 Oct 2016 20:15:01 +0000 Message-Id: <20161028201503.10315-4-e@80x24.org> In-Reply-To: <20161028201503.10315-1-e@80x24.org> References: <20161028201503.10315-1-e@80x24.org> List-Id: Constant lookups (even with caching) have extra size and speed costs compared to just using frozen string literals. This slows down performance on older Rubies (even if they become unsupported upstream), but newest C Ruby takes priority (currently 2.3). --- lib/http_spew/content_md5.rb | 4 +--- lib/http_spew/headers.rb | 23 +++++++---------------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/lib/http_spew/content_md5.rb b/lib/http_spew/content_md5.rb index 1c5be88..a999111 100644 --- a/lib/http_spew/content_md5.rb +++ b/lib/http_spew/content_md5.rb @@ -7,8 +7,6 @@ class HTTP_Spew::ContentMD5 attr_reader :content_md5 attr_reader :bytes_digested - CRLF = "\r\n" # :nodoc: - def initialize(env, input = env["rack.input"]) if trailer = env["HTTP_TRAILER"] unless trailer.split(/\s*,\s*/).grep(/\AContent-MD5\z/i)[0] @@ -41,7 +39,7 @@ class HTTP_Spew::ContentMD5 @bytes_digested += n wr.write("#{n.to_s(16)}\r\n") digest.update(buf) - wr.write(buf << CRLF) + wr.write(buf << "\r\n".freeze) end while input.read(0x4000, buf) end if expect_len && expect_len.to_i != @bytes_digested diff --git a/lib/http_spew/headers.rb b/lib/http_spew/headers.rb index da34c03..6f6f4d0 100644 --- a/lib/http_spew/headers.rb +++ b/lib/http_spew/headers.rb @@ -1,18 +1,10 @@ # -*- encoding: binary -*- module HTTP_Spew::Headers - # :stopdoc: - REQUEST_METHOD = "REQUEST_METHOD" - REQUEST_URI = "REQUEST_URI" - CRLF = "\r\n" - QUERY_STRING = "QUERY_STRING" - PATH_INFO = "PATH_INFO" - CONTENT_TYPE = "CONTENT_TYPE" # specified by Rack to be !/^HTTP_/ - # :startdoc: # regenerates the request_uri from a Rack +env+ def request_uri(env) - qs = env[QUERY_STRING] - qs.size == 0 ? env[PATH_INFO] : "#{env[PATH_INFO]}?#{qs}" + qs = env['QUERY_STRING'] + qs.size == 0 ? env['PATH_INFO'] : "#{env['PATH_INFO']}?#{qs}" end module_function :request_uri @@ -27,26 +19,25 @@ module HTTP_Spew::Headers # # buf, input = env_to_headers(env, input) def env_to_headers(env, input) - req = "#{env[REQUEST_METHOD]} " \ - "#{env[REQUEST_URI] || request_uri(env)} HTTP/1.1\r\n" \ + req = "#{env['REQUEST_METHOD']} " \ + "#{env['REQUEST_URI'] || request_uri(env)} HTTP/1.1\r\n" \ "Connection: close\r\n" - uscore, dash = "_", "-" env.each do |key,value| %r{\AHTTP_(\w+)\z} =~ key or next key = $1 %r{\A(?:VERSION|EXPECT|TRANSFER_ENCODING|CONNECTION|KEEP_ALIVE)\z}x =~ key and next - key.tr!(uscore, dash) + key.tr!('_'.freeze, '-'.freeze) req << "#{key}: #{value}\r\n" end if input req << (input.respond_to?(:size) ? "Content-Length: #{input.size}\r\n" : "Transfer-Encoding: chunked\r\n") - ct = env[CONTENT_TYPE] and req << "Content-Type: #{ct}\r\n" + ct = env['CONTENT_TYPE'] and req << "Content-Type: #{ct}\r\n" end - req << CRLF + req << "\r\n".freeze String === input ? (req << input) : [ req, input ] end module_function :env_to_headers -- EW