From: Eric Wong <e@80x24.org>
To: unicorn-public@bogomips.org
Subject: [PATCH 2/2] reduce and localize constant string use
Date: Mon, 9 Feb 2015 09:12:10 +0000 [thread overview]
Message-ID: <1423473130-14754-2-git-send-email-e@80x24.org> (raw)
In-Reply-To: <1423473130-14754-1-git-send-email-e@80x24.org>
Literal String#freeze avoids allocations since Ruby 2.1 via the
opt_str_freeze instruction, so we can start relying on it in
some places as Ruby 2.1 adoption increases. The 100-continue
handling is a good place to start since it is an uncommonly-used
code path which benefits from size reduction and the negative
performance impact is restricted to a handful of users.
HTTP_RESPONSE_START can safely live in http_request.rb as its
usage does not cross namespace boundaries
The goal is to eventually eliminate Unicorn::Const entirely.
---
lib/unicorn/const.rb | 17 +----------------
lib/unicorn/http_request.rb | 5 ++++-
lib/unicorn/http_server.rb | 18 ++++++++++--------
3 files changed, 15 insertions(+), 25 deletions(-)
diff --git a/lib/unicorn/const.rb b/lib/unicorn/const.rb
index e24b511..33ab4ac 100644
--- a/lib/unicorn/const.rb
+++ b/lib/unicorn/const.rb
@@ -1,12 +1,6 @@
# -*- encoding: binary -*-
-# :enddoc:
-# Frequently used constants when constructing requests or responses.
-# Many times the constant just refers to a string with the same
-# contents. Using these constants gave about a 3% to 10% performance
-# improvement over using the strings directly. Symbols did not really
-# improve things much compared to constants.
-module Unicorn::Const
+module Unicorn::Const # :nodoc:
# default TCP listen host address (0.0.0.0, all interfaces)
DEFAULT_HOST = "0.0.0.0"
@@ -23,14 +17,5 @@ module Unicorn::Const
# temporary file for reading (112 kilobytes). This is the default
# value of client_body_buffer_size.
MAX_BODY = 1024 * 112
-
- # :stopdoc:
- EXPECT_100_RESPONSE = "HTTP/1.1 100 Continue\r\n\r\n"
- EXPECT_100_RESPONSE_SUFFIXED = "100 Continue\r\n\r\nHTTP/1.1 "
-
- HTTP_RESPONSE_START = ['HTTP', '/1.1 ']
- HTTP_EXPECT = "HTTP_EXPECT"
-
- # :startdoc:
end
require_relative 'version'
diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb
index 6b20431..9888430 100644
--- a/lib/unicorn/http_request.rb
+++ b/lib/unicorn/http_request.rb
@@ -26,8 +26,11 @@ class Unicorn::HttpParser
# :stopdoc:
# A frozen format for this is about 15% faster
+ # Drop these frozen strings when Ruby 2.2 becomes more prevalent,
+ # 2.2+ optimizes hash assignments when used with literal string keys
REMOTE_ADDR = 'REMOTE_ADDR'.freeze
RACK_INPUT = 'rack.input'.freeze
+ HTTP_RESPONSE_START = [ 'HTTP', '/1.1 ']
@@input_class = Unicorn::TeeInput
@@check_client_connection = false
@@ -86,7 +89,7 @@ class Unicorn::HttpParser
# detect if the socket is valid by writing a partial response:
if @@check_client_connection && headers?
@response_start_sent = true
- Unicorn::Const::HTTP_RESPONSE_START.each { |c| socket.write(c) }
+ HTTP_RESPONSE_START.each { |c| socket.write(c) }
end
e[RACK_INPUT] = 0 == content_length ?
diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index c44a71e..f0216d0 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -546,12 +546,15 @@ class Unicorn::HttpServer
rescue
end
- def expect_100_response
- if @request.response_start_sent
- Unicorn::Const::EXPECT_100_RESPONSE_SUFFIXED
- else
- Unicorn::Const::EXPECT_100_RESPONSE
- end
+ def e100_response_write(client, env)
+ # We use String#freeze to avoid allocations under Ruby 2.1+
+ # Not many users hit this code path, so it's better to reduce the
+ # constant table sizes even for 1.9.3-2.0 users who'll hit extra
+ # allocations here.
+ client.write(@request.response_start_sent ?
+ "100 Continue\r\n\r\nHTTP/1.1 ".freeze :
+ "HTTP/1.1 100 Continue\r\n\r\n".freeze)
+ env.delete('HTTP_EXPECT'.freeze)
end
# once a client is accepted, it is processed in its entirety here
@@ -561,8 +564,7 @@ class Unicorn::HttpServer
return if @request.hijacked?
if 100 == status.to_i
- client.write(expect_100_response)
- env.delete(Unicorn::Const::HTTP_EXPECT)
+ e100_response_write(client, env)
status, headers, body = @app.call(env)
return if @request.hijacked?
end
--
EW
prev parent reply other threads:[~2015-02-09 9:12 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-09 9:12 [PATCH 1/2] const: drop constants used by Rainbows! Eric Wong
2015-02-09 9:12 ` Eric Wong [this message]
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/unicorn/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1423473130-14754-2-git-send-email-e@80x24.org \
--to=e@80x24.org \
--cc=unicorn-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/unicorn.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).