8000 feat: optimize construction of outgoing dns records (#1182) · python-zeroconf/python-zeroconf@fc0341f · GitHub
[go: up one dir, main page]

Skip to content

Commit fc0341f

Browse files
authored
feat: optimize construction of outgoing dns records (#1182)
1 parent efa1e45 commit fc0341f

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

build_ext.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def build(setup_kwargs: Any) -> None:
2323
dict(
2424
ext_modules=cythonize(
2525
[
26-
"src/zeroconf/_cache.py",
2726
"src/zeroconf/_dns.py",
27+
"src/zeroconf/_cache.py",
2828
"src/zeroconf/_protocol/incoming.py",
2929
"src/zeroconf/_protocol/outgoing.py",
3030
],

src/zeroconf/_protocol/outgoing.pxd

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import cython
33

4+
from .._dns cimport DNSEntry, DNSQuestion, DNSRecord
45
from .incoming cimport DNSIncoming
56

67

@@ -41,7 +42,14 @@ cdef class DNSOutgoing:
4142

4243
cdef _write_int(self, object value)
4344

44-
cdef _write_question(self, object question)
45+
cdef _write_question(self, DNSQuestion question)
46+
47+
@cython.locals(
48+
d=cython.bytes,
49+
data_view=cython.list,
50+
length=cython.uint
51+
)
52+
cdef _write_record(self, DNSRecord record, object now)
4553

4654
cdef _write_record_class(self, object record)
4755

@@ -55,6 +63,12 @@ cdef class DNSOutgoing:
5563

5664
cdef _has_more_to_add(self, object questions_offset, object answer_offset, object authority_offset, object additional_offset)
5765

66+
cdef _write_ttl(self, DNSRecord record, object now)
67+
68+
cpdef write_name(self, object name)
69+
70+
cpdef write_short(self, object value)
71+
5872
@cython.locals(
5973
questions_offset=cython.uint,
6074
answer_offset=cython.uint,

src/zeroconf/_protocol/outgoing.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
)
4141
from .incoming import DNSIncoming
4242

43+
str_ = str
44+
float_ = float
45+
DNSQuestion_ = DNSQuestion
46+
DNSRecord_ = DNSRecord
47+
4348

4449
class State(enum.Enum):
4550
init = 0
@@ -238,7 +243,7 @@ def write_character_string(self, value: bytes) -> None:
238243
self._write_byte(length)
239244
self.write_string(value)
240245

241-
def write_name(self, name: str) -> None:
246+
def write_name(self, name: str_) -> None:
242247
"""
243248
Write names to packet
244249
@@ -276,26 +281,26 @@ def write_name(self, name: str) -> None:
276281
# this is the end of a name
277282
self._write_byte(0)
278283

279-
def _write_question(self, question: DNSQuestion) -> bool:
284+
def _write_question(self, question: DNSQuestion_) -> bool:
280285
"""Writes a question to the packet"""
281286
start_data_length, start_size = len(self.data), self.size
282287
self.write_name(question.name)
283288
self.write_short(question.type)
284289
self._write_record_class(question)
285290
return self._check_data_limit_or_rollback(start_data_length, start_size)
286291

287-
def _write_record_class(self, record: Union[DNSQuestion, DNSRecord]) -> None:
292+
def _write_record_class(self, record: Union[DNSQuestion_, DNSRecord_]) -> None:
288293
"""Write out the record class including the unique/unicast (QU) bit."""
289294
if record.unique and self.multicast:
290295
self.write_short(record.class_ | _CLASS_UNIQUE)
291296
else:
292297
self.write_short(record.class_)
293298

294-
def _write_ttl(self, record: DNSRecord, now: float) -> None:
299+
def _write_ttl(self, record: DNSRecord_, now: float_) -> None:
295300
"""Write out the record ttl."""
296301
self._write_int(record.ttl if now == 0 else record.get_remaining_ttl(now))
297302

298-
def _write_record(self, record: DNSRecord, now: float) -> bool:
303+
def _write_record(self, record: DNSRecord_, now: float_) -> bool:
299304
"""Writes a record (answer, authoritative answer, additional) to
300305
the packet. Returns True on success, or False if we did not
301306
because the packet because the record does not fit."""
@@ -308,7 +313,9 @@ def _write_record(self, record: DNSRecord, now: float) -> bool:
308313
self.write_short(0) # Will get replaced with the actual size
309314
record.write(self)
310315
# Adjust size for the short we will write before this record
311-
length = sum(len(d) for d in self.data[index + 1 :])
316+
length = 0
317+
for d in self.data[index + 1 :]:
318+
length += len(d)
312319
# Here we replace the 0 length short we wrote
313320
# before with the actual length
314321
self._replace_short(index, length)

0 commit comments

Comments
 (0)
0