Skip to content

Commit

Permalink
feat: speed up the service registry with a cython pxd (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Aug 22, 2023
1 parent cd7b56b commit 47d3c7a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions build_ext.py
Expand Up @@ -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
),
Expand Down
18 changes: 18 additions & 0 deletions 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)
5 changes: 4 additions & 1 deletion src/zeroconf/_services/registry.py
Expand Up @@ -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."""
Expand Down

0 comments on commit 47d3c7a

Please sign in to comment.