dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 2/2] webrick/httpresponse: IO.copy_stream for regular files
Date: Tue, 26 Dec 2017 04:39:36 +0000	[thread overview]
Message-ID: <20171226043936.7694-2-e@80x24.org> (raw)
In-Reply-To: <20171226043936.7694-1-e@80x24.org>

Remove the redundant _send_file method since its functionality
is unnecessary with IO.copy_stream.  IO.copy_stream also allows
the use of sendfile under some OSes to speed up copies to
non-TLS sockets.

Testing with "curl >/dev/null" and "ruby -run -e httpd" to
read a 1G file over Linux loopback reveals a reduction from
around ~0.770 to ~0.490 seconds on the client side.

* lib/webrick/httpresponse.rb (send_body_io): use IO.copy_stream
  (_send_file): remove
---
 lib/webrick/httpresponse.rb | 27 +++------------------------
 1 file changed, 3 insertions(+), 24 deletions(-)

diff --git a/lib/webrick/httpresponse.rb b/lib/webrick/httpresponse.rb
index d9e46bbf73..9f29a7c0b1 100644
--- a/lib/webrick/httpresponse.rb
+++ b/lib/webrick/httpresponse.rb
@@ -410,9 +410,9 @@ def send_body_io(socket)
           buf.clear
           socket.write("0#{CRLF}#{CRLF}")
         else
-          size = @header['content-length'].to_i
-          _send_file(socket, @body, 0, size)
-          @sent_size = size
+          size = @header['content-length']
+          size = size.to_i if size
+          @sent_size = IO.copy_stream(@body, socket, size)
         end
       ensure
         @body.close
@@ -474,27 +474,6 @@ def write(buf)
       alias :<< :write
     end
 
-    def _send_file(output, input, offset, size)
-      while offset > 0
-        sz = @buffer_size < size ? @buffer_size : size
-        buf = input.read(sz)
-        offset -= buf.bytesize
-      end
-
-      if size == 0
-        while buf = input.read(@buffer_size)
-          output.write(buf)
-        end
-      else
-        while size > 0
-          sz = @buffer_size < size ? @buffer_size : size
-          buf = input.read(sz)
-          output.write(buf)
-          size -= buf.bytesize
-        end
-      end
-    end
-
     # :startdoc:
   end
 
-- 
EW


      reply	other threads:[~2017-12-26  4:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-26  4:39 [PATCH 1/2] webrick: favor .write over << method Eric Wong
2017-12-26  4:39 ` 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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171226043936.7694-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.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.
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).