Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize SocketMixin#write to avoid string copying #964

Merged
merged 1 commit into from Nov 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 9 additions & 21 deletions lib/redis/connection/ruby.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down