Skip to content

Commit 11f3f0e

Browse files
authored
feat: optimize incoming parser by reducing call stack (#1116)
1 parent bef9194 commit 11f3f0e

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/zeroconf/_protocol/incoming.pxd

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ cdef class DNSIncoming:
4242
cdef public object questions
4343
cdef object _answers
4444
cdef public object id
45-
cdef public object num_questions
46-
cdef public object num_answers
47-
cdef public object num_authorities
48-
cdef public object num_additionals
45+
cdef public cython.uint num_questions
46+
cdef public cython.uint num_answers
47+
cdef public cython.uint num_authorities
48+
cdef public cython.uint num_additionals
4949
cdef public object valid
5050
cdef public object now
5151
cdef public object scope_id
@@ -61,6 +61,14 @@ cdef class DNSIncoming:
6161

6262
cdef _read_header(self)
6363

64+
cdef _initial_parse(self)
65+
66+
@cython.locals(
67+
end=cython.uint,
68+
length=cython.uint
69+
)
70+
cdef _read_others(self)
71+
6472
cdef _read_questions(self)
6573

6674
@cython.locals(
@@ -73,7 +81,7 @@ cdef class DNSIncoming:
7381
@cython.locals(
7482
name_start=cython.uint
7583
)
76-
cdef _read_record(self, object domain, unsigned int type_, object class_, object ttl, object length)
84+
cdef _read_record(self, object domain, unsigned int type_, object class_, object ttl, unsigned int length)
7785

7886
cdef _read_bitmap(self, unsigned int end)
7987

src/zeroconf/_protocol/incoming.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,15 @@ def __init__(
116116
self.now = now or current_time_millis()
117117
self.source = source
118118
self.scope_id = scope_id
119-
self._parse_data(self._initial_parse)
119+
try:
120+
self._initial_parse()
121+
except DECODE_EXCEPTIONS:
122+
self._log_exception_debug(
123+
'Received invalid packet from %s at offset %d while unpacking %r',
124+
self.source,
125+
self.offset,
126+
self.data,
127+
)
120128

121129
def is_query(self) -> bool:
122130
"""Returns true if this is a query."""
@@ -139,18 +147,6 @@ def _initial_parse(self) -> None:
139147
self._read_others()
140148
self.valid = True
141149

142-
def _parse_data(self, parser_call: Callable) -> None:
143-
"""Parse part of the packet and catch exceptions."""
144-
try:
145-
parser_call()
146-
except DECODE_EXCEPTIONS:
147-
self._log_exception_debug(
148-
'Received invalid packet from %s at offset %d while unpacking %r',
149-
self.source,
150-
self.offset,
151-
self.data,
152-
)
153-
154150
@classmethod
155151
def _log_exception_debug(cls, *logger_data: Any) -> None:
156152
log_exc_info = False
@@ -166,7 +162,15 @@ def _log_exception_debug(cls, *logger_data: Any) -> None:
166162
def answers(self) -> List[DNSRecord]:
167163
"""Answers in the packet."""
168164
if not self._did_read_others:
169-
self._parse_data(self._read_others)
165+
try:
166+
self._read_others()
167+
except DECODE_EXCEPTIONS:
168+
self._log_exception_debug(
169+
'Received invalid packet from %s at offset %d while unpacking %r',
170+
self.source,
171+
self.offset,
172+
self.data,
173+
)
170174
return self._answers
171175

172176
def __repr__(self) -> str:

0 commit comments

Comments
 (0)