perf: buffer accumulation in _write_query_params() reduces f.write() calls (~100ns improvement, 1.1-1.3x speedup)#790
Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
Draft
Conversation
bc1545f to
9b21d5b
Compare
Author
|
Just spitting this here: Honest answer: on its own, ~100ns per call is tiny. But context matters:
|
f2be2a8 to
ac64459
Compare
Replace the per-parameter write_value(f, param) loop in _QueryMessage._write_query_params() with a buffer accumulation approach: list.append + b"".join + single f.write(). This reduces the number of f.write() calls from 2*N+1 to 1, which is significant for vector workloads with large parameters. Also removes the redundant ExecuteMessage._write_query_params() pass-through override to avoid extra MRO lookup per call. Includes 14 unit tests covering normal, NULL, UNSET, empty, large vector, and mixed parameter scenarios for both ExecuteMessage and QueryMessage. Includes a benchmark script (benchmarks/bench_execute_write_params.py).
ac64459 to
1e1e709
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.
Summary
Replace per-parameter
write_value(f, param)loops with buffer accumulation (list.append+b"".join+ singlef.write()), reducingf.write()calls from(2*N + 1)to 1 for N query parameters in the execute/query path.What changed
cassandra/protocol.py_QueryMessage._write_query_params()-- Buffer accumulation for the parameter loop. Local variable caching (_int32_pack,_parts_append) for Cython-friendly tight loop.ExecuteMessage._write_query_params()-- Removed unnecessarysuper()pass-through override (now inherited directly from_QueryMessage).tests/unit/test_protocol.pyAdded 14 new test methods in
WriteQueryParamsBufferAccumulationTest.Benchmark
Measured with
min()oftimeit.repeat(repeat=7, number=200_000)on a quiet machine (load <3), Cython.socompiled, before/after rebuild.For single-param workloads, the list/join overhead slightly exceeds the write-call savings. The benefit appears with multiple params (10+), where reducing
2*Nwrites to 1 join becomes significant. The real payoff is inBatchMessage.send_body()(PR #791), where N is much larger.Tests