Live data from Hacker News

Coroutines and Fibers: Why and When

medium.com

1–10 of 23 posts

Re: Coroutines and Fibers: Why and When

#3
post #2

I don't understand how resumable functions can be used to build stack-full coroutines, like in Go language, can anyone give an example or some links?

Go has an interesting history surrounding its stacks. Originally the stack was a doubly linked-list (segmented stack), but it changed into a more vector-like format (contiguous stacks) in 1.3. Here's a pretty good write-up:

http://agis.io/2014/03/25/contiguous-stacks-in-go.html

Re: Coroutines and Fibers: Why and When

#4
Coroutines are also fantastic for scripting Game AI:

  local player = nil
  while(player == nil) do
    yeild(0)
    player = LookForPlayer(...)
  end
  
  while(WalkTowardsPlayer(player)) do
    yeild(0)
  end

  while(!PlayerDead(player)) do
    MeleePlayer(player)
    yield(0)
  end
Makes state management super-simple and is something you can easily teach to designers as well. This is partly why you see Lua show up in so many game engines.

Re: Coroutines and Fibers: Why and When

#6
I generally prefer coroutines myself but this article doesn't cover some points well.

'Fair scheduling' - nobody writes single threaded programs that process requests sequentially. It's either a thread-per-request system vs. a coroutine-per-request system. Threads are generally going to be more fair at scheduling since they are preemptive. Most coroutines are cooperative (Python, Lua) - so a long running coroutine (stuck doing some CPU cycle) will block all other inflight requests and cause latency variance. Some systems are preemptive (Erlang) so they don't suffer from the variance.

The benefit of coroutines is you can have a very large number of coroutines, probably orders of magnitude more than the number of threads - so coroutine-per-request models will scale much more than threads-per-request. The article is spot on about the context switching cost - it's so much cheaper to switch between coroutines.

You can also use multiple request per threads and use async IO, but that's the same as using coroutines with a much worse programming model.

Re: Coroutines and Fibers: Why and When

#9
post #5

One caveat with coroutines and fibers is that all the code that runs on them must be fully aware that they are running on said coroutines and fibers.

at least in java (i'm using https://github.com/kilim/kilim, but quasar appears to be similar), only methods that yield need to be aware

Re: Coroutines and Fibers: Why and When

#10
i'm using java fibers (https://github.com/kilim/kilim) for a database

for anyone interested in using fibers inside a java webapp, i just wrote up a quick survey of async java servlet performance: http://blog.nqzero.com/2016/01/asynchronous-java-webserver-h...

(based TechEmpower plaintext benchmark, but limited to java async)

the model is that you use async in the server, and then bridge to a fiber implementation and then complete the async response when the fiber completes. for handling high latency low cpu processing on the server, this technique allows handling a huge number of connections

Post reply on HN