aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJanko Marohnić <janko.marohnic@gmail.com>2018-08-04 09:50:42 +0200
committerKazuki Yamaguchi <k@rhe.jp>2018-08-08 19:08:59 +0900
commit251b5be20d5b58c27490f44cdeb6e655f9be6f19 (patch)
tree5f226c0c747ffa29a54e2b1bf4a5a6561195a0c3
parent3f6e30e53ce8050375955322e170612e1de099b1 (diff)
downloadruby-openssl-251b5be20d5b58c27490f44cdeb6e655f9be6f19.tar.gz
Reduce memory allocation when writing to SSLSocketjm/buffering-reduce-memory-allocation
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
-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