Skip to content

Commit

Permalink
Optimize rb_wait_for_single_fd() to call poll(2) more directly
Browse files Browse the repository at this point in the history
* IO.select also uses poll() on TruffleRuby but requires
  extra processing on the arguments, extra allocations, etc.
  • Loading branch information
eregon committed Jan 10, 2024
1 parent 8b4a7b3 commit 0258c5f
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1977,25 +1977,15 @@ def rb_thread_fd_writable(fd)
end

def rb_wait_for_single_fd(fd, events, tv_secs, tv_usecs)
io = IO.for_fd(fd)
io.autoclose = false
read = (events & RB_WAITFD_IN) != 0 ? [io] : nil
write = (events & RB_WAITFD_OUT) != 0 ? [io] : nil
error = (events & RB_WAITFD_PRI) != 0 ? [io] : nil
timeout = nil
if tv_secs >= 0 || tv_usecs >= 0
timeout = tv_secs + tv_usecs/1.0e6
end
r, w, e = Primitive.send_without_cext_lock(IO, :select, [read, write, error, *timeout], nil)
if Primitive.nil?(r) # timeout
0
else
result = 0
result |= RB_WAITFD_IN unless r.empty?
result |= RB_WAITFD_OUT unless w.empty?
result |= RB_WAITFD_PRI unless e.empty?
result
end

io = IO.for_fd(fd)
io.autoclose = false
returned_events = Primitive.send_without_cext_lock(Truffle::IOOperations, :poll, [io, events, timeout], nil)
returned_events & (RB_WAITFD_IN | RB_WAITFD_OUT | RB_WAITFD_PRI)
end

def rb_call_super(args)
Expand Down

0 comments on commit 0258c5f

Please sign in to comment.