Project

General

Profile

Feature #11024

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

This is for consistency with accept_nonblock arguments and gives a 
 minor speedup from avoiding exceptions: 

 Results: 

                  |        user |      system |       total |         real 
 -----------------|----------:|----------:|----------:|-----------: 
 

 default            |                 0.050000 |        0.100000 |        0.150000 |( (    0.151307) 
 exception: false |        0.030000 |        0.080000 |        0.110000 |( (    0.108840) 

 --------------------------------------------------- ----------------------------8<----------------------- 
 ~~~ruby 
 require 'socket' 
 require 'benchmark' 
 require 'io/wait' 
 require 'tmpdir' 

 host = "127.#{rand 255}.#{rand 255}.#{rand(254) + 1}" 

 serv = TCPServer.new(host, 0) 

 nr = 5000 

 addr = serv.getsockname 
 pid = fork do 
   begin 
     serv.accept.close 
   rescue => e 
     warn "#$$: #{e.message} (#{e.class})" 
   end while true 
 end 
 at_exit { Process.kill(:TERM, pid) } 
 serv.close 

 Benchmark.bmbm do |x| 
   x.report("default") do 
     nr.times do 
       s = Socket.new(:INET, :STREAM) 
       s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1) 
       begin 
         s.connect_nonblock(addr) 
       rescue IO::WaitWritable 
         s.wait_writable 
       end 
       s.close 
     end 
   end 
   x.report("exception: false") do 
     nr.times do 
       s = Socket.new(:INET, :STREAM) 
       s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1) 
       case s.connect_nonblock(addr, exception: false) 
       when :wait_writable 
         s.wait_writable 
       end 
       s.close 
     end 
   end 
 end 
 ~~~ 

Back