Skip to content

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
merged 7 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions Lib/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,6 @@ def close(self):
def get_map(self):
return self._map

def _key_from_fd(self, fd):
"""Return the key associated to a given file descriptor.

Parameters:
fd -- file descriptor

Returns:
corresponding key, or None if not found
"""
try:
return self._fd_to_key[fd]
except KeyError:
return None


class SelectSelector(_BaseSelectorImpl):
Expand Down Expand Up @@ -332,7 +319,7 @@ def select(self, timeout=None):
if fd in w:
events |= EVENT_WRITE

key = self._key_from_fd(fd)
key = self._fd_to_key.get(fd)
if key:
ready.append((key, events & key.events))
return ready
Expand Down Expand Up @@ -422,7 +409,7 @@ def select(self, timeout=None):
if event & ~self._EVENT_WRITE:
events |= EVENT_READ

key = self._key_from_fd(fd)
key = self._fd_to_key.get(fd)
if key:
ready.append((key, events & key.events))
return ready
Expand Down Expand Up @@ -475,7 +462,7 @@ def select(self, timeout=None):
if event & ~select.EPOLLOUT:
events |= EVENT_READ

key = self._key_from_fd(fd)
key = self._fd_to_key.get(fd)
if key:
ready.append((key, events & key.events))
return ready
Expand Down Expand Up @@ -570,7 +557,7 @@ def select(self, timeout=None):
if flag == select.KQ_FILTER_WRITE:
events |= EVENT_WRITE

key = self._key_from_fd(fd)
key = self._fd_to_key.get(fd)
if key:
ready.append((key, events & key.events))
return ready
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`selectors`: Reduce Selector overhead by using a ``dict.get()`` to lookup file descriptors.