Skip to content

Commit ebbb2af

Browse files
feat: improve performance of processing incoming records (#1467)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 45c82d1 commit ebbb2af

4 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/zeroconf/_handlers/record_manager.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ from .._dns cimport DNSQuestion, DNSRecord
66
from .._protocol.incoming cimport DNSIncoming
77
from .._updates cimport RecordUpdateListener
88
from .._utils.time cimport current_time_millis
9-
9+
from .._record_update cimport RecordUpdate
1010

1111
cdef cython.float _DNS_PTR_MIN_TTL
1212
cdef cython.uint _TYPE_PTR
1313
cdef object _ADDRESS_RECORD_TYPES
14-
cdef object RecordUpdate
1514
cdef bint TYPE_CHECKING
1615
cdef object _TYPE_PTR
1716

@@ -31,6 +30,7 @@ cdef class RecordManager:
3130
record=DNSRecord,
3231
answers=cython.list,
3332
maybe_entry=DNSRecord,
33+
rec_update=RecordUpdate
3434
)
3535
cpdef void async_updates_from_response(self, DNSIncoming msg)
3636

src/zeroconf/_handlers/record_manager.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
120120
address_adds.append(record)
121121
else:
122122
other_adds.append(record)
123-
updates.append(RecordUpdate(record, maybe_entry))
123+
rec_update = RecordUpdate.__new__(RecordUpdate)
124+
rec_update._fast_init(record, maybe_entry)
125+
updates.append(rec_update)
124126
# This is likely a goodbye since the record is
125127
# expired and exists in the cache
126128
elif maybe_entry is not None:
127-
updates.append(RecordUpdate(record, maybe_entry))
129+
rec_update = RecordUpdate.__new__(RecordUpdate)
130+
rec_update._fast_init(record, maybe_entry)
131+
updates.append(rec_update)
128132
removes.add(record)
129133

130134
if unique_types:

src/zeroconf/_record_update.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ cdef class RecordUpdate:
88

99
cdef public DNSRecord new
1010
cdef public DNSRecord old
11+
12+
cdef void _fast_init(self, object new, object old)

src/zeroconf/_record_update.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@
2424

2525
from ._dns import DNSRecord
2626

27+
_DNSRecord = DNSRecord
28+
2729

2830
class RecordUpdate:
2931
__slots__ = ("new", "old")
3032

31-
def __init__(self, new: DNSRecord, old: Optional[DNSRecord] = None):
33+
def __init__(self, new: DNSRecord, old: Optional[DNSRecord] = None) -> None:
3234
"""RecordUpdate represents a change in a DNS record."""
35+
self._fast_init(new, old)
36+
37+
def _fast_init(self, new: _DNSRecord, old: Optional[_DNSRecord]) -> None:
38+
"""Fast init for RecordUpdate."""
3339
self.new = new
3440
self.old = old
3541

0 commit comments

Comments
 (0)