Skip to content

Commit a7d50ba

Browse files
authored
feat: optimize incoming parser by using unpack_from (#1115)
1 parent f41fcc3 commit a7d50ba

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/zeroconf/_protocol/incoming.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@
6060

6161
DECODE_EXCEPTIONS = (IndexError, struct.error, IncomingDecodeError)
6262

63-
UNPACK_3H = struct.Struct(b'!3H').unpack
64-
UNPACK_6H = struct.Struct(b'!6H').unpack
65-
UNPACK_HH = struct.Struct(b'!HH').unpack
66-
UNPACK_HHiH = struct.Struct(b'!HHiH').unpack
63+
UNPACK_3H = struct.Struct(b'!3H').unpack_from
64+
UNPACK_6H = struct.Struct(b'!6H').unpack_from
65+
UNPACK_HH = struct.Struct(b'!HH').unpack_from
66+
UNPACK_HHiH = struct.Struct(b'!HHiH').unpack_from
6767

6868
_seen_logs: Dict[str, Union[int, tuple]] = {}
6969

@@ -184,9 +184,9 @@ def __repr__(self) -> str:
184184
]
185185
)
186186

187-
def _unpack(self, unpacker: Callable[[bytes], tuple], length: int) -> tuple:
187+
def _unpack(self, unpacker: Callable[[bytes, int], tuple], length: int) -> tuple:
188188
self.offset += length
189-
return unpacker(self.data[self.offset - length : self.offset])
189+
return unpacker(self.data, self.offset - length)
190190

191191
def _read_header(self) -> None:
192192
"""Reads header portion of packet"""
@@ -224,7 +224,8 @@ def _read_others(self) -> None:
224224
n = self.num_answers + self.num_authorities + self.num_additionals
225225
for _ in range(n):
226226
domain = self._read_name()
227-
type_, class_, ttl, length = self._unpack(UNPACK_HHiH, 10)
227+
type_, class_, ttl, length = UNPACK_HHiH(self.data, self.offset)
228+
self.offset += 10
228229
end = self.offset + length
229230
rec = None
230231
try:

0 commit comments

Comments
 (0)