Skip to content

Commit 0fc031b

Browse files
authored
feat: small improvements to ServiceBrowser performance (#1283)
1 parent 29d694a commit 0fc031b

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/zeroconf/_services/browser.pxd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ from .._utils.time cimport current_time_millis, millis_to_seconds
1010
cdef bint TYPE_CHECKING
1111
cdef object cached_possible_types
1212
cdef cython.uint _EXPIRE_REFRESH_TIME_PERCENT
13+
cdef cython.uint _TYPE_PTR
1314
cdef object SERVICE_STATE_CHANGE_ADDED, SERVICE_STATE_CHANGE_REMOVED, SERVICE_STATE_CHANGE_UPDATED
1415

1516
cdef class _DNSPointerOutgoingBucket:
@@ -58,7 +59,7 @@ cdef class _ServiceBrowserBase(RecordUpdateListener):
5859

5960
cpdef _enqueue_callback(self, object state_change, object type_, object name)
6061

61-
@cython.locals(record=DNSRecord, cache=DNSCache, service=DNSRecord)
62+
@cython.locals(record=DNSRecord, cache=DNSCache, service=DNSRecord, pointer=DNSPointer)
6263
cpdef async_update_records(self, object zc, cython.float now, cython.list records)
6364

6465
cpdef _names_matching_types(self, object types)

src/zeroconf/_services/browser.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,24 +404,26 @@ def async_update_records(self, zc: 'Zeroconf', now: float_, records: List[Record
404404
This method will be run in the event loop.
405405
"""
406406
for record_update in records:
407-
record, old_record = record_update
407+
record = record_update[0]
408+
old_record = record_update[1]
408409
record_type = record.type
409410

410411
if record_type is _TYPE_PTR:
411412
if TYPE_CHECKING:
412413
record = cast(DNSPointer, record)
413-
for type_ in self.types.intersection(cached_possible_types(record.name)):
414+
pointer = record
415+
for type_ in self.types.intersection(cached_possible_types(pointer.name)):
414416
if old_record is None:
415-
self._enqueue_callback(SERVICE_STATE_CHANGE_ADDED, type_, record.alias)
416-
elif record.is_expired(now):
417-
self._enqueue_callback(SERVICE_STATE_CHANGE_REMOVED, type_, record.alias)
417+
self._enqueue_callback(SERVICE_STATE_CHANGE_ADDED, type_, pointer.alias)
418+
elif pointer.is_expired(now):
419+
self._enqueue_callback(SERVICE_STATE_CHANGE_REMOVED, type_, pointer.alias)
418420
else:
419-
expire_time = record.get_expiration_time(_EXPIRE_REFRESH_TIME_PERCENT)
421+
expire_time = pointer.get_expiration_time(_EXPIRE_REFRESH_TIME_PERCENT)
420422
self.reschedule_type(type_, now, expire_time)
421423
continue
422424

423425
# If its expired or already exists in the cache it cannot be updated.
424-
if old_record or record.is_expired(now):
426+
if old_record or record.is_expired(now) is True:
425427
continue
426428

427429
if record_type in _ADDRESS_RECORD_TYPES:

0 commit comments

Comments
 (0)