Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ae51de
commit e688a41
Showing
3 changed files
with
58 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'fiber' if defined?(Fiber) | ||
|
||
module Thin | ||
# Turns out Enumerator#next is pretty slow and rolling our own w/ Fibers is a lot faster. | ||
# Still not as fast as Enumerable#each but eh... | ||
class FastEnumerator | ||
if defined?(Fiber) | ||
|
||
def initialize(array) | ||
@fiber = Fiber.new do | ||
array.each do |i| | ||
Fiber.yield i | ||
end | ||
end | ||
end | ||
|
||
def next | ||
nil unless @fiber.alive? | ||
@fiber.resume | ||
end | ||
|
||
else | ||
|
||
# Slow Enumerator based version for Rubies w/ no fibers. | ||
|
||
def initialize(args) | ||
@enum = array.each | ||
end | ||
|
||
def next | ||
begin | ||
@enum.next | ||
rescue StopIteration | ||
nil | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |