perf: inline row decoding and eliminate closures in recv_results_rows (100's to 1000's of ns, x1.3-1.8 speedup, Python only)#765
Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
Conversation
3c3fea8 to
020c764
Compare
Split recv_results_rows into fast path (no column encryption) and slow
path (column encryption enabled):
Fast path (common case):
- Reads raw column bytes and decodes types in a single pass per row
via _decode_row_inline(), eliminating the intermediate list-of-lists
- Skips ColDesc namedtuple creation entirely (only needed for CE)
- No closure allocation per call
- Wraps decode errors with column name/type info for diagnostics
Slow path (column encryption):
- Preserves full CE logic with ColDesc creation
- Moves decode_val/decode_row closures to module-level functions
(_decode_val_ce, _decode_row_ce) to avoid per-call closure overhead
Note: This PR modifies the same method as PR scylladb#630 (which also splits
recv_results_rows into CE/non-CE branches). There will be a merge
conflict that needs manual resolution if both PRs are accepted.
020c764 to
30d3a44
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
recv_results_rowsinto fast path (no column encryption) and slow path (CE enabled)Details
Problem
The current
recv_results_rowshas three sources of overhead on every call:Two passes over row data: First
recv_rowreads all raw bytes into alist[list[bytes]], thendecode_rowiterates again to deserialize — doubling iteration and creating intermediate lists that are immediately discarded.Per-call closures:
decode_valanddecode_roware defined as closures insiderecv_results_rows, meaning Python allocates new function objects on every result set.Unconditional
ColDesccreation:ColDescnamedtuples are built for every column even when column encryption is not configured (the vast majority of deployments).Solution
Fast path (no column encryption — the common case):
_decode_row_inline(f, colcount, col_types, protocol_version)reads each column's size, reads the bytes, and immediately callsfrom_binary()— one pass, no intermediate listColDesccreation is skipped entirelySlow path (column encryption enabled):
decode_val/decode_rowmoved to module-level functions (_decode_val_ce,_decode_row_ce) to avoid per-call closure overheadBenchmark results
Measured on CPython 3.14.3, Protocol V4, 300 iterations, 100 warmup. All values in nanoseconds per row.
1.3x–1.8x speedup on the pure Python path. The speedup is higher with NULL-heavy workloads because the inline path short-circuits
from_binary()for negative-length (NULL) columns.Merge conflict note
Testing
test_protocol.py)