8000 feat: speed up answering incoming questions (#1186) · python-zeroconf/python-zeroconf@8f37665 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f37665

Browse files
authored
feat: speed up answering incoming questions (#1186)
1 parent 8cf5b87 commit 8f37665

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/zeroconf/_dns.pxd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ cdef class DNSNsec(DNSRecord):
9898

9999
cdef class DNSRRSet:
100100

101-
cdef _record_sets
101+
cdef cython.list _record_sets
102102
cdef cython.dict _lookup
103103

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

107-
@cython.locals(lookup=cython.dict)
107+
@cython.locals(
108+
record=DNSRecord,
109+
record_sets=cython.list,
110+
)
108111
cdef cython.dict _get_lookup(self)

src/zeroconf/_dns.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import enum
2424
import socket
25-
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Union, cast
25+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, cast
2626

2727
from ._exceptions import AbstractMethodException
2828
from ._utils.net import _is_v6_address
@@ -516,7 +516,7 @@ class DNSRRSet:
516516

517517
__slots__ = ('_record_sets', '_lookup')
518518

519-
def __init__(self, record_sets: Iterable[List[DNSRecord]]) -> None:
519+
def __init__(self, record_sets: List[List[DNSRecord]]) -> None:
520520
"""Create an RRset from records sets."""
521521
self._record_sets = record_sets
522522
self._lookup: Optional[Dict[DNSRecord, float]] = None
@@ -530,7 +530,10 @@ def _get_lookup(self) -> Dict[DNSRecord, float]:
530530
"""Return the lookup table, building it if needed."""
531531
if self._lookup is None:
532532
# Build the hash table so we can lookup the record ttl
533-
self._lookup = {record: record.ttl for record_sets in self._record_sets for record in record_sets}
533+
self._lookup = {}
534+
for record_sets in self._record_sets:
535+
for record in record_sets:
536+
self._lookup[record] = record.ttl
534537
return self._lookup
535538

536539
def suppresses(self, record: _DNSRecord) -> bool:

src/zeroconf/_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def async_response( # pylint: disable=unused-argument
322322
This function must be run in the event loop as it is not
323323
threadsafe.
324324
"""
325-
known_answers = DNSRRSet(msg.answers for msg in msgs if not msg.is_probe)
325+
known_answers = DNSRRSet([msg.answers for msg in msgs if not msg.is_probe])
326326
query_res = _QueryResponse(self.cache, msgs)
327327

328328
for msg in msgs:

0 commit comments

Comments
 (0)
0