Skip to content

Commit 499d777

Browse files
committed
gh-106754: Optimize kqueue selector of the event loop
1 parent aecf6ac commit 499d777

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

Lib/selectors.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -547,23 +547,21 @@ def select(self, timeout=None):
547547
# If max_ev is 0, kqueue will ignore the timeout. For consistent
548548
# behavior with the other selector classes, we prevent that here
549549
# (using max). See https://bugs.python.org/issue29255
550-
max_ev = max(len(self._fd_to_key), 1)
550+
max_ev = len(self._fd_to_key) or 1
551551
ready = []
552552
try:
553553
kev_list = self._selector.control(None, max_ev, timeout)
554554
except InterruptedError:
555555
return ready
556+
557+
fd_to_key = self._fd_to_key
556558
for kev in kev_list:
557559
fd = kev.ident
558560
flag = kev.filter
559-
events = 0
560-
if flag == select.KQ_FILTER_READ:
561-
events |= EVENT_READ
562-
if flag == select.KQ_FILTER_WRITE:
563-
events |= EVENT_WRITE
564-
565-
key = self._fd_to_key.get(fd)
561+
key = fd_to_key.get(fd)
566562
if key:
563+
events = ((flag == select.KQ_FILTER_READ and EVENT_READ)
564+
| (flag == select.KQ_FILTER_WRITE and EVENT_WRITE))
567565
ready.append((key, events & key.events))
568566
return ready
569567

0 commit comments

Comments
 (0)