Skip to content

Commit 74aac51

Browse files
committed
[GR-17457] Improve performance of IO#read_nonblock when no data is available.
PullRequest: truffleruby/2635
2 parents 6167eaa + 0e68c4a commit 74aac51

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Performance:
3333
* Reduce the number of AST nodes created for methods and blocks (#2261).
3434
* Fiber-local variables are much faster now by using less synchronization.
3535
* Improved the performance of the exceptional case of `String#chr` (#2318, @chrisseaton).
36+
* Improved the performance of `IO#read_nonblock` when no data is available to be read.
3637

3738
Changes:
3839

src/main/ruby/truffleruby/core/io.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,11 +1654,14 @@ def nonblock(nonblock=true)
16541654

16551655
# Normally only provided by io/nonblock
16561656
def nonblock=(value)
1657-
if value
1658-
fcntl(F_SETFL, fcntl(F_GETFL) | NONBLOCK)
1659-
else
1660-
fcntl(F_SETFL, fcntl(F_GETFL) & ~NONBLOCK)
1661-
end
1657+
old_flags = fcntl(F_GETFL)
1658+
new_flags = if value
1659+
old_flags | NONBLOCK
1660+
else
1661+
old_flags & ~NONBLOCK
1662+
end
1663+
fcntl(F_SETFL, new_flags) unless old_flags == new_flags
1664+
self
16621665
end
16631666

16641667
##
@@ -1831,17 +1834,12 @@ def read_nonblock(size, buffer = nil, exception: true)
18311834
return @ibuffer.shift(size)
18321835
end
18331836

1834-
begin
1835-
str = Truffle::POSIX.read_string_nonblock(self, size)
1836-
rescue Errno::EAGAIN
1837-
if exception
1838-
raise EAGAINWaitReadable
1839-
else
1840-
return :wait_readable
1841-
end
1842-
end
1837+
str = Truffle::POSIX.read_string_nonblock(self, size, exception)
18431838

1844-
if str
1839+
case str
1840+
when Symbol
1841+
str
1842+
when String
18451843
buffer ? buffer.replace(str) : str
18461844
else # EOF
18471845
if exception

src/main/ruby/truffleruby/core/posix.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,14 @@ def self.read_string_at_least_one_byte(io, count)
366366

367367
# Used in IO#read_nonblock
368368

369-
def self.read_string_nonblock(io, count)
369+
def self.read_string_nonblock(io, count, exception)
370370
# must call #read_string in order to properly support polyglot STDIO.
371371
string, errno = read_string(io, count)
372372
if errno == 0
373373
string
374374
elsif TRY_AGAIN_ERRNOS.include? errno
375-
raise IO::EAGAINWaitReadable
375+
raise IO::EAGAINWaitReadable if exception
376+
:wait_readable
376377
else
377378
Errno.handle_errno(errno)
378379
end

0 commit comments

Comments
 (0)