From 24afd8ac3ea0375ec276a89055db58afc8c4f69c Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 19 Nov 2020 11:53:27 +0100 Subject: [PATCH] Optimize SocketMixin#write to avoid string copying --- lib/redis/connection/ruby.rb | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/lib/redis/connection/ruby.rb b/lib/redis/connection/ruby.rb index f2fbc2c05..7ef5ca9fa 100644 --- a/lib/redis/connection/ruby.rb +++ b/lib/redis/connection/ruby.rb @@ -67,10 +67,13 @@ def _read_from_socket(nbytes) end end - def _write_to_socket(data) + def write(buffer) + return super(buffer) unless @write_timeout + + bytes_to_write = buffer.bytesize total_bytes_written = 0 loop do - case bytes_written = write_nonblock(data, exception: false) + case bytes_written = write_nonblock(buffer, exception: false) when :wait_readable unless wait_readable(@write_timeout) raise Redis::TimeoutError @@ -83,28 +86,13 @@ def _write_to_socket(data) raise Errno::ECONNRESET when Integer total_bytes_written += bytes_written - if bytes_written < data.bytesize - data.slice!(0, bytes_written) - else + + if total_bytes_written >= bytes_to_write return total_bytes_written end - end - end - end - def write(data) - return super(data) unless @write_timeout - - data = data.b - length = data.bytesize - total_count = 0 - loop do - count = _write_to_socket(data) - - total_count += count - return total_count if total_count >= length - - data = data.byteslice(count..-1) + buffer = buffer.byteslice(bytes_written..-1) + end end end end