perf: add Cython metadata parser using BytesIOReader (100's-1000's of ns improvements, x1.5-4 speedup)#814
Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
Draft
Conversation
48538ac to
3ec7841
Compare
Author
|
I'm slowly but surely converting the Python driver to a C driver.... Unsure about this direction. :-/ |
d5c237e to
713bcc5
Compare
Add cassandra/metadata_parser.pyx with zero-copy metadata parsing that uses BytesIOReader instead of Python BytesIO, eliminating per-read bytes allocation in recv_results_metadata and read_type. Modify row_parser.pyx to create a single BytesIOReader from the full remaining buffer and reuse it for both metadata parsing and row parsing, instead of using Python BytesIO for metadata then a separate BytesIOReader for rows. Benchmarks (taskset -c 0, Python 3.14, Cython): recv_results_metadata only (10 simple cols): 6082 -> 1265 ns (4.8x) recv_results_rows (10 cols, 0 rows): 11068 -> 5652 ns (1.96x) recv_results_rows (10 cols, 10 rows): 29590 -> 24759 ns (1.20x) recv_results_rows (10 cols, 100 rows): 181053 -> 173446 ns (1.04x) recv_results_rows (50 cols, 0 rows): 44904 -> 20569 ns (2.18x)
713bcc5 to
1f3e38f
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
Add
cassandra/metadata_parser.pyxwith zero-copy metadata parsing usingBytesIOReader, eliminating per-readbytesallocation that dominatesrecv_results_metadataandread_typecost in the Cython path.Changes
New file:
cassandra/metadata_parser.pyx— Cythoncdef inlinefunctions (read_int_br,read_short_br,read_string_br,read_type_br) operating onBytesIOReaderinstead of PythonBytesIO. A factory functionmake_recv_results_metadata()returns a closure that captures the type-code map and type objects once.Modified:
cassandra/row_parser.pyx— Creates a singleBytesIOReaderfrom the full remaining buffer and reuses it for both metadata parsing and row parsing (previously: PythonBytesIOfor metadata, then a separateBytesIOReaderfor rows). Error recovery path correctly saves/restores reader position.Modified:
cassandra/protocol.py—cython_protocol_handler()imports the new metadata parser and passes it tomake_recv_results_rows().Benchmarks
All measurements:
taskset -c 0, Python 3.14.3, Cython compiled, origin/master vs this PR.recv_results_metadataonly (isolated)Collections are limited by
apply_parameterscost (addressed separately in #794).recv_results_rowsend-to-end (old Cython vs new Cython)The improvement is entirely in metadata parsing (~2x for 0-row cases). As row count increases, metadata cost becomes a smaller fraction of total time, so the speedup dilutes. The 1000-row case is dominated by row deserialization and shows no change.
Tests
231 passed, 1 skipped — identical to baseline.
Notes
recv_prepared_metadata) is unchanged — it still uses PythonBytesIOinherited fromResultMessage. Prepared statements are parsed once perPREPARE, not on the hot path.setup.pyautomatically picks up the new.pyxfile via the existingcassandra/*.pyxglob.DEFin the.pyxfile (documented with a sync warning comment).