-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
Conversation
Similar technique can be adopted. Benchmarkimport timeit
import os
from selectors import SelectSelector, EVENT_WRITE, EVENT_READ
select = SelectSelector()
for _ in range(100):
r, w = os.pipe()
os.write(w, b"a")
select.register(r, EVENT_READ)
elapsed = timeit.timeit(
"selector.select()",
number=100000,
globals={"selector": select},
)
print(f"elapsed: {elapsed}")
ResultAS-IS: elapsed -> 2.051669834000222 |
cc @eendebakpt |
events |= EVENT_WRITE | ||
|
||
key = self._fd_to_key.get(fd) | ||
r = frozenset(r) |
There was a problem hiding this comment.
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
key = self._fd_to_key.get(fd) | ||
r = frozenset(r) | ||
w = frozenset(w) | ||
rw = r | w |
There was a problem hiding this comment.
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?
closes: gh-106751