gh-106554: replace _BaseSelectorImpl._key_from_fd with dict.get (#106555)

This commit is contained in:
J. Nick Koston 2023-07-14 08:46:30 -10:00 committed by GitHub
parent 6a70edf24c
commit aeef8591e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 17 deletions

View File

@ -276,19 +276,6 @@ class _BaseSelectorImpl(BaseSelector):
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):
@ -336,7 +323,7 @@ class SelectSelector(_BaseSelectorImpl):
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
@ -426,7 +413,7 @@ class _PollLikeSelector(_BaseSelectorImpl):
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
@ -479,7 +466,7 @@ if hasattr(select, 'epoll'):
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
@ -574,7 +561,7 @@ if hasattr(select, 'kqueue'):
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

View File

@ -0,0 +1 @@
:mod:`selectors`: Reduce Selector overhead by using a ``dict.get()`` to lookup file descriptors.