From d057fe98ef117c0ca7c890365ac16790485e5776 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Sep 2023 19:01:42 -0500 Subject: [PATCH] feat: reduce overhead to process incoming questions When the mac discovery app is open, the number of questions goes up almost 10x. We can reduce a bit of the overhead by cythonizing the time functions and cimporting it into the hot path --- build_ext.py | 1 + src/zeroconf/_listener.pxd | 6 +++--- src/zeroconf/_utils/time.pxd | 4 ++++ src/zeroconf/_utils/time.py | 4 +++- 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 src/zeroconf/_utils/time.pxd diff --git a/build_ext.py b/build_ext.py index 1b27457da..8e4e7b99f 100644 --- a/build_ext.py +++ b/build_ext.py @@ -31,6 +31,7 @@ def build(setup_kwargs: Any) -> None: "src/zeroconf/_protocol/outgoing.py", "src/zeroconf/_handlers/record_manager.py", "src/zeroconf/_services/registry.py", + "src/zeroconf/_utils/time.py", ], compiler_directives={"language_level": "3"}, # Python 3 ), diff --git a/src/zeroconf/_listener.pxd b/src/zeroconf/_listener.pxd index 0f32a44a7..87ed8b5f2 100644 --- a/src/zeroconf/_listener.pxd +++ b/src/zeroconf/_listener.pxd @@ -1,13 +1,13 @@ import cython +from ._protocol.incoming cimport DNSIncoming +from ._utils.time cimport current_time_millis, millis_to_seconds + -cdef object millis_to_seconds cdef object log cdef object logging_DEBUG -from ._protocol.incoming cimport DNSIncoming - cdef class AsyncListener: diff --git a/src/zeroconf/_utils/time.pxd b/src/zeroconf/_utils/time.pxd new file mode 100644 index 000000000..367f39b66 --- /dev/null +++ b/src/zeroconf/_utils/time.pxd @@ -0,0 +1,4 @@ + +cpdef current_time_millis() + +cpdef millis_to_seconds(object millis) diff --git a/src/zeroconf/_utils/time.py b/src/zeroconf/_utils/time.py index 59362c557..c6811585c 100644 --- a/src/zeroconf/_utils/time.py +++ b/src/zeroconf/_utils/time.py @@ -23,6 +23,8 @@ import time +_float = float + def current_time_millis() -> float: """Current time in milliseconds. @@ -33,6 +35,6 @@ def current_time_millis() -> float: return time.monotonic() * 1000 -def millis_to_seconds(millis: float) -> float: +def millis_to_seconds(millis: _float) -> float: """Convert milliseconds to seconds.""" return millis / 1000.0