Skip to content

Commit fb9f443

Browse files
authored
Directly use IO#write from Buffered#syswrite. (#5)
1 parent 5267a9b commit fb9f443

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

lib/io/stream/buffered.rb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,26 @@ def sysclose
106106
end
107107
end
108108

109-
def syswrite(buffer)
110-
# This fails due to re-entrancy issues with a concurrent call to `sysclose`.
111-
# return @io.write(buffer)
112-
113-
while true
114-
result = @io.write_nonblock(buffer, exception: false)
115-
116-
case result
117-
when :wait_readable
118-
@io.wait_readable(@io.timeout) or raise ::IO::TimeoutError, "read timeout"
119-
when :wait_writable
120-
@io.wait_writable(@io.timeout) or raise ::IO::TimeoutError, "write timeout"
121-
else
122-
if result == buffer.bytesize
123-
return
109+
if RUBY_VERSION >= "3.3"
110+
def syswrite(buffer)
111+
return @io.write(buffer)
112+
end
113+
else
114+
def syswrite(buffer)
115+
while true
116+
result = @io.write_nonblock(buffer, exception: false)
117+
118+
case result
119+
when :wait_readable
120+
@io.wait_readable(@io.timeout) or raise ::IO::TimeoutError, "read timeout"
121+
when :wait_writable
122+
@io.wait_writable(@io.timeout) or raise ::IO::TimeoutError, "write timeout"
124123
else
125-
buffer = buffer.byteslice(result, buffer.bytesize)
124+
if result == buffer.bytesize
125+
return
126+
else
127+
buffer = buffer.byteslice(result, buffer.bytesize)
128+
end
126129
end
127130
end
128131
end

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- On Ruby v3.3+, use `IO#write` directly instead of `IO#write_nonblock`, for better performance.
6+
37
## v0.7.0
48

59
- Split stream functionality into separate `Readable` and `Writable` modules for better modularity and composition.

0 commit comments

Comments
 (0)