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
Conversation
3c3fea8 to
020c764
Compare
020c764 to
30d3a44
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.
30d3a44 to
8d73498
Compare
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
There was a problem hiding this comment.
Pull request overview
Optimizes pure-Python result-row decoding while preserving the column-encryption path.
Changes:
- Decodes unencrypted rows in one pass without intermediate lists or closures.
- Moves encrypted-column decoding helpers to module scope.
- Adds coverage for inline decoding error messages.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
cassandra/protocol.py |
Implements optimized row decoding paths. |
tests/unit/test_protocol.py |
Tests inline decode error wrapping. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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)