From 47d3c7ad4bc5f2247631c3ad5e6b6156d45a0a4e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 22 Aug 2023 09:46:33 -0500 Subject: [PATCH] feat: speed up the service registry with a cython pxd (#1226) --- build_ext.py | 1 + src/zeroconf/_services/registry.pxd | 18 ++++++++++++++++++ src/zeroconf/_services/registry.py | 5 ++++- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/zeroconf/_services/registry.pxd diff --git a/build_ext.py b/build_ext.py index c0042df2..38c8127a 100644 --- a/build_ext.py +++ b/build_ext.py @@ -28,6 +28,7 @@ def build(setup_kwargs: Any) -> None: "src/zeroconf/_listener.py", "src/zeroconf/_protocol/incoming.py", "src/zeroconf/_protocol/outgoing.py", + "src/zeroconf/_services/registry.py", ], compiler_directives={"language_level": "3"}, # Python 3 ), diff --git a/src/zeroconf/_services/registry.pxd b/src/zeroconf/_services/registry.pxd new file mode 100644 index 00000000..722ef0ec --- /dev/null +++ b/src/zeroconf/_services/registry.pxd @@ -0,0 +1,18 @@ + +import cython + + +cdef class ServiceRegistry: + + cdef cython.dict _services + cdef public cython.dict types + cdef public cython.dict servers + + @cython.locals( + record_list=cython.list, + ) + cdef _async_get_by_index(self, cython.dict records, str key) + + cdef _add(self, object info) + + cdef _remove(self, cython.list infos) diff --git a/src/zeroconf/_services/registry.py b/src/zeroconf/_services/registry.py index 64f13512..1f2f1d52 100644 --- a/src/zeroconf/_services/registry.py +++ b/src/zeroconf/_services/registry.py @@ -78,7 +78,10 @@ def async_get_infos_server(self, server: str) -> List[ServiceInfo]: def _async_get_by_index(self, records: Dict[str, List], key: str) -> List[ServiceInfo]: """Return all ServiceInfo matching the index.""" - return [self._services[name] for name in records.get(key, [])] + record_list = records.get(key) + if record_list is None: + return [] + return [self._services[name] for name in record_list] def _add(self, info: ServiceInfo) -> None: """Add a new service under the lock."""