Skip to content

Commit 5230143

Browse files
Move _VALID_TYPE_CODES frozenset to module scope in tuples.py
The frozenset(ValueType) used for type code validation in encode_row_header() was being recreated on every function call. Since ValueType is a fixed enum, this frozenset can be computed once at module load time and reused across all calls. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6af7301 commit 5230143

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/dqlitewire/tuples.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from dqlitewire.exceptions import DecodeError, EncodeError
1414
from dqlitewire.types import decode_value, encode_value, pad_to_word
1515

16+
# Valid ValueType codes as integers, for fast membership testing in hot paths.
17+
_VALID_TYPE_CODES = frozenset(int(v) for v in ValueType)
18+
1619

1720
class RowMarker(Enum):
1821
"""Row marker detected during header parsing."""
@@ -163,11 +166,10 @@ def encode_row_header(types: Sequence[ValueType]) -> bytes:
163166
if not types:
164167
return b""
165168

166-
_valid_type_codes = frozenset(ValueType)
167169
for i, t in enumerate(types):
168170
if int(t) > 15:
169171
raise EncodeError(f"Value type {t} at index {i} exceeds 4-bit nibble range (max 15)")
170-
if int(t) not in _valid_type_codes:
172+
if int(t) not in _VALID_TYPE_CODES:
171173
raise EncodeError(f"Invalid type code {int(t)} at index {i}: not a valid ValueType")
172174

173175
header = bytearray()

0 commit comments

Comments
 (0)