-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-106554: Replace _BaseSelectorImpl._key_from_fd
with a simple get
#106555
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`_key_from_fd` re-implemented `.get()` and can be removed
import timeit
import math
import select
import os
from selectors import EpollSelector, EVENT_WRITE, EVENT_READ
class OriginalEpollSelector(EpollSelector):
def select(self, timeout=None):
if timeout is None:
timeout = -1
elif timeout <= 0:
timeout = 0
else:
# epoll_wait() has a resolution of 1 millisecond, round away
# from zero to wait *at least* timeout seconds.
timeout = math.ceil(timeout * 1e3) * 1e-3
# epoll_wait() expects `maxevents` to be greater than zero;
# we want to make sure that `select()` can be called when no
# FD is registered.
max_ev = max(len(self._fd_to_key), 1)
ready = []
try:
fd_event_list = self._selector.poll(timeout, max_ev)
except InterruptedError:
return ready
for fd, event in fd_event_list:
events = 0
if event & ~select.EPOLLIN:
events |= EVENT_WRITE
if event & ~select.EPOLLOUT:
events |= EVENT_READ
key = self._key_from_fd(fd)
if key:
ready.append((key, events & key.events))
return ready
class NewEpollSelector(EpollSelector):
def select(self, timeout=None):
if timeout is None:
timeout = -1
elif timeout <= 0:
timeout = 0
else:
# epoll_wait() has a resolution of 1 millisecond, round away
# from zero to wait *at least* timeout seconds.
timeout = math.ceil(timeout * 1e3) * 1e-3
# epoll_wait() expects `maxevents` to be greater than zero;
# we want to make sure that `select()` can be called when no
# FD is registered.
max_ev = max(len(self._fd_to_key), 1)
ready = []
try:
fd_event_list = self._selector.poll(timeout, max_ev)
except InterruptedError:
return ready
for fd, event in fd_event_list:
events = 0
if event & ~select.EPOLLIN:
events |= EVENT_WRITE
if event & ~select.EPOLLOUT:
events |= EVENT_READ
key = self._fd_to_key.get(fd)
if key:
ready.append((key, events & key.events))
return ready
original_epoll = OriginalEpollSelector()
new_epoll = NewEpollSelector()
for _ in range(512):
r, w = os.pipe()
os.write(w, b"a")
original_epoll.register(r, EVENT_READ)
new_epoll.register(r, EVENT_READ)
original_time = timeit.timeit(
"selector.select()",
number=100000,
globals={"selector": original_epoll},
)
new_time = timeit.timeit(
"selector.select()",
number=100000,
globals={"selector": new_epoll},
)
print("original: %s" % original_time)
print("new: %s" % new_time) |
512 pipes
1024 pipes
|
def select(self, timeout=None):
timeout = None if timeout is None else max(timeout, 0)
# If max_ev is 0, kqueue will ignore the timeout. For consistent
# behavior with the other selector classes, we prevent that here
# (using max). See https://bugs.python.org/issue29255
max_ev = max(len(self._fd_to_key), 1)
ready = []
try:
kev_list = self._selector.control(None, max_ev, timeout)
except InterruptedError:
return ready
for kev in kev_list:
key = self._fd_to_key.get(kev.ident)
if key:
flag = kev.filter
if flag == select.KQ_FILTER_READ:
events = EVENT_READ
elif flag == select.KQ_FILTER_WRITE:
events = EVENT_WRITE
else:
events = 0
ready.append((key, events & key.events))
return ready |
bdraco
commented
Jul 12, 2023
Misc/NEWS.d/next/Library/2023-07-09-01-59-24.gh-issue-106554.37c53J.rst
Outdated
Show resolved
Hide resolved
bdraco
commented
Jul 12, 2023
Misc/NEWS.d/next/Library/2023-07-09-01-59-24.gh-issue-106554.37c53J.rst
Outdated
Show resolved
Hide resolved
bdraco
commented
Jul 12, 2023
Misc/NEWS.d/next/Library/2023-07-09-01-59-24.gh-issue-106554.37c53J.rst
Outdated
Show resolved
Hide resolved
This was referenced Jul 13, 2023
With #106555 (comment) the selector overhead falls below the cost of |
methane
reviewed
Jul 14, 2023
Misc/NEWS.d/next/Library/2023-07-09-01-59-24.gh-issue-106554.37c53J.rst
Outdated
Show resolved
Hide resolved
Thanks. Will work on the follow ups from above |
This was referenced Dec 6, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
_BaseSelectorImpl._key_from_fd
reimplements.get()
#106554