8000 feat: reduce overhead to handle queries and responses (#1184) · python-zeroconf/python-zeroconf@81126b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81126b7

Browse files
authored
feat: reduce overhead to handle queries and responses (#1184)
- adds slots to handler classes - avoid any expression overhead and inline instead
1 parent 1a1036d commit 81126b7

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/zeroconf/_handlers.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,25 @@ def _add_answers_additionals(out: DNSOutgoing, answers: _AnswerWithAdditionalsTy
124124
class _QueryResponse:
125125
"""A pair for unicast and multicast DNSOutgoing responses."""
126126

127+
__slots__ = (
128+
"_is_probe",
129+
"_msg",
130+
"_now",
131+
"_cache",
132+
"_additionals",
133+
"_ucast",
134+
"_mcast_now",
135+
"_mcast_aggregate",
136+
"_mcast_aggregate_last_second",
137+
)
138+
127139
def __init__(self, cache: DNSCache, msgs: List[DNSIncoming]) -> None:
128140
"""Build a query response."""
129-
self._is_probe = any(msg.is_probe for msg in msgs)
141+
self._is_probe = False
142+
for msg in msgs:
143+
if msg.is_probe:
144+
self._is_probe = True
145+
break
130146
self._msg = msgs[0]
131147
self._now = self._msg.now
132148
self._cache = cache
@@ -212,6 +228,8 @@ def _has_mcast_record_in_last_second(self, record: DNSRecord) -> bool:
212228
class QueryHandler:
213229
"""Query the ServiceRegistry."""
214230

231+
__slots__ = ("registry", "cache", "question_history")
232+
215233
def __init__(self, registry: ServiceRegistry, cache: DNSCache, question_history: QuestionHistory) -> None:
216234
"""Init the query handler."""
217235
self.registry = registry
@@ -345,6 +363,8 @@ def async_response( # pylint: disable=unused-argument
345363
class RecordManager:
346364
"""Process records into the cache and notify listeners."""
347365

366+
__slots__ = ("zc", "cache", "listeners")
367+
348368
def __init__(self, zeroconf: 'Zeroconf') -> None:
349369
"""Init the record manager."""
350370
self.zc = zeroconf
@@ -516,6 +536,8 @@ def async_remove_listener(self, listener: RecordUpdateListener) -> None:
516536
class MulticastOutgoingQueue:
517537
"""An outgoing queue used to aggregate multicast responses."""
518538

539+
__slots__ = ("zc", "queue", "additional_delay", "aggregation_delay")
540+
519541
def __init__(self, zeroconf: 'Zeroconf', additional_delay: int, max_aggregation_delay: int) -> None:
520542
self.zc = zeroconf
521543
self.queue: deque = deque()

0 commit comments

Comments
 (0)
0