Skip to content

Commit 251b5be

Browse files
jankorhenium
authored andcommitted
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: ruby/ruby#1924
1 parent 3f6e30e commit 251b5be

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

lib/openssl/buffering.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,20 +316,15 @@ def do_write(s)
316316
@wbuffer << s
317317
@wbuffer.force_encoding(Encoding::BINARY)
318318
@sync ||= false
319-
if @sync or @wbuffer.size > BLOCK_SIZE or idx = @wbuffer.rindex("\n")
320-
remain = idx ? idx + 1 : @wbuffer.size
321-
nwritten = 0
322-
while remain > 0
323-
str = @wbuffer[nwritten,remain]
319+
if @sync or @wbuffer.size > BLOCK_SIZE
320+
until @wbuffer.empty?
324321
begin
325-
nwrote = syswrite(str)
322+
nwrote = syswrite(@wbuffer)
326323
rescue Errno::EAGAIN
327324
retry
328325
end
329-
remain -= nwrote
330-
nwritten += nwrote
326+
@wbuffer[0, nwrote] = ""
331327
end
332-
@wbuffer[0,nwritten] = ""
333328
end
334329
end
335330

0 commit comments

Comments
 (0)