feat: avoid python float conversion in listener hot path (#1245) · python-zeroconf/python-zeroconf@816ad4d
@@ -38,6 +38,8 @@
383839394040_bytes = bytes
41+_str = str
42+_int = int
41434244logging_DEBUG = logging.DEBUG
4345@@ -110,19 +112,23 @@ def datagram_received(
110112 )
111113return
112114113-v6_flow_scope: Union[Tuple[()], Tuple[int, int]] = ()
114115if len(addrs) == 2:
116+v6_flow_scope: Union[Tuple[()], Tuple[int, int]] = ()
115117# https://github.com/python/mypy/issues/1178
116118addr, port = addrs # type: ignore
119+addr_port = addrs
120+if TYPE_CHECKING:
121+addr_port = cast(Tuple[str, int], addr_port)
117122scope = None
118123else:
119124# https://github.com/python/mypy/issues/1178
120125addr, port, flow, scope = addrs # type: ignore
121126if debug: # pragma: no branch
122127log.debug('IPv6 scope_id %d associated to the receiving interface', scope)
123128v6_flow_scope = (flow, scope)
129+addr_port = (addr, port)
124130125-msg = DNSIncoming(data, (addr, port), scope, now)
131+msg = DNSIncoming(data, addr_port, scope, now)
126132self.data = data
127133self.last_time = now
128134self.last_message = msg
@@ -176,22 +182,23 @@ def handle_query_or_defer(
176182return
177183deferred.append(msg)
178184delay = millis_to_seconds(random.randint(*_TC_DELAY_RANDOM_INTERVAL))
179-assert self.zc.loop is not None
185+loop = self.zc.loop
186+assert loop is not None
180187self._cancel_any_timers_for_addr(addr)
181-self._timers[addr] = self.zc.loop.call_later(
182-delay, self._respond_query, None, addr, port, transport, v6_flow_scope
188+self._timers[addr] = loop.call_at(
189+loop.time() + delay, self._respond_query, None, addr, port, transport, v6_flow_scope
183190 )
184191185-def _cancel_any_timers_for_addr(self, addr: str) -> None:
192+def _cancel_any_timers_for_addr(self, addr: _str) -> None:
186193"""Cancel any future truncated packet timers for the address."""
187194if addr in self._timers:
188195self._timers.pop(addr).cancel()
189196190197def _respond_query(
191198self,
192199msg: Optional[DNSIncoming],
193-addr: str,
194-port: int,
200+addr: _str,
201+port: _int,
195202transport: _WrappedTransport,
196203v6_flow_scope: Union[Tuple[()], Tuple[int, int]] = (),
197204 ) -> None: