Skip to content

Commit ebc23ee

Browse files
authored
Sort responses to increase chance of name compression (#954)
- When building an outgoing response, sort the names together to increase the likelihood of name compression. In testing this reduced the number of packets for large responses (from 7 packets to 6)
1 parent 9d69d18 commit ebc23ee

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

zeroconf/_handlers.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,16 @@ def construct_outgoing_unicast_answers(
100100

101101
def _add_answers_additionals(out: DNSOutgoing, answers: _AnswerWithAdditionalsType) -> None:
102102
# Find additionals and suppress any additionals that are already in answers
103-
additionals: Set[DNSRecord] = set().union(*answers.values()) # type: ignore
104-
additionals -= answers.keys()
105-
for answer in answers:
103+
sending: Set[DNSRecord] = set(answers.keys())
104+
# Answers are sorted to group names together to increase the chance
105+
# that similar names will end up in the same packet and can reduce the
106+
# overall size of the outgoing response via name compression
107+
for answer, additionals in sorted(answers.items(), key=lambda kv: kv[0].name):
106108
out.add_answer_at_time(answer, 0)
107-
for additional in additionals:
108-
out.add_additional_answer(additional)
109+
for additional in additionals:
110+
if additional not in sending:
111+
out.add_additional_answer(additional)
112+
sending.add(additional)
109113

110114

111115
def sanitize_incoming_record(record: DNSRecord) -> None:

0 commit comments

Comments
 (0)