Skip to content

Commit 4e9f137

Browse files
committed
feat: precalc bytes
1 parent 624c925 commit 4e9f137

2 files changed

Lines changed: 5 additions & 15 deletions

File tree

src/zeroconf/_protocol/outgoing.pxd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ cdef object TYPE_CHECKING
2020
cdef object PACK_BYTE
2121
cdef object PACK_SHORT
2222
cdef object PACK_LONG
23-
cdef object CACHED_PACK_SHORT
2423

25-
cdef cython.dict BYTE_TABLE
24+
cdef cython.tuple BYTE_TABLE
2625

2726
cdef class DNSOutgoing:
2827

src/zeroconf/_protocol/outgoing.py

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

2323
import enum
2424
import logging
25-
from functools import lru_cache
2625
from struct import Struct
2726
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union
2827

@@ -54,15 +53,7 @@
5453
PACK_SHORT = Struct('>H').pack
5554
PACK_LONG = Struct('>L').pack
5655

57-
BYTE_TABLE = [PACK_BYTE(i) for i in range(256)]
58-
59-
60-
@lru_cache(maxsize=256)
61-
def _cached_pack_short(value: int_) -> bytes_:
62-
return PACK_SHORT(value)
63-
64-
65-
CACHED_PACK_SHORT = _cached_pack_short
56+
BYTE_TABLE = tuple(PACK_BYTE(i) for i in range(256))
6657

6758

6859
class State(enum.Enum):
@@ -224,15 +215,15 @@ def _write_byte(self, value: int_) -> None:
224215

225216
def _insert_short_at_start(self, value: int_) -> None:
226217
"""Inserts an unsigned short at the start of the packet"""
227-
self.data.insert(0, CACHED_PACK_SHORT(value))
218+
self.data.insert(0, PACK_SHORT(value))
228219

229220
def _replace_short(self, index: int_, value: int_) -> None:
230221
"""Replaces an unsigned short in a certain position in the packet"""
231-
self.data[index] = CACHED_PACK_SHORT(value)
222+
self.data[index] = PACK_SHORT(value)
232223

233224
def write_short(self, value: int_) -> None:
234225
"""Writes an unsigned short to the packet"""
235-
self.data.append(CACHED_PACK_SHORT(value))
226+
self.data.append(PACK_SHORT(value))
236227
self.size += 2
237228

238229
def _write_int(self, value: Union[float, int]) -> None:

0 commit comments

Comments
 (0)