From f459856a0a61b8afa8a541926d7e15d51f8e4aea Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 14 Aug 2023 12:24:47 -0500 Subject: [PATCH] feat: add cython pxd file for _listener.py to improve incoming message processing performance (#1221) --- src/zeroconf/_listener.pxd | 24 ++++++++++++++++++++++++ src/zeroconf/_listener.py | 9 +++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/zeroconf/_listener.pxd diff --git a/src/zeroconf/_listener.pxd b/src/zeroconf/_listener.pxd new file mode 100644 index 00000000..0f32a44a --- /dev/null +++ b/src/zeroconf/_listener.pxd @@ -0,0 +1,24 @@ + +import cython + + +cdef object millis_to_seconds +cdef object log +cdef object logging_DEBUG + +from ._protocol.incoming cimport DNSIncoming + + +cdef class AsyncListener: + + cdef public object zc + cdef public cython.bytes data + cdef public cython.float last_time + cdef public DNSIncoming last_message + cdef public object transport + cdef public object sock_description + cdef public cython.dict _deferred + cdef public cython.dict _timers + + @cython.locals(now=cython.float, msg=DNSIncoming) + cpdef datagram_received(self, cython.bytes bytes, cython.tuple addrs) diff --git a/src/zeroconf/_listener.py b/src/zeroconf/_listener.py index 97bcf007..bc0af296 100644 --- a/src/zeroconf/_listener.py +++ b/src/zeroconf/_listener.py @@ -37,6 +37,11 @@ _TC_DELAY_RANDOM_INTERVAL = (400, 500) +_bytes = bytes + +logging_DEBUG = logging.DEBUG + + class AsyncListener: """A Listener is used by this module to listen on the multicast @@ -69,11 +74,11 @@ def __init__(self, zc: 'Zeroconf') -> None: super().__init__() def datagram_received( - self, data: bytes, addrs: Union[Tuple[str, int], Tuple[str, int, int, int]] + self, data: _bytes, addrs: Union[Tuple[str, int], Tuple[str, int, int, int]] ) -> None: assert self.transport is not None data_len = len(data) - debug = log.isEnabledFor(logging.DEBUG) + debug = log.isEnabledFor(logging_DEBUG) if data_len > _MAX_MSG_ABSOLUTE: # Guard against oversized packets to ensure bad implementations cannot overwhelm