1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

webrick/httpresponse: minor cleanups to reduce memory use

I never knew "format" was a global method alias for "sprintf";
so it was confusing to me.  Normally, one would use "sprintf"
since it's also available in many other languages, but
Integer#to_s avoids parsing a format string so it's less
bug-prone.

Furthermore, favor string interpolation over String#<< since it
is easier for the VM to optimize memory allocation (as in r60320).
Interpolation also reduces method calls and memory overhead
for inline method cache.

Finally, ensure we clear all short-lived buffers for body
responses.  A similar change was made and measured for Net::*
in r58840 showing a large memory reduction on some workloads.

* webrick/httpresponse.rb (send_body_io): favor String#to_s,
  reduce method calls for String#<<,
  clear `buf' when done,
  avoid extra String#bytesize calls

* (send_body_string): ditto

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
normal 2017-10-31 01:37:37 +00:00
parent a09e175068
commit 3b92c63158

View file

@ -396,19 +396,18 @@ module WEBrick
if @request_method == "HEAD" if @request_method == "HEAD"
# do nothing # do nothing
elsif chunked? elsif chunked?
buf = ''
begin begin
buf = '' @body.readpartial(@buffer_size, buf)
data = '' size = buf.bytesize
while true data = "#{size.to_s(16)}#{CRLF}#{buf}#{CRLF}"
@body.readpartial( @buffer_size, buf ) # there is no need to clear buf? _write_data(socket, data)
data << format("%x", buf.bytesize) << CRLF data.clear
data << buf << CRLF @sent_size += size
_write_data(socket, data) rescue EOFError
data.clear break
@sent_size += buf.bytesize end while true
end buf.clear
rescue EOFError # do nothing
end
_write_data(socket, "0#{CRLF}#{CRLF}") _write_data(socket, "0#{CRLF}#{CRLF}")
else else
size = @header['content-length'].to_i size = @header['content-length'].to_i
@ -427,11 +426,11 @@ module WEBrick
body ? @body.bytesize : 0 body ? @body.bytesize : 0
while buf = @body[@sent_size, @buffer_size] while buf = @body[@sent_size, @buffer_size]
break if buf.empty? break if buf.empty?
data = "" size = buf.bytesize
data << format("%x", buf.bytesize) << CRLF data = "#{size.to_s(16)}#{CRLF}#{buf}#{CRLF}"
data << buf << CRLF buf.clear
_write_data(socket, data) _write_data(socket, data)
@sent_size += buf.bytesize @sent_size += size
end end
_write_data(socket, "0#{CRLF}#{CRLF}") _write_data(socket, "0#{CRLF}#{CRLF}")
else else