Skip to content

Commit

Permalink
Optimize decoding labels from incoming packets (#1019)
Browse files Browse the repository at this point in the history
- decode is a bit faster vs str()

```
>>> ts = Timer("s.decode('utf-8', 'replace')", "s = b'TV Beneden (2)\x10_androidtvremote\x04_tcp\x05local'")
>>> ts.timeit()
0.09910525000003645
>>> ts = Timer("str(s, 'utf-8', 'replace')", "s = b'TV Beneden (2)\x10_androidtvremote\x04_tcp\x05local'")
>>> ts.timeit()
0.1304596250000145
```
  • Loading branch information
bdraco authored Oct 30, 2021
1 parent 0fdcd51 commit 4b9a6c3
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions zeroconf/_protocol/incoming.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def _decode_labels_at_offset(self, off: int, labels: List[str], seen_pointers: S

if length < 0x40:
label_idx = off + DNS_COMPRESSION_HEADER_LEN
labels.append(str(self.data[label_idx : label_idx + length], 'utf-8', 'replace'))
labels.append(self.data[label_idx : label_idx + length].decode('utf-8', 'replace'))
off += DNS_COMPRESSION_HEADER_LEN + length
continue

Expand All @@ -302,9 +302,9 @@ def _decode_labels_at_offset(self, off: int, labels: List[str], seen_pointers: S
raise IncomingDecodeError(f"DNS compression pointer at {off} points to itself")
if link in seen_pointers:
raise IncomingDecodeError(f"DNS compression pointer at {off} was seen again")
seen_pointers.add(link)
linked_labels = self.name_cache.get(link, [])
if not linked_labels:
seen_pointers.add(link)
self._decode_labels_at_offset(link, linked_labels, seen_pointers)
self.name_cache[link] = linked_labels
labels.extend(linked_labels)
Expand Down

0 comments on commit 4b9a6c3

Please sign in to comment.