Rubyファイバー


Ruby 1.9はFiberを持ってきました。http://www.infoq.com/news/2007/08/ruby-1-9-fibers
 
Rubyの中のFiberはsemi-cooutineです。これを初めて見たので分かりにくいです。
 
 
Subrooutine VS Coroutine
 
Semi-corountineを理解する前に、cooutineとは何かを知っておくと、サブルーチンは分かりやすく、比較することによってその違いが分かります。
 
Subrooutine
Coroutine
The lifespan of subroouties is dictated by last in、first out(the last subrouttine caled is the first to return)
The lifespan of corounties is dictated entirely by their use and need.
The start of a subrooutine is the only point of entry
The start of a corontine is the first point of entry and subsequent points of entry are following yield command.
Subrounties can return onlyonece
corounties can return(yield)several times
 
Practically,yielding returns the rerelt to the careling coouting and gives it back control,like a usual subrontine.However,the next time the corontine is caled,the execution doot not start the begining the the the cording of jective。
 
あれはセミ・コロンティエ:Semi-Coroutines are asymmetric Coroutines which are limited in their choice of control.Asymmetric Coroutines can only trace back to theirler,where Coroutines therever。
 
ファイバーVS Thread
 
RubyのFiberは制御構造(後にFiber:Coreといいます。)で、その運行方式と操作方法はthreadに似ています。
ファイバー
Thread
not concurrency
concurrency
reume、suspend(call Fiber.yieldまたはblockが終わるまで待つ)
レスリング
return and give control back to caler
not return or give control back unless join it
 
ファイバーVS Close ure
 
この方法を見てください。
 
fib = Fiber.new do
   f1 = f2 = 1
   loop do
     Fiber.yield f1 (  resume,   Fiber.yield  block     )
     f1, f2 = f2, f1 + f2 (   resume,     )
   end
end

10.times { puts fib.resume }
 
 
簡単にclosureでそれを実現すると思います。
 
def local_proc
    f1 = f2 = 1
    return Proc.new {f1, f2 = f2, f1 + f2; p f1;}
end

proc1 = local_proc

10.times {proc1.call}
 
 
上の二つの方法の実現方法はとても違っています。Fiber方式はenable the atomatic conversion of internal iterators、such as each、to enumerator-external iteratorsによって実現される。
ファイバー::Core VS Fiber
 
Fiberに比べて、Fiber:Coreは本当のcooutine--userspace threadを実現できるライト級スレッドです。
 
書き記す
Next to implement ing control structures、Coroutines provide a way to use
lightweight concurrency.In effect,they allow to
implement userspace threads with cooperabie scheduling.The Coroutines can einther yield control to each other,or have centralized scheduling by control to one scheduler Coroutine whech then dewdunets。
 
Ruby 1.9はケナール・threadsを実現しているので、軽量級の並列化が必要です。
 
書き記す
Nevertheless、creatiting a lot of kenel thread s still has still has of overhead、or might simply cause proble problems on Ostath have have have have thread limitstostugggle with largagage numbersof threads.It.It's.It's inininininininthethesesecasecasesecaseseseseseseaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaatttttttttttttttttttttstraightforward solution、but keeps the overhead down.
 
ファイバーfor JRuby
 
JRubyなど他のrubyを実現するFiberはいくつかの困難があります。
 
書き記す
If Fibers get adopted in Ruby、this will create headaches for Ruby implemenation s targingthe JVM or CLR、such as JRuby、XRLuby、Ruy.NET or IreonRuby.
None of them currently support Continations because manipulating or reading the calstack is hard or impossible to to do in these VMs.The lack of Continations a controversial ise,but doesn't seem to have caused problems.Rems.because they art widely used in Ruby.The only use of the m in the Rubry 1.8 standard library the Generator implemenation,but,for instance,JRuby 1.0,soved this by implement the same functions.Contingtiony.Contingtiongtiony。
 
ワーカソードを使うにしても、心配な性能の問題があります。
 
書き記す
While it's certainly possible to implement these feature s using workounds,the question is whether the se worlound will cause performance regressis.If,for instance,
cal stack s must be emulated on the heap,instead of using the VM's stack,this can lead to lower performance or prevent(JIT)copiler optimizations from being apped.Worlounds for asymmetric Coroutines the mastars。
 
 
用途
 
Coroutines are well-suited for implementing more familliar program components such as cooperate taskysiteratorsinfinite lists and pipes.
 
References:
corountine:http://en.wikipedia.org/wiki/Coroutine
Rubyのfiberでpipeを実現する例:http://pragdave.blogs.pragprog.com/pragdave/2007/12/pipelines-using.html
 
 
----EOF----