Skip to content

Commit 2b4e88b

Browse files
committed
perf: eliminate extra BytesIO allocation in encode_message compression path
When compression is active (protocol v4 and below), encode_message previously created two BytesIO objects: one for the uncompressed body, then another to write the header + compressed body. The second BytesIO is unnecessary -- use direct bytes concatenation (header + compressed_body) instead, which avoids one BytesIO allocation per compressed message. The non-compression path already used a single BytesIO and is unchanged.
1 parent 8e6c4d4 commit 2b4e88b

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

cassandra/protocol.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,32 +1098,33 @@ def encode_message(cls, msg, stream_id, protocol_version, compressor, allow_beta
10981098
flags |= USE_BETA_FLAG
10991099

11001100
buff = io.BytesIO()
1101-
buff.seek(9)
11021101

11031102
# With checksumming, the compression is done at the segment frame encoding
11041103
if (compressor and not ProtocolVersion.has_checksumming_support(protocol_version)):
1105-
body = io.BytesIO()
11061104
if msg.custom_payload:
1107-
write_bytesmap(body, msg.custom_payload)
1108-
msg.send_body(body, protocol_version)
1109-
body = body.getvalue()
1105+
write_bytesmap(buff, msg.custom_payload)
1106+
msg.send_body(buff, protocol_version)
1107+
body = buff.getvalue()
11101108

11111109
if len(body) > 0:
11121110
body = compressor(body)
11131111
flags |= COMPRESSED_FLAG
11141112

1115-
buff.write(body)
11161113
length = len(body)
1114+
header = v3_header_pack(protocol_version, flags, stream_id, msg.opcode) + int32_pack(length)
1115+
return header + body
11171116
else:
1117+
buff.seek(9)
1118+
11181119
if msg.custom_payload:
11191120
write_bytesmap(buff, msg.custom_payload)
11201121
msg.send_body(buff, protocol_version)
11211122

11221123
length = buff.tell() - 9
11231124

1124-
buff.seek(0)
1125-
cls._write_header(buff, protocol_version, flags, stream_id, msg.opcode, length)
1126-
return buff.getvalue()
1125+
buff.seek(0)
1126+
cls._write_header(buff, protocol_version, flags, stream_id, msg.opcode, length)
1127+
return buff.getvalue()
11271128

11281129
@staticmethod
11291130
def _write_header(f, version, flags, stream_id, opcode, length):

0 commit comments

Comments
 (0)