Issue #5138 has been updated by tenderlovemaking (Aaron Patterson).


=begin
Just another data point.  I ran the benchmarks that Charles showed on my implementation in MRI.  We also see a good speed improvement on MRI:

  $ ruby -rbenchmark -rsocket -e "sock = TCPSocket.new('google.com', 80); 10.times { puts Benchmark.measure { 100_000.times { begin; sock.read_nonblock(1000); rescue; end } } }"
    0.650000   0.110000   0.760000 (  0.769794)
    0.620000   0.110000   0.730000 (  0.721198)
    0.600000   0.090000   0.690000 (  0.701172)
    0.640000   0.110000   0.750000 (  0.738934)
    0.600000   0.100000   0.700000 (  0.702289)
    0.600000   0.090000   0.690000 (  0.694982)
    0.630000   0.110000   0.740000 (  0.737876)
    0.630000   0.100000   0.730000 (  0.729356)
    0.640000   0.110000   0.750000 (  0.743386)
    0.600000   0.100000   0.700000 (  0.705391)
  $ ruby -rbenchmark -rsocket -e "sock = TCPSocket.new('google.com', 80); 10.times { puts Benchmark.measure { 100_000.times { begin; sock.try_read_nonblock(1000); rescue; end } } }"
    0.160000   0.070000   0.230000 (  0.230004)
    0.150000   0.080000   0.230000 (  0.224267)
    0.170000   0.080000   0.250000 (  0.253598)
    0.150000   0.070000   0.220000 (  0.221024)
    0.150000   0.070000   0.220000 (  0.225998)
    0.160000   0.070000   0.230000 (  0.223979)
    0.150000   0.080000   0.230000 (  0.231132)
    0.160000   0.070000   0.230000 (  0.233416)
    0.160000   0.080000   0.240000 (  0.238810)
    0.150000   0.070000   0.220000 (  0.222216)

=end

----------------------------------------
Feature #5138: Add nonblocking IO that does not use exceptions for EOF and EWOULDBLOCK
https://bugs.ruby-lang.org/issues/5138#change-40391

Author: wycats (Yehuda Katz)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


The current Ruby I/O classes have non-blocking methods (read_nonblock and write_nonblock). These methods will never block, and if they would block, they raise an exception instead (IO::WaitReadable or IO::WaitWritable). In addition, if the IO is at EOF, they raise an EOFError.

These exceptions are raised repeatedly in virtually every use of the non-blocking methods. This patch adds a pair of methods (try_read_nonblock and try_write_nonblock) that have the same semantics as the existing methods, but they return Symbols instead of raising exceptions for these routine cases:

* :read_would_block
* :write_would_block
* :eof

The patch contains updates for IO, StringIO, and OpenSSL. The updates are fully documented and tested.


-- 
http://bugs.ruby-lang.org/