Project

General

Profile

Feature #14426

[PATCH] openssl: reduce memory allocation in OpenSSL::Buffering#do_write

Added by janko (Janko Marohnić) 18 days ago. Updated 17 days ago.

Status:
Assigned
Priority:
Normal
Target version:
-
[ruby-core:85298]

Description

When writing data to an SSLSocket, there are a lot of, in my opinion, unnecessary strings being allocated, concretely in OpenSSL::Buffering#do_write.

When the buffer would be written, it would always be copied into a new string first, regardless of whether the write was partial or not. And in case of partial writes, it's not necessary to create copies of remaining data, we could just use the String[from, length] = "" trick immediately which modifies the string in-place.

I also thought that splitting writes on newlines was adding unnecessary memory allocations, so I removed that.

I tested uploading a 5MB file using HTTP.rb, and memory allocation went from 7.7 MB to 0.2 MB with this change.

require "http"
require "memory_profiler"
require "stringio"

body = StringIO.new("a" * 5*1024*1024)

MemoryProfiler.report do
  HTTP.post("https://example.com", body: body)
end.pretty_print
openssl-memory-allocation.patch (913 Bytes) openssl-memory-allocation.patch janko (Janko Marohnić), 01/31/2018 12:35 PM

History

#1 [ruby-core:85315] Updated by normalperson (Eric Wong) 18 days ago

janko.marohnic@gmail.com wrote:

https://bugs.ruby-lang.org/issues/14426

I also thought that splitting writes on newlines was adding
unnecessary memory allocations, so I removed that.

Agreed.

I tested uploading a 5MB file using HTTP.rb, and memory
allocation went from 7.7 MB to 0.2 MB with this change.

Nice! This patch looks good to me, thanks.

#2 [ruby-core:85318] Updated by hsbt (Hiroshi SHIBATA) 17 days ago

  • Assignee set to rhenium (Kazuki Yamaguchi)
  • Status changed from Open to Assigned

Also available in: Atom PDF