8000 feat: speed up answering incoming questions by bdraco · Pull Request #1186 · python-zeroconf/python-zeroconf · GitHub
[go: up one dir, main page]

Skip to content

feat: speed up answering incoming questions #1186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/zeroconf/_dns.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ cdef class DNSNsec(DNSRecord):

cdef class DNSRRSet:

cdef _record_sets
cdef cython.list _record_sets
cdef cython.dict _lookup

@cython.locals(other=DNSRecord)
cpdef suppresses(self, DNSRecord record)

@cython.locals(lookup=cython.dict)
@cython.locals(
record=DNSRecord,
record_sets=cython.list,
)
cdef cython.dict _get_lookup(self)
9 changes: 6 additions & 3 deletions src/zeroconf/_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import enum
import socket
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Union, cast
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, cast

from ._exceptions import AbstractMethodException
from ._utils.net import _is_v6_address
Expand Down Expand Up @@ -516,7 +516,7 @@ class DNSRRSet:

__slots__ = ('_record_sets', '_lookup')

def __init__(self, record_sets: Iterable[List[DNSRecord]]) -> None:
def __init__(self, record_sets: List[List[DNSRecord]]) -> None:
"""Create an RRset from records sets."""
self._record_sets = record_sets
self._lookup: Optional[Dict[DNSRecord, float]] = None
Expand All @@ -530,7 +530,10 @@ def _get_lookup(self) -> Dict[DNSRecord, float]:
"""Return the lookup table, building it if needed."""
if self._lookup is None:
# Build the hash table so we can lookup the record ttl
self._lookup = {record: record.ttl for record_sets in self._record_sets for record in record_sets}
self._lookup = {}
for record_sets in self._record_sets:
for record in record_sets:
self._lookup[record] = record.ttl
return self._lookup

def suppresses(self, record: _DNSRecord) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/zeroconf/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def async_response( # pylint: disable=unused-argument
This function must be run in the event loop as it is not
threadsafe.
"""
known_answers = DNSRRSet(msg.answers for msg in msgs if not msg.is_probe)
known_answers = DNSRRSet([msg.answers for msg in msgs if not msg.is_probe])
query_res = _QueryResponse(self.cache, msgs)

for msg in msgs:
Expand Down
0