KEMBAR78
gh-106751: Optimize SelectSelector.select() for many iteration case by corona10 · Pull Request #106879 · python/cpython · GitHub
Skip to content

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