Skip to content
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

Fiber / green threads / coroutines? #106

Closed
chuckremes opened this issue Mar 25, 2017 · 7 comments
Closed

Fiber / green threads / coroutines? #106

chuckremes opened this issue Mar 25, 2017 · 7 comments
Assignees

Comments

@chuckremes
Copy link
Contributor

What is the plan for supporting Fibers? Right now JRuby backs each Fiber with a thread which is rather heavy on resources. Will TruffleRuby support anything lighter like green threads or coroutines to support the Fiber?

@eregon
Copy link
Member

eregon commented Mar 25, 2017

Currently TruffleRuby uses Java threads for Fibers.

The standard JVM does not yet support native coroutines.
However, Lukas Stadler made a patch to add native coroutine support (http://ssw.jku.at/General/Staff/LS/coro/).

I am using that patch in my Oz language implementation.
The patched JVM is at https://github.com/eregon/jvmci and the corresponding API in https://github.com/eregon/truffle/tree/coro, which uses threads or coroutines depending on the JVM it runs on.

So we might use that to try what would be the benefits of native coroutines, but it is unpractical as it requires a custom JVM build.

Do you have any particular library or application in mind where it is important to have lightweight Fibers?
Thread switching is not so much slower, but they use more memory.

@chrisseaton
Copy link
Collaborator

This is documented in our compatibility document. I've updated it a bit to say some more about fibers.

https://github.com/graalvm/truffleruby/blob/9d3a8f3e9f47915a0e4167e1002960a70b68f44b/doc/user/compatibility.md

Fibers, continuations and coroutines are all things that fundamentally don't match the execution model we are using at the moment and are unlikely to every be implemented with the same performance characteristics as MRI.

It's a shame, but it's one of a very few hard compatibility limitations we have.

@chrisseaton chrisseaton self-assigned this Mar 25, 2017
@eregon
Copy link
Member

eregon commented Mar 26, 2017

@chrisseaton I don't believe coroutines (not continuations) "fundamentally don't match the execution model we are using at the moment". See my previous answer.
Using that patch would allow to have similar performance characteristics as MRI.
But yes, it's not practical to require a custom JVM build of course.
I agree it's better to be clear in compatibility: we don't support coroutines in the current distribution and use threads for Fibers.
But there is nothing fundamentally impossible here.

@chrisseaton
Copy link
Collaborator

To clarify, when I say 'fundamentally doesn't match our execution model' I mean running on an unmodified JVM. Benoit is talking about things which may be possible with modified JVMs or the SubstrateVM.

@chuckremes
Copy link
Contributor Author

Thanks for your responses.

My use-case is related to celluloid and other programming libraries that hide asynchronous behavior behind a Fiber-based synchronous interface. At times there can be hundreds or thousands of fibers utilized by the libraries, so lightweight fibers are useful in that regard.

I would like to use TruffleRuby for a few proof-of-concept projects. I will likely need to implement some kind of fiber pooling mechanism to prevent run away thread allocation. If I discover anything interesting, I'll be sure to share it with you all.

@eregon
Copy link
Member

eregon commented Mar 27, 2017

FWIW reusing FIber threads is something relatively easy, just not done currently in TruffleRuby (patch welcome!).
https://github.com/eregon/truffle/blob/247496f4984d9f08775f165cd7538a6d4c0dd90b/truffle/com.oracle.truffle.coro/src/com/oracle/truffle/coro/ThreadCoroutineSupport.java#L108

chrisseaton pushed a commit that referenced this issue May 30, 2018
* commit 'f45d5739edf37d2a03ee944a7da137aa1ad1667b':
  Use mx native-image instead of ./native-image
@eregon
Copy link
Member

eregon commented Jun 21, 2018

For information, Fibers now use a thread pool, so the cost to create many of them should be much lower.

chrisseaton pushed a commit to Shopify/truffleruby that referenced this issue Nov 13, 2019
Point to our fork of graal on jt

Merge-Requested-By: rafaelfranca
Merge-Queue-Digest: 5086b6fbf6fa3b11a9406903a500642ab948d7cc850b6becb25692c13d312128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants