perf: use pre-allocated constant for null sentinel in collection serialization#763
Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
Draft
perf: use pre-allocated constant for null sentinel in collection serialization#763mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul wants to merge 1 commit intoscylladb:masterfrom
Conversation
…alization Replace per-call int32_pack(-1) with a module-level _INT32_NULL constant in serialize methods of ListType, SetType, MapType, TupleType, and UserType. This avoids a struct.pack call on every null element during collection serialization. Affected sites: - _SimpleParameterizedType.serialize_safe (ListType/SetType) - MapType.serialize_safe (null key + null value) - TupleType.serialize_safe - UserType.serialize_safe
mykaul
added a commit
to mykaul/python-driver
that referenced
this pull request
Apr 3, 2026
Replace io.BytesIO() buffer pattern with list accumulation + b''.join() in serialize_safe methods for ListType, SetType, MapType, TupleType, and UserType. Also pre-compute _INT32_NULL = int32_pack(-1) as a module-level constant to avoid repeated packing of the null sentinel. Buffer assembly micro-benchmarks (isolating the BytesIO overhead from per-element to_binary() cost): Scenario Before (us) After (us) Speedup List 100 elements 9.0 8.6 1.05x List 10 elements 1.1 0.9 1.18x List 10 all-null 0.8 0.4 2.23x Map 10 entries 2.0 1.6 1.24x The all-null case benefits most from the pre-computed _INT32_NULL constant, which eliminates repeated int32_pack(-1) calls. Note: PR scylladb#763 on this repo adds only the _INT32_NULL constant; this commit is a superset that also replaces BytesIO with b''.join() across all four collection/composite type serializers.
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.
Summary
int32_pack(-1)with a module-level_INT32_NULLconstant in 5 collection serialize methodsstruct.pack()call on every null element during collection serializationDetails
The CQL protocol represents
nullcollection elements as a 4-byteint32with value-1. Previously, every null element triggered a freshstruct.pack('>i', -1)call. This PR pre-computes the result once at module load time and reuses thebytesobject.Affected sites
ListType/SetType_SimpleParameterizedType.serialize_safeMapTypeMapType.serialize_safeTupleTypeTupleType.serialize_safeUserTypeUserType.serialize_safeBenchmark results
Isolated write operation (the
buf.write(...)call itself):buf.write(int32_pack(-1))(before)buf.write(_INT32_NULL)(after)End-to-end
ListType.serialize(list with 50% null elements):TupleType.serialize(5-element tuple, all nulls):Testing
CollectionNullSentinelTestswith 5 round-trip tests covering List, Set, Map, Tuple, and UserType withNoneelements