Skip to content

Commit 1ed6bd2

Browse files
authored
feat: small speed up to writing outgoing dns records (#1258)
1 parent bb74309 commit 1ed6bd2

2 files changed

Lines changed: 36 additions & 14 deletions

File tree

src/zeroconf/_protocol/outgoing.pxd

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
import cython
33

4-
from .._dns cimport DNSEntry, DNSQuestion, DNSRecord
4+
from .._cache cimport DNSCache
5+
from .._dns cimport DNSEntry, DNSPointer, DNSQuestion, DNSRecord
56
from .incoming cimport DNSIncoming
67

78

@@ -14,6 +15,7 @@ cdef cython.uint _FLAGS_TC
1415
cdef cython.uint _MAX_MSG_ABSOLUTE
1516
cdef cython.uint _MAX_MSG_TYPICAL
1617

18+
cdef object TYPE_CHECKING
1719

1820
cdef class DNSOutgoing:
1921

@@ -70,13 +72,31 @@ cdef class DNSOutgoing:
7072
cpdef write_short(self, object value)
7173

7274
@cython.locals(
73-
questions_offset=cython.uint,
74-
answer_offset=cython.uint,
75-
authority_offset=cython.uint,
76-
additional_offset=cython.uint,
77-
questions_written=cython.uint,
78-
answers_written=cython.uint,
79-
authorities_written=cython.uint,
80-
additionals_written=cython.uint,
75+
questions_offset=object,
76+
answer_offset=object,
77+
authority_offset=object,
78+
additional_offset=object,
79+
questions_written=object,
80+
answers_written=object,
81+
authorities_written=object,
82+
additionals_written=object,
8183
)
8284
cdef _packets(self)
85+
86+
cpdef add_question_or_all_cache(self, DNSCache cache, object now, str name, object type_, object class_)
87+
88+
cpdef add_question_or_one_cache(self, DNSCache cache, object now, str name, object type_, object class_)
89+
90+
cpdef add_question(self, DNSQuestion question)
91+
92+
cpdef add_answer(self, DNSIncoming inp, DNSRecord record)
93+
94+
cpdef add_answer_at_time(self, DNSRecord record, object now)
95+
96+
cpdef add_authorative_answer(self, DNSPointer record)
97+
98+
cpdef add_additional_answer(self, DNSRecord record)
99+
100+
cpdef is_query(self)
101+
102+
cpdef is_response(self)

src/zeroconf/_protocol/outgoing.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import enum
2424
import logging
25-
from typing import Dict, List, Optional, Sequence, Tuple, Union
25+
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union
2626

2727
from .._cache import DNSCache
2828
from .._dns import DNSPointer, DNSQuestion, DNSRecord
@@ -176,7 +176,7 @@ def add_additional_answer(self, record: DNSRecord) -> None:
176176
self.additionals.append(record)
177177

178178
def add_question_or_one_cache(
179-
self, cache: DNSCache, now: float, name: str, type_: int, class_: int
179+
self, cache: DNSCache, now: float_, name: str_, type_: int_, class_: int_
180180
) -> None:
181181
"""Add a question if it is not already cached."""
182182
cached_entry = cache.get_by_details(name, type_, class_)
@@ -186,7 +186,7 @@ def add_question_or_one_cache(
186186
self.add_answer_at_time(cached_entry, now)
187187

188188
def add_question_or_all_cache(
189-
self, cache: DNSCache, now: float, name: str, type_: int, class_: int
189+
self, cache: DNSCache, now: float_, name: str_, type_: int_, class_: int_
190190
) -> None:
191191
"""Add a question if it is not already cached.
192192
This is currently only used for IPv6 addresses.
@@ -223,7 +223,8 @@ def _write_int(self, value: Union[float, int]) -> None:
223223

224224
def write_string(self, value: bytes) -> None:
225225
"""Writes a string to the packet"""
226-
assert isinstance(value, bytes)
226+
if TYPE_CHECKING:
227+
assert isinstance(value, bytes)
227228
self.data.append(value)
228229
self.size += len(value)
229230

@@ -237,7 +238,8 @@ def _write_utf(self, s: str) -> None:
237238
self.write_string(utfstr)
238239

239240
def write_character_string(self, value: bytes) -> None:
240-
assert isinstance(value, bytes)
241+
if TYPE_CHECKING:
242+
assert isinstance(value, bytes)
241243
length = len(value)
242244
if length > 256:
243245
raise NamePartTooLongException

0 commit comments

Comments
 (0)