Skip to content

Commit f7d354f

Browse files
authored
Merge pull request #964 from casperisfine/ruby-connection-shared-strings
Optimize SocketMixin#write to avoid string copying
2 parents 02b6ce5 + 24afd8a commit f7d354f

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

lib/redis/connection/ruby.rb

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ def _read_from_socket(nbytes)
6767
end
6868
end
6969

70-
def _write_to_socket(data)
70+
def write(buffer)
71+
return super(buffer) unless @write_timeout
72+
73+
bytes_to_write = buffer.bytesize
7174
total_bytes_written = 0
7275
loop do
73-
case bytes_written = write_nonblock(data, exception: false)
76+
case bytes_written = write_nonblock(buffer, exception: false)
7477
when :wait_readable
7578
unless wait_readable(@write_timeout)
7679
raise Redis::TimeoutError
@@ -83,28 +86,13 @@ def _write_to_socket(data)
8386
raise Errno::ECONNRESET
8487
when Integer
8588
total_bytes_written += bytes_written
86-
if bytes_written < data.bytesize
87-
data.slice!(0, bytes_written)
88-
else
89+
90+
if total_bytes_written >= bytes_to_write
8991
return total_bytes_written
9092
end
91-
end
92-
end
93-
end
9493

95-
def write(data)
96-
return super(data) unless @write_timeout
97-
98-
data = data.b
99-
length = data.bytesize
100-
total_count = 0
101-
loop do
102-
count = _write_to_socket(data)
103-
104-
total_count += count
105-
return total_count if total_count >= length
106-
107-
data = data.byteslice(count..-1)
94+
buffer = buffer.byteslice(bytes_written..-1)
95+
end
10896
end
10997
end
11098
end

0 commit comments

Comments
 (0)