feat: optimize incoming parser by using unpack_from (#1115) · python-zeroconf/python-zeroconf@a7d50ba

@@ -60,10 +60,10 @@

60606161

DECODE_EXCEPTIONS = (IndexError, struct.error, IncomingDecodeError)

626263-

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

67676868

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

6969

@@ -184,9 +184,9 @@ def __repr__(self) -> str:

184184

]

185185

)

186186187-

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)

190190191191

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: