]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106751: Optimize KqueueSelector.select() for many iteration case (gh-106864)
authorDong-hee Na <donghee.na@python.org>
Wed, 19 Jul 2023 01:18:23 +0000 (10:18 +0900)
committerGitHub <noreply@github.com>
Wed, 19 Jul 2023 01:18:23 +0000 (10:18 +0900)
Lib/selectors.py
Misc/NEWS.d/next/Library/2023-07-18-23-05-12.gh-issue-106751.tVvzN_.rst [new file with mode: 0644]

index a42d15634064178acbe4cbdcc29e07c8b0ba2de1..d13405963f219de903b6d96e638b65b1ed3396cf 100644 (file)
@@ -547,23 +547,21 @@ if hasattr(select, 'kqueue'):
             # 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)
+            max_ev = len(self._fd_to_key) or 1
             ready = []
             try:
                 kev_list = self._selector.control(None, max_ev, timeout)
             except InterruptedError:
                 return ready
+
+            fd_to_key_get = self._fd_to_key.get
             for kev in kev_list:
                 fd = kev.ident
                 flag = kev.filter
-                events = 0
-                if flag == select.KQ_FILTER_READ:
-                    events |= EVENT_READ
-                if flag == select.KQ_FILTER_WRITE:
-                    events |= EVENT_WRITE
-
-                key = self._fd_to_key.get(fd)
+                key = fd_to_key_get(fd)
                 if key:
+                    events = ((flag == select.KQ_FILTER_READ and EVENT_READ)
+                              | (flag == select.KQ_FILTER_WRITE and EVENT_WRITE))
                     ready.append((key, events & key.events))
             return ready
 
diff --git a/Misc/NEWS.d/next/Library/2023-07-18-23-05-12.gh-issue-106751.tVvzN_.rst b/Misc/NEWS.d/next/Library/2023-07-18-23-05-12.gh-issue-106751.tVvzN_.rst
new file mode 100644 (file)
index 0000000..1cb8424
--- /dev/null
@@ -0,0 +1,2 @@
+Optimize :meth:`KqueueSelector.select` for many iteration case. Patch By
+Dong-hee Na.