[Python-checkins] gh-106751: Optimize SelectSelector.select() for many iteration case (gh-106879)

corona10 webhook-mailer at python.org
Wed Jul 19 02:12:42 EDT 2023


https://github.com/python/cpython/commit/e6f96cf9c62e38514e8f5465a1c43f85d861adb2
commit: e6f96cf9c62e38514e8f5465a1c43f85d861adb2
branch: main
author: Dong-hee Na <donghee.na at python.org>
committer: corona10 <donghee.na92 at gmail.com>
date: 2023-07-19T15:12:38+09:00
summary:

gh-106751: Optimize SelectSelector.select() for many iteration case (gh-106879)

files:
A Misc/NEWS.d/next/Library/2023-07-19-10-45-24.gh-issue-106751.3HJ1of.rst
M Lib/selectors.py

diff --git a/Lib/selectors.py b/Lib/selectors.py
index d13405963f219..13497a2409723 100644
--- a/Lib/selectors.py
+++ b/Lib/selectors.py
@@ -314,17 +314,15 @@ def select(self, timeout=None):
             r, w, _ = self._select(self._readers, self._writers, [], timeout)
         except InterruptedError:
             return ready
-        r = set(r)
-        w = set(w)
-        for fd in r | w:
-            events = 0
-            if fd in r:
-                events |= EVENT_READ
-            if fd in w:
-                events |= EVENT_WRITE
-
-            key = self._fd_to_key.get(fd)
+        r = frozenset(r)
+        w = frozenset(w)
+        rw = r | w
+        fd_to_key_get = self._fd_to_key.get
+        for fd in rw:
+            key = fd_to_key_get(fd)
             if key:
+                events = ((fd in r and EVENT_READ)
+                          | (fd in w and EVENT_WRITE))
                 ready.append((key, events & key.events))
         return ready
 
diff --git a/Misc/NEWS.d/next/Library/2023-07-19-10-45-24.gh-issue-106751.3HJ1of.rst b/Misc/NEWS.d/next/Library/2023-07-19-10-45-24.gh-issue-106751.3HJ1of.rst
new file mode 100644
index 0000000000000..2696b560371d1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-07-19-10-45-24.gh-issue-106751.3HJ1of.rst
@@ -0,0 +1,2 @@
+Optimize :meth:`SelectSelector.select` for many iteration case. Patch By
+Dong-hee Na.



More information about the Python-checkins mailing list