Skip to content

Commit f459856

Browse files
authored
feat: add cython pxd file for _listener.py to improve incoming message processing performance (#1221)
1 parent 1901fb4 commit f459856

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/zeroconf/_listener.pxd

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
import cython
3+
4+
5+
cdef object millis_to_seconds
6+
cdef object log
7+
cdef object logging_DEBUG
8+
9+
from ._protocol.incoming cimport DNSIncoming
10+
11+
12+
cdef class AsyncListener:
13+
14+
cdef public object zc
15+
cdef public cython.bytes data
16+
cdef public cython.float last_time
17+
cdef public DNSIncoming last_message
18+
cdef public object transport
19+
cdef public object sock_description
20+
cdef public cython.dict _deferred
21+
cdef public cython.dict _timers
22+
23+
@cython.locals(now=cython.float, msg=DNSIncoming)
24+
cpdef datagram_received(self, cython.bytes bytes, cython.tuple addrs)

src/zeroconf/_listener.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
_TC_DELAY_RANDOM_INTERVAL = (400, 500)
3838

3939

40+
_bytes = bytes
41+
42+
logging_DEBUG = logging.DEBUG
43+
44+
4045
class AsyncListener:
4146

4247
"""A Listener is used by this module to listen on the multicast
@@ -69,11 +74,11 @@ def __init__(self, zc: 'Zeroconf') -> None:
6974
super().__init__()
7075

7176
def datagram_received(
72-
self, data: bytes, addrs: Union[Tuple[str, int], Tuple[str, int, int, int]]
77+
self, data: _bytes, addrs: Union[Tuple[str, int], Tuple[str, int, int, int]]
7378
) -> None:
7479
assert self.transport is not None
7580
data_len = len(data)
76-
debug = log.isEnabledFor(logging.DEBUG)
81+
debug = log.isEnabledFor(logging_DEBUG)
7782

7883
if data_len > _MAX_MSG_ABSOLUTE:
7984
# Guard against oversized packets to ensure bad implementations cannot overwhelm

0 commit comments

Comments
 (0)