From: "headius (Charles Nutter)" Date: 2013-04-24T06:19:58+09:00 Subject: [ruby-core:54533] [ruby-trunk - Feature #5138] Add nonblocking IO that does not use exceptions for EOF and EWOULDBLOCK Issue #5138 has been updated by headius (Charles Nutter). JRuby master (1.7.4) now has a new ext io/try_nonblock that implements just the IO portion of wycats's patch (I did not implement the StringIO and SSLSocket logic). Numbers for each of the three modes (errnos with backtraces (slow due to JVM), errnos without backtraces, and returning symbol): $ jruby -Xerrno.backtrace=true -rbenchmark -rsocket -e "sock = TCPSocket.new('google.com', 80); 10.times { puts Benchmark.measure { 100_000.times { begin; sock.read_nonblock(1000); rescue; end } } }" 12.610000 0.380000 12.990000 ( 11.157000) 9.290000 0.290000 9.580000 ( 9.477000) 9.350000 0.300000 9.650000 ( 9.464000) 9.260000 0.290000 9.550000 ( 9.335000) 9.320000 0.300000 9.620000 ( 9.341000) 9.140000 0.290000 9.430000 ( 9.218000) 9.150000 0.300000 9.450000 ( 9.235000) 9.730000 0.330000 10.060000 ( 9.794000) 9.210000 0.290000 9.500000 ( 9.275000) 9.340000 0.300000 9.640000 ( 9.393000) $ jruby -rbenchmark -rsocket -e "sock = TCPSocket.new('google.com', 80); 10.times { puts Benchmark.measure { 100_000.times { begin; sock.read_nonblock(1000); rescue; end } } }" 3.110000 0.310000 3.420000 ( 2.065000) 0.690000 0.230000 0.920000 ( 0.800000) 0.670000 0.200000 0.870000 ( 0.803000) 0.610000 0.220000 0.830000 ( 0.798000) 0.630000 0.210000 0.840000 ( 0.825000) 0.620000 0.210000 0.830000 ( 0.816000) 0.610000 0.200000 0.810000 ( 0.809000) 0.620000 0.210000 0.830000 ( 0.811000) 0.620000 0.210000 0.830000 ( 0.809000) 0.620000 0.210000 0.830000 ( 0.819000) $ jruby -rbenchmark -rsocket -rio/try_nonblock -e "sock = TCPSocket.new('google.com', 80); 10.times { puts Benchmark.measure { 100_000.times { sock.try_read_nonblock(1000) } } }" 1.150000 0.220000 1.370000 ( 0.846000) 0.320000 0.160000 0.480000 ( 0.342000) 0.190000 0.180000 0.370000 ( 0.346000) 0.220000 0.170000 0.390000 ( 0.341000) 0.210000 0.160000 0.370000 ( 0.319000) 0.210000 0.160000 0.370000 ( 0.322000) 0.170000 0.160000 0.330000 ( 0.319000) 0.160000 0.150000 0.310000 ( 0.308000) 0.150000 0.150000 0.300000 ( 0.309000) 0.170000 0.160000 0.330000 ( 0.323000) ---------------------------------------- Feature #5138: Add nonblocking IO that does not use exceptions for EOF and EWOULDBLOCK https://bugs.ruby-lang.org/issues/5138#change-38847 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/