From 6eb04b778f76d5deecbb266b88dc2762f0f91f13 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 18 Jul 2023 22:34:24 +0900 Subject: [PATCH 1/2] gh-106751: Optimize KqueueSelector.select() for many iteration case --- Lib/selectors.py | 14 ++++++-------- .../2023-07-18-23-05-12.gh-issue-106751.tVvzN_.rst | 2 ++ 2 files changed, 8 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-07-18-23-05-12.gh-issue-106751.tVvzN_.rst diff --git a/Lib/selectors.py b/Lib/selectors.py index a42d1563406417..106488d888ac1e 100644 --- a/Lib/selectors.py +++ b/Lib/selectors.py @@ -547,23 +547,21 @@ def select(self, timeout=None): # 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 = self._fd_to_key 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 index 00000000000000..1cb8424b6221ee --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-18-23-05-12.gh-issue-106751.tVvzN_.rst @@ -0,0 +1,2 @@ +Optimize :meth:`KqueueSelector.select` for many iteration case. Patch By +Dong-hee Na. From e4f7f645fbdc96817d0f75837a21c53d889ad378 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 19 Jul 2023 00:49:20 +0900 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Pieter Eendebak --- Lib/selectors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/selectors.py b/Lib/selectors.py index 106488d888ac1e..d13405963f219d 100644 --- a/Lib/selectors.py +++ b/Lib/selectors.py @@ -554,11 +554,11 @@ def select(self, timeout=None): except InterruptedError: return ready - fd_to_key = self._fd_to_key + fd_to_key_get = self._fd_to_key.get for kev in kev_list: fd = kev.ident flag = kev.filter - key = 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))