Uh oh!
There was an error while loading. Please reload this page.
gh-106554: Replace _BaseSelectorImpl._key_from_fd with a simple get - #106555
Merged
Conversation
`_key_from_fd` re-implemented `.get()` and can be removed
bdraco
commented
Jul 12, 2023
ContributorAuthor
importtimeitimportmathimportselectimportosfromselectorsimportEpollSelector, EVENT_WRITE, EVENT_READclassOriginalEpollSelector(EpollSelector):
defselect(self, timeout=None):
iftimeoutisNone:
timeout=-1eliftimeout<=0:
timeout=0else:
# epoll_wait() has a resolution of 1 millisecond, round away# from zero to wait *at least* timeout seconds.timeout=math.ceil(timeout*1e3) *1e-3# epoll_wait() expects `maxevents` to be greater than zero;# we want to make sure that `select()` can be called when no# FD is registered.max_ev=max(len(self._fd_to_key), 1)
ready= []
try:
fd_event_list=self._selector.poll(timeout, max_ev)
exceptInterruptedError:
returnreadyforfd, eventinfd_event_list:
events=0ifevent&~select.EPOLLIN:
events|=EVENT_WRITEifevent&~select.EPOLLOUT:
events|=EVENT_READkey=self._key_from_fd(fd)
ifkey:
ready.append((key, events&key.events))
returnreadyclassNewEpollSelector(EpollSelector):
defselect(self, timeout=None):
iftimeoutisNone:
timeout=-1eliftimeout<=0:
timeout=0else:
# epoll_wait() has a resolution of 1 millisecond, round away# from zero to wait *at least* timeout seconds.timeout=math.ceil(timeout*1e3) *1e-3# epoll_wait() expects `maxevents` to be greater than zero;# we want to make sure that `select()` can be called when no# FD is registered.max_ev=max(len(self._fd_to_key), 1)
ready= []
try:
fd_event_list=self._selector.poll(timeout, max_ev)
exceptInterruptedError:
returnreadyforfd, eventinfd_event_list:
events=0ifevent&~select.EPOLLIN:
events|=EVENT_WRITEifevent&~select.EPOLLOUT:
events|=EVENT_READkey=self._fd_to_key.get(fd)
ifkey:
ready.append((key, events&key.events))
returnreadyoriginal_epoll=OriginalEpollSelector()
new_epoll=NewEpollSelector()
for_inrange(512):
r, w=os.pipe()
os.write(w, b"a")
original_epoll.register(r, EVENT_READ)
new_epoll.register(r, EVENT_READ)
original_time=timeit.timeit(
"selector.select()",
number=100000,
globals={"selector": original_epoll},
)
new_time=timeit.timeit(
"selector.select()",
number=100000,
globals={"selector": new_epoll},
)
print("original: %s"%original_time)
print("new: %s"%new_time) |
bdraco
commented
Jul 12, 2023
ContributorAuthor
512 pipes 1024 pipes |
ContributorAuthor
bdraco
commented
Jul 12, 2023
ContributorAuthor
defselect(self, timeout=None):
timeout=NoneiftimeoutisNoneelsemax(timeout, 0)
# 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/issue29255max_ev=max(len(self._fd_to_key), 1)
ready= []
try:
kev_list=self._selector.control(None, max_ev, timeout)
exceptInterruptedError:
returnreadyforkevinkev_list:
key=self._fd_to_key.get(kev.ident)
ifkey:
flag=kev.filterifflag==select.KQ_FILTER_READ:
events=EVENT_READelifflag==select.KQ_FILTER_WRITE:
events=EVENT_WRITEelse:
events=0ready.append((key, events&key.events))
returnready |
bdraco
commented
Jul 12, 2023
Uh oh!
There was an error while loading. Please reload this page.
bdraco
commented
Jul 12, 2023
Uh oh!
There was an error while loading. Please reload this page.
bdraco
commented
Jul 12, 2023
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Jul 13, 2023
bdraco
commented
Jul 13, 2023
ContributorAuthor
With #106555 (comment) the selector overhead falls below the cost of |
methane
reviewed
Jul 14, 2023
Uh oh!
There was an error while loading. Please reload this page.
bdraco
commented
Jul 14, 2023
ContributorAuthor
Thanks. Will work on the follow ups from above |
This was referenced Dec 6, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

_BaseSelectorImpl._key_from_fdreimplements.get()#106554