Skip to content

Commit 6d69ad8

Browse files
committed
feat: now 22% speed up
1 parent 79c336a commit 6d69ad8

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

src/zeroconf/_protocol/outgoing.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ cdef cython.uint _FLAGS_TC
1515
cdef cython.uint _MAX_MSG_ABSOLUTE
1616
cdef cython.uint _MAX_MSG_TYPICAL
1717

18+
1819
cdef bint TYPE_CHECKING
1920

21+
cdef unsigned int SHORT_CACHE_MAX
22+
2023
cdef object PACK_BYTE
2124
cdef object PACK_SHORT
2225
cdef object PACK_LONG

src/zeroconf/_protocol/outgoing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@
5353
PACK_SHORT = Struct('>H').pack
5454
PACK_LONG = Struct('>L').pack
5555

56+
SHORT_CACHE_MAX = 128
57+
5658
BYTE_TABLE = tuple(PACK_BYTE(i) for i in range(256))
57-
SHORT_LOOKUP = tuple(PACK_SHORT(i) for i in range(32))
59+
SHORT_LOOKUP = tuple(PACK_SHORT(i) for i in range(SHORT_CACHE_MAX))
5860

5961

6062
class State(enum.Enum):
@@ -223,7 +225,7 @@ def _write_byte(self, value: int_) -> None:
223225

224226
def _get_short(self, value: int_) -> bytes:
225227
"""Gets an unsigned short from a certain position in the packet"""
226-
if value < 32:
228+
if value < SHORT_CACHE_MAX:
227229
return SHORT_LOOKUP[value]
228230
return PACK_SHORT(value)
229231

0 commit comments

Comments
 (0)