perf: replace BytesIO with b"".join() in collection serialization#787
Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
Draft
perf: replace BytesIO with b"".join() in collection serialization#787mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul wants to merge 1 commit intoscylladb:masterfrom
Conversation
d9b8b3c to
17369e1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The
serialize_safemethods for collection types (ListType,SetType,MapType,TupleType,UserType) useio.BytesIO()as an intermediate buffer — creating a BytesIO object, writing fragments with multiple.write()calls, then extracting the result with.getvalue(). This pattern incurs overhead from:.write()call (Python method lookup + C-level buffer management)int32_pack(-1)calls for null elements, recomputing the same 4-byte value each timeSummary of Changes
io.BytesIO()with list accumulation +b"".join()in fourserialize_safemethods:_SimpleParameterizedType.serialize_safe(used byListType,SetType)MapType.serialize_safeTupleType.serialize_safeUserType.serialize_safe_INT32_NULL = int32_pack(-1)as a pre-computed module-level constant, eliminating repeated packing of the null sentinel valueThe
b"".join(parts)pattern is a well-known Python idiom that avoids intermediate buffer object overhead. CPython'sbytes.join()pre-calculates the total output size and copies all fragments in a single pass, whereas BytesIO must manage a growable internal buffer with potential reallocations.How It Was Tested
pytest tests/unit/test_types.py(62 passed),pytest tests/unit/test_query.py tests/unit/test_cluster.py(36 passed)Benchmarks
Micro-benchmarks measuring end-to-end
serialize_safeperformance (Python 3.14, timeit):The pattern also produces simpler, more idiomatic Python code.