Skip to content

Commit

Permalink
Reduce memory allocation when writing to SSLSocket
Browse files Browse the repository at this point in the history
[ 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: ruby/ruby#1924
(cherry picked from commit 251b5be)
  • Loading branch information
janko authored and rhenium committed Sep 26, 2021
1 parent b5a4c78 commit acc8079
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/openssl/buffering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit acc8079

Please sign in to comment.