aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJanko Marohnić <janko.marohnic@gmail.com>2018-08-04 09:50:42 +0200
committerKazuki Yamaguchi <k@rhe.jp>2021-09-26 19:15:37 +0900
commitacc8079b4a6b88d3376a8ed941a18d3dfc556cc5 (patch)
treebd64d82f02434fefe9e691d63d3bc0d4a32145db
parentb5a4c78afb536eff5b906e066fad5ec57dfb85a8 (diff)
downloadruby-openssl-acc8079b4a6b88d3376a8ed941a18d3dfc556cc5.tar.gz
Reduce memory allocation when writing to SSLSocket
[ This is a backport to the 2.1 branch. ] 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 (cherry picked from commit 251b5be20d5b58c27490f44cdeb6e655f9be6f19)
-rw-r--r--lib/openssl/buffering.rb13
1 files changed, 4 insertions, 9 deletions
diff --git a/lib/openssl/buffering.rb b/lib/openssl/buffering.rb
index 1f2b2a7e..5d1586e5 100644
--- a/lib/openssl/buffering.rb
+++ b/lib/openssl/buffering.rb
@@ -316,20 +316,15 @@ module OpenSSL::Buffering
@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