From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: * X-Spam-ASN: AS62744 199.249.223.0/24 X-Spam-Status: No, score=1.2 required=3.0 tests=AWL,BAYES_00,RCVD_IN_MSPIKE_BL, RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,RDNS_NONE,SPF_FAIL,SPF_HELO_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (unknown [199.249.223.67]) by dcvr.yhbt.net (Postfix) with ESMTP id B47821FAE2 for ; Tue, 26 Dec 2017 04:39:42 +0000 (UTC) From: Eric Wong 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 Message-Id: <20171226043936.7694-2-e@80x24.org> In-Reply-To: <20171226043936.7694-1-e@80x24.org> References: <20171226043936.7694-1-e@80x24.org> List-Id: 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