Skip to content

Commit 72081f4

Browse files
committed
feat: small speed ups to incoming data parser
1 parent c5a1eaa commit 72081f4

3 files changed

Lines changed: 6 additions & 11 deletions

File tree

src/zeroconf/_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def datagram_received(
301301
self.data == data
302302
and (now - _DUPLICATE_PACKET_SUPPRESSION_INTERVAL) < self.last_time
303303
and self.last_message is not None
304-
and not self.last_message.has_qu_question()
304+
and not self.last_message.has_qu_question
305305
):
306306
# Guard against duplicate packets
307307
if debug:

src/zeroconf/_protocol/incoming.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ cdef class DNSIncoming:
6464
cdef public object now
6565
cdef public object scope_id
6666
cdef public object source
67+
cdef public cython.bint has_qu_question
6768

6869
@cython.locals(
6970
question=DNSQuestion

src/zeroconf/_protocol/incoming.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class DNSIncoming:
9090
'now',
9191
'scope_id',
9292
'source',
93+
'has_qu_question',
9394
)
9495

9596
def __init__(
@@ -117,6 +118,7 @@ def __init__(
117118
self.now = now or current_time_millis()
118119
self.source = source
119120
self.scope_id = scope_id
121+
self.has_qu_question = False
120122
try:
121123
self._initial_parse()
122124
except DECODE_EXCEPTIONS:
@@ -135,16 +137,6 @@ def is_response(self) -> bool:
135137
"""Returns true if this is a response."""
136138
return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE
137139

138-
def has_qu_question(self) -> bool:
139-
"""Returns true if any question is a QU question."""
140-
if not self.num_questions:
141-
return False
142-
for question in self.questions:
143-
# QU questions use the same bit as unique
144-
if question.unique:
145-
return True
146-
return False
147-
148140
@property
149141
def truncated(self) -> bool:
150142
"""Returns true if this is a truncated."""
@@ -223,6 +215,8 @@ def _read_questions(self) -> None:
223215
type_, class_ = UNPACK_HH(self.data, self.offset)
224216
self.offset += 4
225217
question = DNSQuestion(name, type_, class_)
218+
if not self.has_qu_question and question.unique:
219+
self.has_qu_question = True
226220
self.questions.append(question)
227221

228222
def _read_character_string(self) -> bytes:

0 commit comments

Comments
 (0)