Skip to content

Commit 91fe2ed

Browse files
committed
perf: pre-allocate buffer in VectorType.serialize for fixed-size elements
When subtype.serial_size() is known (e.g. FloatType, DoubleType, Int32Type), pre-allocate a bytearray of exact size and write elements via slice assignment instead of using io.BytesIO with dynamic growth. This avoids BytesIO's internal buffer reallocation overhead for the common case of fixed-size vector elements. Variable-size elements continue to use BytesIO with uvint length prefixes.
1 parent 91cd1ef commit 91fe2ed

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

cassandra/cqltypes.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,13 +1483,24 @@ def serialize(cls, v, protocol_version):
14831483
.format(cls.vector_size, cls.subtype.typename, v_length))
14841484

14851485
serialized_size = cls.subtype.serial_size()
1486-
buf = io.BytesIO()
1487-
for item in v:
1488-
item_bytes = cls.subtype.serialize(item, protocol_version)
1489-
if serialized_size is None:
1486+
if serialized_size is not None:
1487+
# Fixed-size elements: pre-allocate exact buffer, write directly
1488+
total = serialized_size * v_length
1489+
buf = bytearray(total)
1490+
offset = 0
1491+
for item in v:
1492+
item_bytes = cls.subtype.serialize(item, protocol_version)
1493+
buf[offset:offset + serialized_size] = item_bytes
1494+
offset += serialized_size
1495+
return bytes(buf)
1496+
else:
1497+
# Variable-size elements: use BytesIO with uvint length prefix
1498+
buf = io.BytesIO()
1499+
for item in v:
1500+
item_bytes = cls.subtype.serialize(item, protocol_version)
14901501
buf.write(uvint_pack(len(item_bytes)))
1491-
buf.write(item_bytes)
1492-
return buf.getvalue()
1502+
buf.write(item_bytes)
1503+
return buf.getvalue()
14931504

14941505
@classmethod
14951506
def cql_parameterized_type(cls):

0 commit comments

Comments
 (0)