From: Eric Wong <e@80x24.org>
To: http_spew-public@bogomips.org
Cc: Eric Wong <e@80x24.org>
Subject: [PATCH 3/5] use frozen string literals for Ruby 2.1+
Date: Fri, 28 Oct 2016 20:15:01 +0000 [thread overview]
Message-ID: <20161028201503.10315-4-e@80x24.org> (raw)
In-Reply-To: <20161028201503.10315-1-e@80x24.org>
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 +---
| 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
--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
next prev parent reply other threads:[~2016-10-28 20:15 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-28 20:14 [PATCH 0/5] random half-brained updates Eric Wong
2016-10-28 20:14 ` [PATCH 1/5] declare empty classes with constant assignment Eric Wong
2016-10-28 20:15 ` [PATCH 2/5] test_upload: use object_id to check matches Eric Wong
2016-10-28 20:15 ` Eric Wong [this message]
2016-10-28 20:15 ` [PATCH 4/5] merge into kcar project and mailing list Eric Wong
2016-10-28 20:15 ` [PATCH 5/5] dedicated " Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://yhbt.net/http_spew/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20161028201503.10315-4-e@80x24.org \
--to=e@80x24.org \
--cc=http_spew-public@bogomips.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhbt.net/http_spew.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox