ruby-changes:46723
From: normal <ko1@a...>
Date: Mon, 22 May 2017 08:52:13 +0900 (JST)
Subject: [ruby-changes:46723] normal:r58840 (trunk): lib/net/protocol: clear short-lived read buffer
normal 2017-05-22 08:52:08 +0900 (Mon, 22 May 2017) New Revision: 58840 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58840 Log: lib/net/protocol: clear short-lived read buffer Using a parallel Net::HTTP downloader, this reduced memory usage from around 120MB to 50MB on my 32-bit x86 system. * lib/net/protocol.rb (rbuf_fill): clear temporary buffer Test script I used: require 'net/http' require 'uri' require 'digest/sha1' url = 'http://80x24.org/git-i-forgot-to-pack/objects/pack/pack-97b25a76c03b489d4cbbd85b12d0e1ad28717e55.idx' uri = URI(url) use_ssl = "https" == uri.scheme thrs = 30.times.map do Thread.start do cur = Thread.current.object_id Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http| req = Net::HTTP::Get.new(uri) http.request(req) do |res| dig = Digest::SHA1.new res.read_body do |buf| dig.update(buf) #buf.clear # most Ruby programmers don't do this :< end warn "#{Time.now} #{cur} #{dig.hexdigest}\n" end end :done end end p thrs.map(&:value) Modified files: trunk/lib/net/protocol.rb Index: lib/net/protocol.rb =================================================================== --- lib/net/protocol.rb (revision 58839) +++ lib/net/protocol.rb (revision 58840) @@ -171,7 +171,9 @@ module Net # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/protocol.rb#L171 def rbuf_fill case rv = @io.read_nonblock(BUFSIZE, exception: false) when String - return @rbuf << rv + @rbuf << rv + rv.clear + return when :wait_readable @io.to_io.wait_readable(@read_timeout) or raise Net::ReadTimeout # continue looping -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/