Skip to content

Commit aeef859

Browse files
authored
gh-106554: replace _BaseSelectorImpl._key_from_fd with dict.get (#106555)
1 parent 6a70edf commit aeef859

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

Lib/selectors.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -276,19 +276,6 @@ def close(self):
276276
def get_map(self):
277277
return self._map
278278

279-
def _key_from_fd(self, fd):
280-
"""Return the key associated to a given file descriptor.
281-
282-
Parameters:
283-
fd -- file descriptor
284-
285-
Returns:
286-
corresponding key, or None if not found
287-
"""
288-
try:
289-
return self._fd_to_key[fd]
290-
except KeyError:
291-
return None
292279

293280

294281
class SelectSelector(_BaseSelectorImpl):
@@ -336,7 +323,7 @@ def select(self, timeout=None):
336323
if fd in w:
337324
events |= EVENT_WRITE
338325

339-
key = self._key_from_fd(fd)
326+
key = self._fd_to_key.get(fd)
340327
if key:
341328
ready.append((key, events & key.events))
342329
return ready
@@ -426,7 +413,7 @@ def select(self, timeout=None):
426413
if event & ~self._EVENT_WRITE:
427414
events |= EVENT_READ
428415

429-
key = self._key_from_fd(fd)
416+
key = self._fd_to_key.get(fd)
430417
if key:
431418
ready.append((key, events & key.events))
432419
return ready
@@ -479,7 +466,7 @@ def select(self, timeout=None):
479466
if event & ~select.EPOLLOUT:
480467
events |= EVENT_READ
481468

482-
key = self._key_from_fd(fd)
469+
key = self._fd_to_key.get(fd)
483470
if key:
484471
ready.append((key, events & key.events))
485472
return ready
@@ -574,7 +561,7 @@ def select(self, timeout=None):
574561
if flag == select.KQ_FILTER_WRITE:
575562
events |= EVENT_WRITE
576563

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

0 commit comments

Comments
 (0)