From 683a66913d6b77eb5351f27ac92533d4d285e7f9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 Jun 2023 14:54:38 -0500 Subject: [PATCH 1/3] fix: reduce debug logging overhead by adding missing checks to datagram_received I only noticed these were missing when I ran the profiler on a system with IPv6 --- src/zeroconf/_core.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/zeroconf/_core.py b/src/zeroconf/_core.py index b29fc790..7067466f 100644 --- a/src/zeroconf/_core.py +++ b/src/zeroconf/_core.py @@ -282,18 +282,20 @@ def datagram_received( else: # https://github.com/python/mypy/issues/1178 addr, port, flow, scope = addrs # type: ignore - log.debug('IPv6 scope_id %d associated to the receiving interface', scope) + if debug: + log.debug('IPv6 scope_id %d associated to the receiving interface', scope) v6_flow_scope = (flow, scope) if data_len > _MAX_MSG_ABSOLUTE: # Guard against oversized packets to ensure bad implementations cannot overwhelm # the system. - log.debug( - "Discarding incoming packet with length %s, which is larger " - "than the absolute maximum size of %s", - data_len, - _MAX_MSG_ABSOLUTE, - ) + if debug: + log.debug( + "Discarding incoming packet with length %s, which is larger " + "than the absolute maximum size of %s", + data_len, + _MAX_MSG_ABSOLUTE, + ) return now = current_time_millis() From 12ded4a53009c9bdc98e6ed0195ed77ff8dfab98 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 Jun 2023 14:59:51 -0500 Subject: [PATCH 2/3] feat: reorder incoming data parsing to reduce overhead --- src/zeroconf/_core.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/zeroconf/_core.py b/src/zeroconf/_core.py index 7067466f..0e285227 100644 --- a/src/zeroconf/_core.py +++ b/src/zeroconf/_core.py @@ -275,17 +275,6 @@ def datagram_received( data_len = len(data) debug = log.isEnabledFor(logging.DEBUG) - if len(addrs) == 2: - # https://github.com/python/mypy/issues/1178 - addr, port = addrs # type: ignore - scope = None - else: - # https://github.com/python/mypy/issues/1178 - addr, port, flow, scope = addrs # type: ignore - if debug: - log.debug('IPv6 scope_id %d associated to the receiving interface', scope) - v6_flow_scope = (flow, scope) - if data_len > _MAX_MSG_ABSOLUTE: # Guard against oversized packets to ensure bad implementations cannot overwhelm # the system. @@ -308,15 +297,25 @@ def datagram_received( # Guard against duplicate packets if debug: log.debug( - 'Ignoring duplicate message with no unicast questions received from %r:%r [socket %s] (%d bytes) as [%r]', - addr, - port, + 'Ignoring duplicate message with no unicast questions received from %s [socket %s] (%d bytes) as [%r]', + addrs, self.sock_description, data_len, data, ) return + if len(addrs) == 2: + # https://github.com/python/mypy/issues/1178 + addr, port = addrs # type: ignore + scope = None + else: + # https://github.com/python/mypy/issues/1178 + addr, port, flow, scope = addrs # type: ignore + if debug: + log.debug('IPv6 scope_id %d associated to the receiving interface', scope) + v6_flow_scope = (flow, scope) + msg = DNSIncoming(data, (addr, port), scope, now) self.data = data self.last_time = now From 0efa08d5bec02a5018a2f0f64b988e871f0aee48 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 Jun 2023 15:17:26 -0500 Subject: [PATCH 3/3] fix: incorrect merge resolution --- src/zeroconf/_core.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/zeroconf/_core.py b/src/zeroconf/_core.py index a4cad468..1f74dcd5 100644 --- a/src/zeroconf/_core.py +++ b/src/zeroconf/_core.py @@ -271,21 +271,9 @@ def datagram_received( self, data: bytes, addrs: Union[Tuple[str, int], Tuple[str, int, int, int]] ) -> None: assert self.transport is not None - v6_flow_scope: Union[Tuple[()], Tuple[int, int]] = () data_len = len(data) debug = log.isEnabledFor(logging.DEBUG) - if len(addrs) == 2: - # https://github.com/python/mypy/issues/1178 - addr, port = addrs # type: ignore - scope = None - else: - # https://github.com/python/mypy/issues/1178 - addr, port, flow, scope = addrs # type: ignore - if debug: - log.debug('IPv6 scope_id %d associated to the receiving interface', scope) - v6_flow_scope = (flow, scope) - if data_len > _MAX_MSG_ABSOLUTE: # Guard against oversized packets to ensure bad implementations cannot overwhelm # the system. @@ -316,6 +304,7 @@ def datagram_received( ) return + v6_flow_scope: Union[Tuple[()], Tuple[int, int]] = () if len(addrs) == 2: # https://github.com/python/mypy/issues/1178 addr, port = addrs # type: ignore