-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PoC] Promise based coroutines #36
base: master
Are you sure you want to change the base?
[PoC] Promise based coroutines #36
Conversation
Performance-wise, this is "pretty okay": require "benchmark/ips"
require "promise"
Benchmark.ips do |x|
x.report("coroutine/await") do
p1 = Promise.new
p2 = Promise.new
p3 = Promise.new
p4 = Promise.new
promise = Promise.coroutine do
Promise.await p1
Promise.await Promise.coroutine {
p2
}
Promise.await p3
Promise.await p4
end
p1.fulfill(1)
p2.fulfill(2)
p3.fulfill(3)
p4.fulfill(4)
end
x.report("chained") do
p1 = Promise.new
p2 = Promise.new
p3 = Promise.new
p4 = Promise.new
promise = p1.then { p2.then }.then { p3 }.then { p4 }
p1.fulfill(1)
p2.fulfill(2)
p3.fulfill(3)
p4.fulfill(4)
end
x.compare!
end
|
The PR description uses
I thought the main problem with fibers was the fixed stack size. As in large fibers could result in overflows from using a smaller heap than the thread's stack and small fibers would waste memory. If you are planning on using it with graphql-batch, then I would expect they would end up being small fibers and would use a lot more memory.
I'm hesitant to add something that won't be widely used, yet would still need to be maintained for backwards compatibility. Also, it looks like something that can exist outside of this gem so it can be tried out in production. |
Whoops, yea, I somehow missed adding that. I added it now. 😅
This was true in Ruby 1.9, but it should no longer be an issue in Ruby 2.0, where the default fiber stack size was increased considerably:
That is totally understandable! 👍 I also would not want this to be merged without having done some proper performance and feasibility testing. This is just something I've been toying around recently and wanted it to throw out there for other people to see and to get some feedback. 😄 |
* coroutine functionality needs to be explicitly required by loading `promise/coroutine` * Promises returned by `Promise.coroutine` can now be correctly `.sync`ed. * The block passed to `Promise.coroutine` is no longer converted to a proc.
…rthur/promise-await
This adds Promise based coroutines, inspired by Bluebird's
Promise.coroutine
and JavaScript's async/await, but based on Ruby'sFiber
s.Example:
begin...rescue...end
also works:What do you think? If this is something that would be nice to have in
promise.rb
, I'll add tests and API documentation. 😄/cc @rmosolgo @josh