Skip to content

Use rb_io_wait function and cache io instance.#47

Open
ioquatix wants to merge 5 commits into
trilogy-libraries:mainfrom
ioquatix:main
Open

Use rb_io_wait function and cache io instance.#47
ioquatix wants to merge 5 commits into
trilogy-libraries:mainfrom
ioquatix:main

Conversation

@ioquatix

Copy link
Copy Markdown

@matthewd matthewd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following up on this @ioquatix!

Comment thread contrib/ruby/ext/trilogy-ruby/cext.c Outdated
Comment thread contrib/ruby/ext/trilogy-ruby/cext.c Outdated
Comment thread contrib/ruby/ext/trilogy-ruby/cext.c Outdated
@ioquatix

ioquatix commented Feb 25, 2023

Copy link
Copy Markdown
Author

@matthewd can you please approve and run the workflows.

@ioquatix ioquatix force-pushed the main branch 3 times, most recently from 61269c1 to dc88886 Compare February 25, 2023 07:47
@ioquatix

Copy link
Copy Markdown
Author

There is one more piece we should consider exposing, which is the "resolve DNS" to use the fiber scheduler hook.

@ioquatix

Copy link
Copy Markdown
Author

So, writing this PR has made me realise we need a set of shims for the blocking C APIs, e.g. open, addrinfo, etc. We have the code, it's just not exposed. I supposed we can do that for Ruby 3.3.

We should also extend the test suite to run within a fiber scheduler (probably Async).

@matthewd

matthewd commented Mar 2, 2023

Copy link
Copy Markdown
Collaborator

@ioquatix is this good to go? It seems fine to me, not sure if you've kept it in draft for a reason, or just not hit the button?

@ioquatix

ioquatix commented Mar 2, 2023

Copy link
Copy Markdown
Author

It's probably as good as we can make it.. but I want to review it this weekend. This week has been pretty busy, have not had much time for open source stuff.

Comment thread contrib/ruby/ext/trilogy-ruby/cext.c Outdated
@kirs

kirs commented Oct 4, 2024

Copy link
Copy Markdown

👋 just wanted to say that I appreciate this WIP PR by Samuel and I'd really really love to see this happen in the upstream, and to be able to use trilogy with fiber scheduler for IO-heavy workloads.

@casperisfine

Copy link
Copy Markdown
Contributor

I'd really really love to see this happen in the upstream, and to be able to use trilogy with fiber scheduler for IO-heavy workloads.

So my knowledge of the fiber scheduler is limited, but AFAIK you can already use it today? My understanding is that this PR make it a bit more efficient, but rb_wait_for_single_fd is already enough to be usable.

As for it happening, there's not much work needed, all the blockers are listed in review comments.

@ioquatix ioquatix marked this pull request as ready for review October 25, 2024 05:24
@ioquatix

Copy link
Copy Markdown
Author

I've rebased and updated the PR.

rb_wait_for_single_fd is okay but it's a slow path. rb_io_wait is the fast path.

@matthewd can you please run CI.

@ioquatix ioquatix force-pushed the main branch 3 times, most recently from cc37ef9 to a3baae0 Compare October 25, 2024 06:47
Comment thread contrib/ruby/ext/trilogy-ruby/cext.c Outdated
Comment thread contrib/ruby/ext/trilogy-ruby/cext.c Outdated
Comment thread contrib/ruby/ext/trilogy-ruby/cext.c Outdated
@ioquatix ioquatix force-pushed the main branch 6 times, most recently from ab563aa to effb070 Compare October 25, 2024 07:57
@ioquatix

ioquatix commented Oct 25, 2024

Copy link
Copy Markdown
Author

I recall why I gave up on this PR for a while, we were missing rb_io_open_descriptor which is needed to create a "temporary non-owning FMODE_EXTERNAL IO instance". Since that's now available in recent CRuby, it is possible to implement it correctly.

@ioquatix

Copy link
Copy Markdown
Author

I believe all the feedback has been addressed, so this is good to go.

Comment thread contrib/ruby/ext/trilogy-ruby/cext.c Outdated

@eregon eregon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the performance gain with this?
Is it worth the duplication of _cb_ruby_wait and additional complexity?

@ioquatix

ioquatix commented Feb 24, 2025

Copy link
Copy Markdown
Author

What's the performance gain with this?

It should be slightly more efficient, but I imagine there isn't much performance difference (except for GC overhead due to the temporary IO objects created).

Is it worth the duplication of _cb_ruby_wait and additional complexity?

I would say yes. rb_wait_for_single_fd creates a temporary IO instance for each call, which creates extra (pointless) garbage. It's better for Ruby extensions that do IO to use a proper IO object IMHO. I would like to deprecate rb_wait_for_single_fd. It's messy/difficult for some platforms to implement, e.g. JRuby.

@ioquatix

Copy link
Copy Markdown
Author

I think now that rb_trilogy_wait_protected was extracted out, maybe we can simplify this PR a little to address the complexity @eregon mentioned.

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

Successfully merging this pull request may close these issues.

5 participants