Skip to content

Commit bb496a1

Browse files
authored
feat: improve dns cache performance (#1172)
1 parent 55c879c commit bb496a1

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/zeroconf/_cache.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cython
2+
23
from ._dns cimport (
34
DNSAddress,
45
DNSEntry,
@@ -10,6 +11,7 @@ from ._dns cimport (
1011
)
1112

1213

14+
cdef object _UNIQUE_RECORD_TYPES
1315
cdef object _TYPE_PTR
1416

1517
cdef _remove_key(cython.dict cache, object key, DNSRecord record)

src/zeroconf/_cache.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,17 @@ def async_get_unique(self, entry: _UniqueRecordsType) -> Optional[DNSRecord]:
134134
return None
135135
return store.get(entry)
136136

137-
def async_all_by_details(self, name: str, type_: int, class_: int) -> Iterator[DNSRecord]:
137+
def async_all_by_details(self, name: _str, type_: int, class_: int) -> Iterator[DNSRecord]:
138138
"""Gets all matching entries by details.
139139
140140
This function is not threadsafe and must be called from
141141
the event loop.
142142
"""
143143
key = name.lower()
144-
for entry in self.cache.get(key, []):
144+
records = self.cache.get(key)
145+
if records is None:
146+
return
147+
for entry in records:
145148
if _dns_record_matches(entry, key, type_, class_):
146149
yield entry
147150

@@ -151,15 +154,15 @@ def async_entries_with_name(self, name: str) -> Dict[DNSRecord, DNSRecord]:
151154
This function is not threadsafe and must be called from
152155
the event loop.
153156
"""
154-
return self.cache.get(name.lower(), {})
157+
return self.cache.get(name.lower()) or {}
155158

156159
def async_entries_with_server(self, name: str) -> Dict[DNSRecord, DNSRecord]:
157160
"""Returns a dict of entries whose key matches the server.
158161
159162
This function is not threadsafe and must be called from
160163
the event loop.
161164
"""
162-
return self.service_cache.get(name.lower(), {})
165+
return self.service_cache.get(name.lower()) or {}
163166

164167
# The below functions are threadsafe and do not need to be run in the
165168
# event loop, however they all make copies so they significantly

0 commit comments

Comments
 (0)