From bb496a1dd5fa3562c0412cb064d14639a542592e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 25 May 2023 07:45:03 -0500 Subject: [PATCH] feat: improve dns cache performance (#1172) --- src/zeroconf/_cache.pxd | 2 ++ src/zeroconf/_cache.py | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/zeroconf/_cache.pxd b/src/zeroconf/_cache.pxd index acf4029e..ea436be7 100644 --- a/src/zeroconf/_cache.pxd +++ b/src/zeroconf/_cache.pxd @@ -1,4 +1,5 @@ import cython + from ._dns cimport ( DNSAddress, DNSEntry, @@ -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) diff --git a/src/zeroconf/_cache.py b/src/zeroconf/_cache.py index f022c2cb..49f92f91 100644 --- a/src/zeroconf/_cache.py +++ b/src/zeroconf/_cache.py @@ -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 @@ -151,7 +154,7 @@ 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. @@ -159,7 +162,7 @@ def async_entries_with_server(self, name: str) -> Dict[DNSRecord, DNSRecord]: 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