about summary refs log tree commit homepage
path: root/lib/yahns/chunk_body.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-08-03 02:57:15 +0000
committerEric Wong <e@80x24.org>2016-08-03 02:59:25 +0000
commitf1f430323bcab90bdbbe980d6ef5b60602796a55 (patch)
treef242391bf9bedc59434915cb063ea6bcd00f6dfe /lib/yahns/chunk_body.rb
parentf770e5ba01bfcbc3c9f5c3eb12b28abdf6849f15 (diff)
downloadyahns-f1f430323bcab90bdbbe980d6ef5b60602796a55.tar.gz
We might as well do it since puma and thin both do(*),
and we can still do writev for now to get some speedups
by avoiding Rack::Chunked overhead.

timing runs of "curl --no-buffer http://127.0.0.1:9292/ >/dev/null"
results in a best case drop from ~260ms to ~205ms on one VM
by disabling Rack::Chunked in the below config.ru

$ ruby -I lib bin/yahns-rackup -E none config.ru

==> config.ru <==
class Body
  STR = ' ' * 1024 * 16
  def each
    10000.times { yield STR }
  end
end

use Rack::Chunked if ENV['RACK_CHUNKED']
run(lambda do |env|
  [ 200, [ %w(Content-Type text/plain) ], Body.new ]
end)

(*) they can do Content-Length, but I don't think it's
    worth the effort at the server level.
Diffstat (limited to 'lib/yahns/chunk_body.rb')
-rw-r--r--lib/yahns/chunk_body.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/yahns/chunk_body.rb b/lib/yahns/chunk_body.rb
new file mode 100644
index 0000000..6e56a18
--- /dev/null
+++ b/lib/yahns/chunk_body.rb
@@ -0,0 +1,27 @@
+# -*- encoding: binary -*-
+# Copyright (C) 2016 all contributors <yahns-public@yhbt.net>
+# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
+# frozen_string_literal: true
+class Yahns::ChunkBody
+  def initialize(body, vec)
+    @body = body
+    @vec = vec
+  end
+
+  def each
+    vec = @vec
+    vec[2] = "\r\n".freeze
+    @body.each do |chunk|
+      vec[0] = "#{chunk.bytesize.to_s(16)}\r\n"
+      vec[1] = chunk
+      # vec[2] never changes: "\r\n" above
+      yield vec
+    end
+    vec.clear
+    yield "0\r\n\r\n".freeze
+  end
+
+  def close
+    @body.close if @body.respond_to?(:close)
+  end
+end