Skip to content

Commit 126849c

Browse files
authored
feat: speed up processing incoming records (#1206)
1 parent 1310f12 commit 126849c

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/zeroconf/_dns.pxd

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
import cython
33

4+
from ._protocol.incoming cimport DNSIncoming
5+
46

57
cdef object _LEN_BYTE
68
cdef object _LEN_SHORT
@@ -9,9 +11,9 @@ cdef object _LEN_INT
911
cdef object _NAME_COMPRESSION_MIN_SIZE
1012
cdef object _BASE_MAX_SIZE
1113

12-
cdef object _EXPIRE_FULL_TIME_MS
13-
cdef object _EXPIRE_STALE_TIME_MS
14-
cdef object _RECENT_TIME_MS
14+
cdef cython.uint _EXPIRE_FULL_TIME_MS
15+
cdef cython.uint _EXPIRE_STALE_TIME_MS
16+
cdef cython.uint _RECENT_TIME_MS
1517

1618
cdef object _CLASS_UNIQUE
1719
cdef object _CLASS_MASK
@@ -34,11 +36,25 @@ cdef class DNSQuestion(DNSEntry):
3436

3537
cdef class DNSRecord(DNSEntry):
3638

37-
cdef public object ttl
38-
cdef public object created
39+
cdef public cython.float ttl
40+
cdef public cython.float created
3941

4042
cdef _suppressed_by_answer(self, DNSRecord answer)
4143

44+
@cython.locals(
45+
answers=cython.list,
46+
)
47+
cpdef suppressed_by(self, DNSIncoming msg)
48+
49+
cpdef get_expiration_time(self, cython.uint percent)
50+
51+
cpdef is_expired(self, cython.float now)
52+
53+
cpdef is_stale(self, cython.float now)
54+
55+
cpdef is_recent(self, cython.float now)
56+
57+
cpdef reset_ttl(self, DNSRecord other)
4258

4359
cdef class DNSAddress(DNSRecord):
4460

src/zeroconf/_dns.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
_EXPIRE_STALE_TIME_MS = 500
4141
_RECENT_TIME_MS = 250
4242

43+
_float = float
44+
_int = int
4345

4446
if TYPE_CHECKING:
4547
from ._protocol.incoming import DNSIncoming
@@ -172,32 +174,36 @@ def __eq__(self, other: Any) -> bool: # pylint: disable=no-self-use
172174
def suppressed_by(self, msg: 'DNSIncoming') -> bool:
173175
"""Returns true if any answer in a message can suffice for the
174176
information held in this record."""
175-
return any(self._suppressed_by_answer(record) for record in msg.answers)
177+
answers = msg.answers
178+
for record in answers:
179+
if self._suppressed_by_answer(record):
180+
return True
181+
return False
176182

177-
def _suppressed_by_answer(self, other) -> bool: # type: ignore[no-untyped-def]
183+
def _suppressed_by_answer(self, other: 'DNSRecord') -> bool:
178184
"""Returns true if another record has same name, type and class,
179185
and if its TTL is at least half of this record's."""
180186
return self == other and other.ttl > (self.ttl / 2)
181187

182-
def get_expiration_time(self, percent: int) -> float:
188+
def get_expiration_time(self, percent: _int) -> float:
183189
"""Returns the time at which this record will have expired
184190
by a certain percentage."""
185191
return self.created + (percent * self.ttl * 10)
186192

187193
# TODO: Switch to just int here
188-
def get_remaining_ttl(self, now: float) -> Union[int, float]:
194+
def get_remaining_ttl(self, now: _float) -> Union[int, float]:
189195
"""Returns the remaining TTL in seconds."""
190196
return max(0, millis_to_seconds((self.created + (_EXPIRE_FULL_TIME_MS * self.ttl)) - now))
191197

192-
def is_expired(self, now: float) -> bool:
198+
def is_expired(self, now: _float) -> bool:
193199
"""Returns true if this record has expired."""
194200
return self.created + (_EXPIRE_FULL_TIME_MS * self.ttl) <= now
195201

196-
def is_stale(self, now: float) -> bool:
202+
def is_stale(self, now: _float) -> bool:
197203
"""Returns true if this record is at least half way expired."""
198204
return self.created + (_EXPIRE_STALE_TIME_MS * self.ttl) <= now
199205

200-
def is_recent(self, now: float) -> bool:
206+
def is_recent(self, now: _float) -> bool:
201207
"""Returns true if the record more than one quarter of its TTL remaining."""
202208
return self.created + (_RECENT_TIME_MS * self.ttl) > now
203209

0 commit comments

Comments
 (0)