Skip to content

Commit 1df2e69

Browse files
authored
feat: improve performance of DNSCache backend (#1415)
1 parent 1827474 commit 1df2e69

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

src/zeroconf/_cache.pxd

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ from ._dns cimport (
1313

1414

1515
cdef object _UNIQUE_RECORD_TYPES
16-
cdef object _TYPE_PTR
16+
cdef unsigned int _TYPE_PTR
1717
cdef cython.uint _ONE_SECOND
1818

19+
@cython.locals(
20+
record_cache=dict,
21+
)
1922
cdef _remove_key(cython.dict cache, object key, DNSRecord record)
2023

2124

@@ -42,7 +45,7 @@ cdef class DNSCache:
4245
records=cython.dict,
4346
record=DNSRecord,
4447
)
45-
cpdef list async_all_by_details(self, str name, object type_, object class_)
48+
cpdef list async_all_by_details(self, str name, unsigned int type_, unsigned int class_)
4649

4750
cpdef cython.dict async_entries_with_name(self, str name)
4851

@@ -51,23 +54,35 @@ cdef class DNSCache:
5154
@cython.locals(
5255
cached_entry=DNSRecord,
5356
)
54-
cpdef DNSRecord get_by_details(self, str name, object type_, object class_)
57+
cpdef DNSRecord get_by_details(self, str name, unsigned int type_, unsigned int class_)
5558

5659
@cython.locals(
5760
records=cython.dict,
5861
entry=DNSRecord,
5962
)
60-
cpdef cython.list get_all_by_details(self, str name, object type_, object class_)
63+
cpdef cython.list get_all_by_details(self, str name, unsigned int type_, unsigned int class_)
6164

6265
@cython.locals(
6366
store=cython.dict,
67+
service_record=DNSService
6468
)
6569
cdef bint _async_add(self, DNSRecord record)
6670

71+
@cython.locals(
72+
service_record=DNSService
73+
)
6774
cdef void _async_remove(self, DNSRecord record)
6875

6976
@cython.locals(
7077
record=DNSRecord,
7178
created_double=double,
7279
)
7380
cpdef void async_mark_unique_records_older_than_1s_to_expire(self, cython.set unique_types, object answers, double now)
81+
82+
cpdef entries_with_name(self, str name)
83+
84+
@cython.locals(
85+
record=DNSRecord,
86+
now=double
87+
)
88+
cpdef current_entry_with_name_and_alias(self, str name, str alias)

src/zeroconf/_cache.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ def _remove_key(cache: _DNSRecordCacheType, key: _str, record: _DNSRecord) -> No
4949
5050
This function must be run in from event loop.
5151
"""
52-
del cache[key][record]
53-
if not cache[key]:
52+
record_cache = cache[key]
53+
del record_cache[record]
54+
if not record_cache:
5455
del cache[key]
5556

5657

@@ -81,7 +82,8 @@ def _async_add(self, record: _DNSRecord) -> bool:
8182
new = record not in store and not isinstance(record, DNSNsec)
8283
store[record] = record
8384
if isinstance(record, DNSService):
84-
self.service_cache.setdefault(record.server_key, {})[record] = record
85+
service_record = record
86+
self.service_cache.setdefault(record.server_key, {})[service_record] = service_record
8587
return new
8688

8789
def async_add_records(self, entries: Iterable[DNSRecord]) -> bool:
@@ -103,7 +105,8 @@ def _async_remove(self, record: _DNSRecord) -> None:
103105
This function must be run in from event loop.
104106
"""
105107
if isinstance(record, DNSService):
106-
_remove_key(self.service_cache, record.server_key, record)
108+
service_record = record
109+
_remove_key(self.service_cache, service_record.server_key, service_record)
107110
_remove_key(self.cache, record.key, record)
108111

109112
def async_remove_records(self, entries: Iterable[DNSRecord]) -> None:

0 commit comments

Comments
 (0)