yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
* [PATCH 0/3] favor newer Rubies for performance
@ 2015-06-30  3:01 Eric Wong
  2015-06-30  3:01 ` [PATCH 1/3] generate response status strings dynamically Eric Wong
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eric Wong @ 2015-06-30  3:01 UTC (permalink / raw)
  To: yahns-public; +Cc: e

* [PATCH 1/3] generate response status strings dynamically

  This unfortunately introduces a minor performance regression.
  However, it is unlikely to be noticeable in real-world apps.

* [PATCH 2/3] reduce constants and optimize for Ruby 2.2+

  This appears to recover the performance lost in 1/3 when doing
  informal benchmarks on a private app (persistent connections
  enabled).

* [PATCH 3/3] http_response: reduce bytecode size

  This generates less bytecode when checking with
  RubyVM::InstructionSequence.compile

 lib/yahns/http_client.rb         | 16 +++++-----------
 lib/yahns/http_response.rb       | 35 ++++++++++++++++-------------------
 lib/yahns/max_body.rb            | 12 ++++--------
 lib/yahns/proxy_http_response.rb | 16 +++++++++-------
 4 files changed, 34 insertions(+), 45 deletions(-)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] generate response status strings dynamically
  2015-06-30  3:01 [PATCH 0/3] favor newer Rubies for performance Eric Wong
@ 2015-06-30  3:01 ` Eric Wong
  2015-06-30  3:01 ` [PATCH 2/3] reduce constants and optimize for Ruby 2.2+ Eric Wong
  2015-06-30  3:01 ` [PATCH 3/3] http_response: reduce bytecode size Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2015-06-30  3:01 UTC (permalink / raw)
  To: yahns-public; +Cc: e

Rack::Utils::HTTP_STATUS_CODES may be altered by the underlying
application, allow changes to that to be reflected in our responses
and do not rely on the Unicorn::HttpResponse::CODES hash which
will probably go away soon.
---
 lib/yahns/http_response.rb       |  8 +++++---
 lib/yahns/proxy_http_response.rb | 11 ++++++-----
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb
index fabd4b7..b70491d 100644
--- a/lib/yahns/http_response.rb
+++ b/lib/yahns/http_response.rb
@@ -59,7 +59,7 @@ module Yahns::HttpResponse # :nodoc:
   end
 
   def err_response(code)
-    "#{response_start}#{CODES[code]}\r\n\r\n"
+    "#{response_start}#{code} #{Rack::Utils::HTTP_STATUS_CODES[code]}\r\n\r\n"
   end
 
   def response_header_blocked(ret, header, body, alive, offset, count)
@@ -130,7 +130,6 @@ module Yahns::HttpResponse # :nodoc:
   # writes the rack_response to socket as an HTTP response
   # returns :wait_readable, :wait_writable, :forget, or nil
   def http_response_write(status, headers, body)
-    status = CODES[status.to_i] || status
     offset = 0
     count = hijack = nil
     k = self.class
@@ -138,7 +137,10 @@ module Yahns::HttpResponse # :nodoc:
     flags = MSG_DONTWAIT
 
     if @hs.headers?
-      buf = "#{response_start}#{status}\r\nDate: #{httpdate}\r\n"
+      code = status.to_i
+      msg = Rack::Utils::HTTP_STATUS_CODES[code]
+      buf = "#{response_start}#{msg ? %Q(#{code} #{msg}) : status}\r\n" \
+            "Date: #{httpdate}\r\n"
       headers.each do |key, value|
         case key
         when %r{\ADate\z}i
diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index cbdce6f..61989c2 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -38,7 +38,8 @@ module Yahns::HttpResponse # :nodoc:
     end
     # try to write something, but don't care if we fail
     Integer === code and
-      kgio_trywrite("HTTP/1.1 #{CODES[code]}\r\n\r\n") rescue nil
+      kgio_trywrite("HTTP/1.1 #{code} #{
+                     Rack::Utils::HTTP_STATUS_CODES[code]}\r\n\r\n") rescue nil
 
     shutdown rescue nil
     req_res.shutdown rescue nil
@@ -59,16 +60,16 @@ module Yahns::HttpResponse # :nodoc:
   # returns nil if completely done
   def proxy_response_start(res, tip, kcar, req_res)
     status, headers = res
-    si = status.to_i
-    status = CODES[si] || status
+    code = status.to_i
+    msg = Rack::Utils::HTTP_STATUS_CODES[code]
     env = @hs.env
-    have_body = !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(si) &&
+    have_body = !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(code) &&
                 env[REQUEST_METHOD] != HEAD
     flags = MSG_DONTWAIT
     alive = @hs.next? && self.class.persistent_connections
     response_headers = env['yahns.proxy_pass.response_headers']
 
-    res = "HTTP/1.1 #{status}\r\n"
+    res = "HTTP/1.1 #{msg ? %Q(#{code} #{msg}) : status}\r\n"
     headers.each do |key,value| # n.b.: headers is an Array of 2-element Arrays
       case key
       when /\A(?:Connection|Keep-Alive)\z/i
-- 
EW


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] reduce constants and optimize for Ruby 2.2+
  2015-06-30  3:01 [PATCH 0/3] favor newer Rubies for performance Eric Wong
  2015-06-30  3:01 ` [PATCH 1/3] generate response status strings dynamically Eric Wong
@ 2015-06-30  3:01 ` Eric Wong
  2015-06-30  3:01 ` [PATCH 3/3] http_response: reduce bytecode size Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2015-06-30  3:01 UTC (permalink / raw)
  To: yahns-public; +Cc: e

Ruby (MRI) 2.1 optimizes allocations away on String#freeze with
literal strings.

Furthermore, Ruby 2.2 optimizes away literal string allocations
when they are used as arguments to Hash#[] and Hash#[]=

Thus we can avoid expensive constant lookups and cache overhead
by taking advantage of advancements in Ruby.

Since Ruby 2.2 has been out for 7 months, now; it ought to be safe
to introduce minor performance regressions for folks using older
Rubies (1.9.3+ remains supported) to benefit folks on the latest
Ruby.
---
 lib/yahns/http_client.rb         | 16 +++++-----------
 lib/yahns/http_response.rb       | 15 +++++----------
 lib/yahns/max_body.rb            | 12 ++++--------
 lib/yahns/proxy_http_response.rb |  5 +++--
 4 files changed, 17 insertions(+), 31 deletions(-)

diff --git a/lib/yahns/http_client.rb b/lib/yahns/http_client.rb
index 0c656e8..9a37811 100644
--- a/lib/yahns/http_client.rb
+++ b/lib/yahns/http_client.rb
@@ -11,12 +11,6 @@ class Yahns::HttpClient < Kgio::Socket # :nodoc:
   include Yahns::HttpResponse
   QEV_FLAGS = Yahns::Queue::QEV_RD # used by acceptor
 
-  # A frozen format for this is about 15% faster (note from Mongrel)
-  REMOTE_ADDR = 'REMOTE_ADDR'.freeze
-  RACK_INPUT = 'rack.input'.freeze
-  RACK_HIJACK = 'rack.hijack'.freeze
-  RACK_HIJACK_IO = "rack.hijack_io".freeze
-
   # called from acceptor thread
   def yahns_init
     @hs = Unicorn::HttpRequest.new
@@ -206,9 +200,9 @@ class Yahns::HttpClient < Kgio::Socket # :nodoc:
     # input is nil if we needed to wait for writability with
     # check_client_connection
     if input
-      env[REMOTE_ADDR] = @kgio_addr
-      env[RACK_HIJACK] = self
-      env[RACK_INPUT] = input
+      env['REMOTE_ADDR'] = @kgio_addr
+      env['rack.hijack'] = self
+      env['rack.input'] = input
 
       if k.check_client_connection && @hs.headers?
         rv = do_ccc and return rv
@@ -262,7 +256,7 @@ class Yahns::HttpClient < Kgio::Socket # :nodoc:
   # this is the env["rack.hijack"] callback exposed to the Rack app
   def call
     hijack_cleanup
-    @hs.env[RACK_HIJACK_IO] = self
+    @hs.env['rack.hijack_io'] = self
   end
 
   def response_hijacked(fn)
@@ -300,7 +294,7 @@ class Yahns::HttpClient < Kgio::Socket # :nodoc:
   end
 
   def app_hijacked?(env, body)
-    return false unless env.include?(RACK_HIJACK_IO)
+    return false unless env.include?('rack.hijack_io'.freeze)
     body.close if body.respond_to?(:close)
     true
   end
diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb
index b70491d..f50c9a1 100644
--- a/lib/yahns/http_response.rb
+++ b/lib/yahns/http_response.rb
@@ -23,13 +23,7 @@ module Yahns::HttpResponse # :nodoc:
   end
 
   # avoid GC overhead for frequently used-strings:
-  CONN_KA = "Connection: keep-alive\r\n\r\n"
-  CONN_CLOSE = "Connection: close\r\n\r\n"
-  Z = ""
   CCC_RESPONSE_START = [ 'HTTP', '/1.1 ' ]
-  RESPONSE_START = CCC_RESPONSE_START.join
-  REQUEST_METHOD = "REQUEST_METHOD"
-  HEAD = "HEAD"
 
   # no point in using one without the other, these have been in Linux
   # for ages
@@ -46,7 +40,7 @@ module Yahns::HttpResponse # :nodoc:
   end
 
   def response_start
-    @hs.response_start_sent ? Z : RESPONSE_START
+    @hs.response_start_sent ? ''.freeze : 'HTTP/1.1 '.freeze
   end
 
   def response_wait_write(rv)
@@ -115,7 +109,7 @@ module Yahns::HttpResponse # :nodoc:
   end
 
   def kv_str(key, value)
-    if value =~ /\n/
+    if value.include?("\n".freeze)
       # avoiding blank, key-only cookies with /\n+/
       value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" }.join
     else
@@ -124,7 +118,7 @@ module Yahns::HttpResponse # :nodoc:
   end
 
   def have_more?(value)
-    value.to_i > 0 && @hs.env[REQUEST_METHOD] != HEAD
+    value.to_i > 0 && @hs.env['REQUEST_METHOD'] != 'HEAD'.freeze
   end
 
   # writes the rack_response to socket as an HTTP response
@@ -163,7 +157,8 @@ module Yahns::HttpResponse # :nodoc:
           buf << kv_str(key, value)
         end
       end
-      buf << (alive ? CONN_KA : CONN_CLOSE)
+      buf << (alive ? "Connection: keep-alive\r\n\r\n".freeze
+                    : "Connection: close\r\n\r\n".freeze)
       case rv = kgio_syssend(buf, flags)
       when nil # all done, likely
         break
diff --git a/lib/yahns/max_body.rb b/lib/yahns/max_body.rb
index fadbddc..e52a10f 100644
--- a/lib/yahns/max_body.rb
+++ b/lib/yahns/max_body.rb
@@ -28,17 +28,13 @@ class Yahns::MaxBody # :nodoc:
     @limit = limit
   end
 
-  RACK_INPUT = "rack.input".freeze # :nodoc:
-  CONTENT_LENGTH = "CONTENT_LENGTH" # :nodoc:
-  HTTP_TRANSFER_ENCODING = "HTTP_TRANSFER_ENCODING" # :nodoc:
-
   # our main Rack middleware endpoint
   def call(env) # :nodoc:
     catch(:yahns_EFBIG) do
-      len = env[CONTENT_LENGTH]
+      len = env['CONTENT_LENGTH']
       if len && len.to_i > @limit
         return err
-      elsif /\Achunked\z/i =~ env[HTTP_TRANSFER_ENCODING]
+      elsif /\Achunked\z/i =~ env['HTTP_TRANSFER_ENCODING']
         limit_input!(env)
       end
       @app.call(env)
@@ -51,9 +47,9 @@ class Yahns::MaxBody # :nodoc:
   end
 
   def limit_input!(env) # :nodoc:
-    input = env[RACK_INPUT]
+    input = env['rack.input']
     klass = input.respond_to?(:rewind) ? RewindableWrapper : Wrapper
-    env[RACK_INPUT] = klass.new(input, @limit)
+    env['rack.input'] = klass.new(input, @limit)
   end
 end
 require_relative 'max_body/wrapper'
diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index 61989c2..50c841d 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -64,7 +64,7 @@ module Yahns::HttpResponse # :nodoc:
     msg = Rack::Utils::HTTP_STATUS_CODES[code]
     env = @hs.env
     have_body = !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(code) &&
-                env[REQUEST_METHOD] != HEAD
+                env['REQUEST_METHOD'] != 'HEAD'.freeze
     flags = MSG_DONTWAIT
     alive = @hs.next? && self.class.persistent_connections
     response_headers = env['yahns.proxy_pass.response_headers']
@@ -91,7 +91,8 @@ module Yahns::HttpResponse # :nodoc:
 
     # For now, do not add a Date: header, assume upstream already did it
     # but do not care if they did not
-    res << (alive ? CONN_KA : CONN_CLOSE)
+    res << (alive ? "Connection: keep-alive\r\n\r\n"
+                  : "Connection: close\r\n\r\n")
 
     # send the headers
     case rv = kgio_syssend(res, flags)
-- 
EW


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] http_response: reduce bytecode size
  2015-06-30  3:01 [PATCH 0/3] favor newer Rubies for performance Eric Wong
  2015-06-30  3:01 ` [PATCH 1/3] generate response status strings dynamically Eric Wong
  2015-06-30  3:01 ` [PATCH 2/3] reduce constants and optimize for Ruby 2.2+ Eric Wong
@ 2015-06-30  3:01 ` Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2015-06-30  3:01 UTC (permalink / raw)
  To: yahns-public; +Cc: e

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-06-30  3:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-30  3:01 [PATCH 0/3] favor newer Rubies for performance Eric Wong
2015-06-30  3:01 ` [PATCH 1/3] generate response status strings dynamically Eric Wong
2015-06-30  3:01 ` [PATCH 2/3] reduce constants and optimize for Ruby 2.2+ Eric Wong
2015-06-30  3:01 ` [PATCH 3/3] http_response: reduce bytecode size Eric Wong

Code repositories for project(s) associated with this inbox:

	../../../yahns.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).