Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions Lib/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: on my system frozenset is slightly slower than set, but the difference is very small

w = frozenset(w)
rw = r | w
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rw is only used once. You assigned r | w to a variable for readability?

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Optimize :meth:`SelectSelector.select` for many iteration case. Patch By
Dong-hee Na.