feat: small improvements to cache performance · python-zeroconf/python-zeroconf@4b9e3ed

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