From df634904b60f0aa17a99afd225c58a7605c9ee37 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 27 Dec 2013 08:08:50 +0000 Subject: merge some select calls for splice users Avoid an additional select syscall in the splice path by injecting the target checks into the main loop. We can do this because we always process writers before readers. This adds additional userspace processing, but it avoids one potentially expensive (and potentially task-switching) syscall in many cases; so it should be worth it to avoid skipping with small buffer. This should avoid buffer underuns with mixed-sized buffers when using multiple sinks. This could work for the read_write path, too, but I don't use that enough and this change may still be buggy even for splice users. --- lib/dtas/buffer/splice.rb | 4 +++- lib/dtas/player.rb | 8 ++++++++ lib/dtas/writable_iter.rb | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) (limited to 'lib/dtas') diff --git a/lib/dtas/buffer/splice.rb b/lib/dtas/buffer/splice.rb index 9093860..2a7e9a4 100644 --- a/lib/dtas/buffer/splice.rb +++ b/lib/dtas/buffer/splice.rb @@ -69,7 +69,9 @@ module DTAS::Buffer::Splice # :nodoc: end def broadcast_inf(targets) - if targets.none?(&:nonblock?) + if targets.all?(&:ready_write_optimized?) + blocked = [] + elsif targets.none?(&:nonblock?) # if all targets are blocking, don't start until they're all writable r = IO.select(nil, targets, nil, 0) or return targets blocked = targets - r[1] diff --git a/lib/dtas/player.rb b/lib/dtas/player.rb index 56fbdac..04c03b0 100644 --- a/lib/dtas/player.rb +++ b/lib/dtas/player.rb @@ -300,6 +300,13 @@ class DTAS::Player # :nodoc: end end + def _optimize_write_prepare(targets) + targets.each do |dst| + dst.wait_writable_prepare + @srv.wait_ctl(dst, :wait_writable) + end + end + # returns a wait_ctl arg for self def broadcast_iter(buf, targets) case rv = buf.broadcast(targets) @@ -315,6 +322,7 @@ class DTAS::Player # :nodoc: # via DTAS::Sink#writable_iter :ignore else # :wait_readable or nil + _optimize_write_prepare(targets) rv end end diff --git a/lib/dtas/writable_iter.rb b/lib/dtas/writable_iter.rb index 1fd65eb..2355352 100644 --- a/lib/dtas/writable_iter.rb +++ b/lib/dtas/writable_iter.rb @@ -4,9 +4,24 @@ require_relative '../dtas' module DTAS::WritableIter # :nodoc: attr_accessor :on_writable + # we may use the ready_write flag to avoid an extra IO.select + attr_accessor :ready_write def writable_iter_init + @mark_writable = proc { @ready_write = true } @on_writable = nil + @ready_write = true + end + + def ready_write_optimized? + rv = @ready_write + @ready_write = false + rv + end + + def wait_writable_prepare + @ready_write = false + @on_writable ||= @mark_writable end # this is used to exchange our own writable status for the readable @@ -14,6 +29,7 @@ module DTAS::WritableIter # :nodoc: def writable_iter if owr = @on_writable @on_writable = nil + @ready_write = true owr.call # this triggers readability watching of DTAS::Buffer end :ignore -- cgit v1.2.3-24-ge0c7