From 53bac4f65d9430495c8043b239cc936012ea7a8d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 6 Feb 2011 06:19:09 +0000 Subject: minimize &block usage for yield No need to allocate a proc every time when we can just yield much more efficiently. --- lib/rainbows/dev_fd_response.rb | 4 ++-- lib/rainbows/ev_core/cap_input.rb | 2 +- lib/rainbows/fiber/base.rb | 4 ++-- lib/rainbows/queue_pool.rb | 4 ++-- lib/rainbows/sync_close.rb | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/rainbows/dev_fd_response.rb b/lib/rainbows/dev_fd_response.rb index 1ee3375..eaa4af4 100644 --- a/lib/rainbows/dev_fd_response.rb +++ b/lib/rainbows/dev_fd_response.rb @@ -79,8 +79,8 @@ class Rainbows::DevFdResponse < Struct.new(:app) class Body < Struct.new(:to_io, :to_path, :orig_body) # called by the webserver or other middlewares if they can't # handle #to_path - def each(&block) - to_io.each(&block) + def each + to_io.each { |x| yield x } end # remain Rack::Lint-compatible for people with wonky systems :P diff --git a/lib/rainbows/ev_core/cap_input.rb b/lib/rainbows/ev_core/cap_input.rb index 4865da4..06887d5 100644 --- a/lib/rainbows/ev_core/cap_input.rb +++ b/lib/rainbows/ev_core/cap_input.rb @@ -15,7 +15,7 @@ class Rainbows::EvCore::CapInput end def gets; @io.gets; end - def each(&block); @io.each(&block); end + def each; @io.each { |x| yield x }; end def size; @io.size; end def rewind; @io.rewind; end def read(*args); @io.read(*args); end diff --git a/lib/rainbows/fiber/base.rb b/lib/rainbows/fiber/base.rb index 126f338..a4f2341 100644 --- a/lib/rainbows/fiber/base.rb +++ b/lib/rainbows/fiber/base.rb @@ -17,7 +17,7 @@ module Rainbows::Fiber::Base # schedules ones that were blocked on I/O. At most it'll sleep # for one second (returned by the schedule_sleepers method) which # will cause it. - def schedule(&block) + def schedule begin Rainbows.tick t = schedule_sleepers @@ -33,7 +33,7 @@ module Rainbows::Fiber::Base ret[1].concat(RD.compact & ret[0]).each { |c| c.f.resume } # accept is an expensive syscall, filter out listeners we don't want - (ret[0] & LISTENERS).each(&block) + (ret[0] & LISTENERS).each { |x| yield x } end # wakes up any sleepers or keepalive-timeout violators that need to be diff --git a/lib/rainbows/queue_pool.rb b/lib/rainbows/queue_pool.rb index 99cb9db..ce888d8 100644 --- a/lib/rainbows/queue_pool.rb +++ b/lib/rainbows/queue_pool.rb @@ -6,12 +6,12 @@ require 'thread' # This is NOT used for the ThreadPool class, since that class does not # need a userspace Queue. class Rainbows::QueuePool < Struct.new(:queue, :threads) - def initialize(size = 20, &block) + def initialize(size = 20) q = Queue.new self.threads = (1..size).map do Thread.new do while job = q.shift - block.call(job) + yield job end end end diff --git a/lib/rainbows/sync_close.rb b/lib/rainbows/sync_close.rb index a336262..75f119e 100644 --- a/lib/rainbows/sync_close.rb +++ b/lib/rainbows/sync_close.rb @@ -20,8 +20,8 @@ class Rainbows::SyncClose @body.to_path end - def each(&block) - @body.each(&block) + def each + @body.each { |x| yield x } end def to_io -- cgit v1.2.3-24-ge0c7