From 3f6e30e53ce8050375955322e170612e1de099b1 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 25 Jan 2018 11:21:47 +0000 Subject: [PATCH 1/2] openssl/buffering.rb: no RS when output * ext/openssl/lib/openssl/buffering.rb (do_write, puts): output methods should not be affected by the input record separator. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62038 b2dd03c8-39d4-4d8f-98ff-823fe69b080e Sync-with-trunk: r62038 --- lib/openssl/buffering.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/openssl/buffering.rb b/lib/openssl/buffering.rb index 935f61f0e..1f2b2a7e4 100644 --- a/lib/openssl/buffering.rb +++ b/lib/openssl/buffering.rb @@ -316,8 +316,8 @@ def do_write(s) @wbuffer << s @wbuffer.force_encoding(Encoding::BINARY) @sync ||= false - if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex($/) - remain = idx ? idx + $/.size : @wbuffer.length + if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex("\n") + remain = idx ? idx + 1 : @wbuffer.size nwritten = 0 while remain > 0 str = @wbuffer[nwritten,remain] @@ -409,9 +409,7 @@ def puts(*args) end args.each{|arg| s << arg.to_s - if $/ && /\n\z/ !~ s - s << "\n" - end + s.sub!(/(? Date: Sat, 4 Aug 2018 09:50:42 +0200 Subject: [PATCH 2/2] Reduce memory allocation when writing to SSLSocket At the moment OpenSSL::Buffering#do_write allocates some additional strings, and in my profiling writing 5MB of data allocates additional 7.7MB of strings. This patch greatly reduces memory allocations, and now writing 5MB of data allocates only additional 0.2MB of strings. This means that large file uploads would effectively not allocate additional memory anymore. Reference: https://bugs.ruby-lang.org/issues/14426 Reference: https://github.com/ruby/ruby/pull/1924 --- lib/openssl/buffering.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/openssl/buffering.rb b/lib/openssl/buffering.rb index 1f2b2a7e4..5d1586e59 100644 --- a/lib/openssl/buffering.rb +++ b/lib/openssl/buffering.rb @@ -316,20 +316,15 @@ def do_write(s) @wbuffer << s @wbuffer.force_encoding(Encoding::BINARY) @sync ||= false - if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex("\n") - remain = idx ? idx + 1 : @wbuffer.size - nwritten = 0 - while remain > 0 - str = @wbuffer[nwritten,remain] + if @sync or @wbuffer.size > BLOCK_SIZE + until @wbuffer.empty? begin - nwrote = syswrite(str) + nwrote = syswrite(@wbuffer) rescue Errno::EAGAIN retry end - remain -= nwrote - nwritten += nwrote + @wbuffer[0, nwrote] = "" end - @wbuffer[0,nwritten] = "" end end