(improvement) Optimize _key_parts_packed routing key computation (100-300ns savings - 27-33% savings)#799
Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
Draft
(improvement) Optimize _key_parts_packed routing key computation (100-300ns savings - 27-33% savings)#799mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul wants to merge 1 commit intoscylladb:masterfrom
Conversation
Replace per-call struct.pack(">H%dsB" % l, l, p, 0) with pre-compiled
uint16_pack(len(p)) + p + b'\\x00'. This eliminates the format string
interpolation and dynamic struct format creation on every call, using
the pre-compiled uint16_pack (struct.Struct('>H').pack) instead.
The routing key computation is called for every query when
TokenAwarePolicy is in use, making this a hot path.
Author
Benchmark results (CPython 3.14, 500k iterations)
The routing key computation runs on every query when |
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
Replace per-call
struct.pack(">H%dsB" % l, l, p, 0)withuint16_pack(len(p)) + p + b'\x00'using the pre-compileduint16_pack(struct.Struct('>H').pack) fromcassandra.marshal. Eliminates format string interpolation and dynamic struct format creation on every call.Motivation
The routing key computation (
_key_parts_packed) is called for every query whenTokenAwarePolicyis in use, making it a hot path. The original code creates a new format string (">H%dsB" % l) on every invocation, which triggers a newstruct.packformat parse each time. Using the pre-compileduint16_packavoids this overhead.Benchmark (CPython 3.14, per-call)
The routing key computation runs on every query when
TokenAwarePolicyis active (the default).Changes
cassandra/query.py: Importuint16_packfromcassandra.marshal, replacestruct.pack(">H%dsB" % l, l, p, 0)withuint16_pack(len(p)) + p + b'\x00'Testing
Unit tests pass (43/43 in test_query.py and test_parameter_binding.py). Output verified to match the original format byte-for-byte.