Skip to content

Commit 98b0309

Browse files
Use IO#read instead of manual readfull loop
Replace the manual `readfull` loop with Ruby's built-in `IO#read` method. Modern Ruby (3.1+) handles non-blocking reads internally with proper timeout support via `IO#timeout=`, making the manual loop unnecessary. This provides ~7% performance improvement on read operations based on benchmarks from PR #1026. The `readfull` method is preserved in socket.rb for backwards compatibility but is no longer used internally. Based on work by @grcooper in PR #1026. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent b6532d0 commit 98b0309

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Performance:
99
- Buffered I/O: Use `socket.sync = false` with explicit flush to reduce syscalls for pipelined operations
1010
- get_multi optimizations: Use Set for O(1) server tracking lookups
1111
- Raw mode optimization: Skip bitflags request in meta protocol when in raw mode (saves 2 bytes per request)
12+
- Use Ruby's built-in `IO#read` instead of manual `readfull` loop for improved performance on read operations (grcooper)
1213

1314
New Features:
1415

lib/dalli/protocol/connection_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def read_line
156156
end
157157

158158
def read(count)
159-
@sock.readfull(count)
159+
@sock.read(count)
160160
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
161161
error_on_request!(e)
162162
end

test/integration/test_network.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@
198198

199199
ssl_error = OpenSSL::SSL::SSLError.new('SSL_read: unexpected eof while reading')
200200

201-
# Binary protocol uses readfull for reading, meta protocol uses gets
202-
stub_method = p == :binary ? :readfull : :gets
201+
# Binary protocol uses read for reading, meta protocol uses gets
202+
stub_method = p == :binary ? :read : :gets
203203
server.sock.stub(stub_method, proc { raise ssl_error }) do
204204
# The operation will retry with a new connection and may succeed
205205
# What matters is the SSLError is caught, not propagated

0 commit comments

Comments
 (0)