Skip to content

Commit

Permalink
feat: speed up answering incoming questions (#1186)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jun 17, 2023
1 parent 8cf5b87 commit 8f37665
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/zeroconf/_dns.pxd
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
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
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 comments on commit 8f37665

Please sign in to comment.