Skip to content

Commit 5a76fc5

Browse files
authored
feat: speed up RecordManager with additional cython defs (#1242)
1 parent f8ad5a2 commit 5a76fc5

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/zeroconf/_cache.pxd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ cdef class DNSCache:
2323
cdef public cython.dict cache
2424
cdef public cython.dict service_cache
2525

26+
cpdef async_add_records(self, object entries)
27+
28+
cpdef async_remove_records(self, object entries)
29+
30+
cpdef async_get_unique(self, DNSRecord entry)
31+
2632
@cython.locals(
2733
records=cython.dict,
2834
record=DNSRecord,
@@ -33,6 +39,8 @@ cdef class DNSCache:
3339

3440
cdef _async_remove(self, DNSRecord record)
3541

42+
cpdef async_mark_unique_records_older_than_1s_to_expire(self, object unique_types, object answers, object now)
43+
3644
@cython.locals(
3745
record=DNSRecord,
3846
)

src/zeroconf/_handlers/record_manager.pxd

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ from .._protocol.incoming cimport DNSIncoming
99
cdef cython.float _DNS_PTR_MIN_TTL
1010
cdef object _ADDRESS_RECORD_TYPES
1111
cdef object RecordUpdate
12+
cdef object TYPE_CHECKING
13+
cdef object _TYPE_PTR
1214

1315
cdef class RecordManager:
1416

15-
cdef object zc
16-
cdef DNSCache cache
17-
cdef cython.list listeners
17+
cdef public object zc
18+
cdef public DNSCache cache
19+
cdef public cython.list listeners
20+
21+
cpdef async_updates(self, object now, object records)
22+
23+
cpdef async_updates_complete(self, object notify)
1824

1925
@cython.locals(
2026
cache=DNSCache,
21-
record=DNSRecord
27+
record=DNSRecord,
28+
maybe_entry=DNSRecord,
29+
now_float=cython.float
2230
)
2331
cpdef async_updates_from_response(self, DNSIncoming msg)

src/zeroconf/_handlers/record_manager.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
if TYPE_CHECKING:
3434
from .._core import Zeroconf
3535

36+
_float = float
37+
3638

3739
class RecordManager:
3840
"""Process records into the cache and notify listeners."""
@@ -45,7 +47,7 @@ def __init__(self, zeroconf: 'Zeroconf') -> None:
4547
self.cache = zeroconf.cache
4648
self.listeners: List[RecordUpdateListener] = []
4749

48-
def async_updates(self, now: float, records: List[RecordUpdate]) -> None:
50+
def async_updates(self, now: _float, records: List[RecordUpdate]) -> None:
4951
"""Used to notify listeners of new information that has updated
5052
a record.
5153
@@ -81,6 +83,7 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
8183
other_adds: List[DNSRecord] = []
8284
removes: Set[DNSRecord] = set()
8385
now = msg.now
86+
now_float = now
8487
unique_types: Set[Tuple[str, int, int]] = set()
8588
cache = self.cache
8689

@@ -108,11 +111,11 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
108111
record = cast(_UniqueRecordsType, record)
109112

110113
maybe_entry = cache.async_get_unique(record)
111-
if not record.is_expired(now):
114+
if not record.is_expired(now_float):
112115
if maybe_entry is not None:
113116
maybe_entry.reset_ttl(record)
114117
else:
115-
if record.type in _ADDRESS_RECORD_TYPES:
118+
if record_type in _ADDRESS_RECORD_TYPES:
116119
address_adds.append(record)
117120
else:
118121
other_adds.append(record)
@@ -146,7 +149,8 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
146149
new = False
147150
if other_adds or address_adds:
148151
new = cache.async_add_records(address_adds)
149-
new |= cache.async_add_records(other_adds)
152+
if cache.async_add_records(other_adds):
153+
new = True
150154
# Removes are processed last since
151155
# ServiceInfo could generate an un-needed query
152156
# because the data was not yet populated.

0 commit comments

Comments
 (0)