Feature #4560

[PATCH] lib/net/protocol.rb: avoid exceptions in rbuf_fill

Added by Eric Wong about 3 years ago. Updated over 1 year ago.

[ruby-core:35636]
Status:Assigned
Priority:Low
Assignee:Akira Tanaka
Category:lib
Target version:next minor

Description

Blindly hitting IO#read_nonblock() and raising is expensive due
to two factors:

1) method cache being scanned/cleared when the IO::WaitReadable
extended class is GC-ed
2) backtrace generation

This reduces the likelyhood of an IO::WaitReadable exception,
but spurious wakeup can still occur due to bad TCP checksums.

This optimization only applies to non-OpenSSL sockets. I am
using IO#wait here instead of IO.select() since IO#wait is not
available on OpenSSL sockets.

0001-lib-net-protocol.rb-avoid-exceptions-in-rbuf_fill.patch Magnifier (1.3 KB) Eric Wong, 04/07/2011 11:48 AM


Related issues

Related to ruby-trunk - Feature #5138: Add nonblocking IO that does not use exceptions for EOF a... Closed 08/02/2011

History

#1 Updated by Charles Nutter about 3 years ago

=begin
This is an interesting one. JRuby recently changed how we generate backtraces to using the Java backtrace as the master. This means our backtraces are as expensive to generate as a full Java backtrace for the full stack (think generating a backtrace for all Ruby and C and intermediate calls in Ruby). As a result, any algorithms that generate backtraces as part of normal flow control took a big perf hit.

On JRuby master, I've made a change that does not generate backtraces for EAGAIN, to avoid the overhead of generating it for the expected case of readnonblock having nothing available. But it's a bit of a band-aid. The overhead from even creating an exception can be weigh into a tight loop over readnonblock when there's nothing available, and of course having the backtrace disabled could annoy someone if it leaked out (JRuby points them to a flag to turn the backtraces on). Not sure what's the best long-term solution.

Also, the 1.9 practice of mixing in WaitReadable is really dreadful. It's bad enough in JRuby that it has to construct a new singleton class for every raised exception, but the cache effects in 1.9 are really painful.
=end

#2 Updated by Eric Wong about 3 years ago

=begin
redmine@ruby-lang.org wrote:

On JRuby master, I've made a change that does not generate backtraces
for EAGAIN, to avoid the overhead of generating it for the expected
case of readnonblock having nothing available. But it's a bit of a
band-aid. The overhead from even creating an exception can be weigh
into a tight loop over read
nonblock when there's nothing available,
and of course having the backtrace disabled could annoy someone if it
leaked out (JRuby points them to a flag to turn the backtraces on).
Not sure what's the best long-term solution.

Perhaps something similar to the kgio[1] API with IO#tryread/trywrite
(that return :waitreadable/:waitwritable Symbols) can be moved into
the core Ruby IO class (and deprecate IO#readnonblock/writenonblock).

Also, the 1.9 practice of mixing in WaitReadable is really dreadful.
It's bad enough in JRuby that it has to construct a new singleton
class for every raised exception, but the cache effects in 1.9 are
really painful.

Yes, I've been trying to fix that in trunk, too:

[1] http://bogomips.org/kgio - *nix-only, but the API is fully RDoc-ed

--
Eric Wong
=end

#3 Updated by Yusuke Endoh about 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Akira Tanaka

#4 Updated by Akira Tanaka over 1 year ago

  • Target version changed from 2.0.0 to next minor

Also available in: Atom PDF