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

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Jul 19, 2023

closes: gh-106751

@corona10
Copy link
Member Author

corona10 commented Jul 19, 2023

Similar technique can be adopted.

Benchmark

import 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}")

Result

AS-IS: elapsed -> 2.051669834000222
TO-BE: elapsed -> 1.9723411249997298 (1.04x faster)

@corona10
Copy link
Member Author

cc @eendebakpt

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

key = self._fd_to_key.get(fd)
r = frozenset(r)
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize XXXSelector for many iterations of the event loop
4 participants