Skip to content

Commit

Permalink
feat: improve dns cache performance (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed May 25, 2023
1 parent 55c879c commit bb496a1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/zeroconf/_cache.pxd
@@ -1,4 +1,5 @@
import cython

from ._dns cimport (
DNSAddress,
DNSEntry,
Expand All @@ -10,6 +11,7 @@ from ._dns cimport (
)


cdef object _UNIQUE_RECORD_TYPES
cdef object _TYPE_PTR

cdef _remove_key(cython.dict cache, object key, DNSRecord record)
Expand Down
11 changes: 7 additions & 4 deletions src/zeroconf/_cache.py
Expand Up @@ -134,14 +134,17 @@ def async_get_unique(self, entry: _UniqueRecordsType) -> Optional[DNSRecord]:
return None
return store.get(entry)

def async_all_by_details(self, name: str, type_: int, class_: int) -> Iterator[DNSRecord]:
def async_all_by_details(self, name: _str, type_: int, class_: int) -> Iterator[DNSRecord]:
"""Gets all matching entries by details.
This function is not threadsafe and must be called from
the event loop.
"""
key = name.lower()
for entry in self.cache.get(key, []):
records = self.cache.get(key)
if records is None:
return
for entry in records:
if _dns_record_matches(entry, key, type_, class_):
yield entry

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

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

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

0 comments on commit bb496a1

Please sign in to comment.