GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Answer zero-timeout IO.select with one poll(2) call by sampokuokkanen · Pull Request #9865 · jruby/jruby

Conversation

@sampokuokkanen

@sampokuokkanen sampokuokkanen commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

A zero-timeout IO.select registered every channel with a pooled selector, polled it, then cancelled the keys and polled again to flush them before returning the selector. io-event's select backend makes this call on every turn of the event loop.

A zero timeout cannot block, so none of that registration is needed to keep it interruptible. When native support is on and every IO has a real, selectable descriptor, check them with poll(2) directly, falling back to the selectors otherwise. Buffered read data still counts as ready, and a socket with a connect in progress goes to the selectors, which finish the connect once it is writable. Readiness is collected per descriptor because macOS reports a descriptor that appears twice in only one of its pollfds.

IO.select([r], [w], nil, 0) on a pipe, on an M1 Pro, drops from 14.8us to 1.0us per call, about 14 times faster; MRI 4.0 takes 1.2us. The new tests also pass on master, just there to catch bugs in the new fast path.

The POLL* values are the ones Linux, macOS, the BSDs and Solaris share; other systems, such as AIX, keep using the selectors.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sampokuokkanen
Answer zero-timeout IO.select with one poll(2) call
59d1943
A zero-timeout IO.select registered every channel with a pooled
selector, polled it, then cancelled the keys and polled again to flush
them before returning the selector. io-event's select backend makes
this call on every turn of the event loop.

A zero timeout cannot block, so none of that registration is needed to
keep it interruptible. When native support is on and every IO has a
real, selectable descriptor, check them with poll(2) directly, falling
back to the selectors otherwise. Buffered read data still counts as
ready, and a socket with a connect in progress goes to the selectors,
which finish the connect once it is writable. Readiness is collected
per descriptor because macOS reports a descriptor that appears twice
in only one of its pollfds.

IO.select([r], [w], nil, 0) on a pipe, on an M1 Pro, drops from 14.8us
to 1.0us per call, about 14 times faster; MRI 4.0 takes 1.2us.

The POLL* values are the ones Linux, macOS, the BSDs and Solaris share;
other systems, such as AIX, keep using the selectors.
@sampokuokkanen
sampokuokkanen force-pushed the select-zero-timeout branch from 59d1943 to eb9e472 Compare October 22, 2026 01:24
@sampokuokkanen
Keep zero-timeout IO.select steady when #to_io changes its arrays
7d3195b
The poll path took the array sizes once, so a #to_io that shrinks the
array it was found in made it read past the end and raise TypeError,
where the selectors and MRI return nil. Poll over copies instead.

The except array is now checked after the read and write arrays and
for its write IO as well, as selectInternal and MRI's select_internal
do, and the struct pollfd fields get named offsets.
@sampokuokkanen
sampokuokkanen force-pushed the select-zero-timeout branch from eb9e472 to 7d3195b Compare October 22, 2026 01:32
sampokuokkanen added 3 commits October 22, 2026 11:24
@sampokuokkanen
Fill the pollfds for zero-timeout IO.select after every #to_io
ba55663
The pollfds buffer is kept per thread, so a #to_io that runs another
zero-timeout IO.select overwrote the entries already filled in, and
the outer select could miss a ready IO. Collect the descriptors first
and fill the buffer just before poll(2), once no Ruby code can run.
@sampokuokkanen
Share the except array check between both IO.select paths
84a09a4
@sampokuokkanen
Load libc for zero-timeout IO.select straight into PollSelect
943fbd2
PollSelect itself is only loaded on the first zero-timeout select, so a
holder class makes the loading no lazier.
headius
headius reviewed Oct 22, 2026
View reviewed changes

@headius headius left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This makes good sense, but I'm sure large parts of this should live in jnr-enxio instead of here. The selector registration process in NIO is cumbersome, but when we know we have an ENXIO native channel we could call a method on it that hides all of this native logic rather than maintaining it here. That would then be usable by other consumers of ENXIO and less maintenance burden for JRuby core.

Make sense?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sampokuokkanen reacted with eyes emoji
All reactions
  • 👀 1 reaction
@sampokuokkanen
sampokuokkanen marked this pull request as draft October 26, 2026 11:28
@sampokuokkanen

sampokuokkanen commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, I'll look into putting most of this into jnr-enxio.

Converted to draft for now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@headius headius headius left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@sampokuokkanen @headius
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.