perf: optimize was_applied fast path for known LWT statements#13
Open
perf: optimize was_applied fast path for known LWT statements#13
Conversation
Add a fast path in ResultSet.was_applied that skips batch detection (isinstance checks + regex match) when the query has a known LWT status from the server PREPARE response. For BoundStatement queries where is_lwt() returns True, the batch_regex match on the query string is entirely avoided. This benefits the most common LWT use case: prepared INSERT/UPDATE IF statements executed via BoundStatement, where the driver already knows from the PREPARE response whether the statement is an LWT. The slow path (isinstance + regex) is preserved for: - BatchStatement queries (detected via isinstance) - SimpleStatement batch queries (detected via regex) - Any query where is_lwt() returns False Also adds explicit tests for the fast path, non-LWT fallback, and BatchStatement handling in was_applied. Part of: scylladb#751
65e2db1 to
d3ee068
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
ResultSet.was_appliedthat skips batch detection when the query has a known LWT status from the server PREPARE responseBoundStatementqueries whereis_lwt()returnsTrue, thebatch_regexmatch andisinstancechecks are entirely avoidedBatchStatement,SimpleStatementbatches, and non-LWT queriesMotivation
ResultSet.was_appliedpreviously always ran batch detection on every call: twoisinstancechecks plus a regex match against the query string. For the most common LWT case (prepared INSERT/UPDATE IF viaBoundStatement), the driver already knows from the server PREPARE response whether it's an LWT statement. Leveragingis_lwt()lets us skip the batch detection entirely in this fast path.This is part of the LWT prepared statement performance improvement effort documented in scylladb#751 (optimization B4).
Changes
cassandra/cluster.py-ResultSet.was_applied:self.response_future.queryin a local variable (avoids repeated attribute lookups)query.is_lwt()isTrueand it's not aBatchStatement, skip batch detection and go directly to single-row validation + result extractionBatchStatement,SimpleStatement, and non-LWT queriestests/unit/test_resultset.py:test_was_applied_lwt_fast_path— tests the fast path with known LWT queries (all row factories, applied/not-applied, too-many-rows error)test_was_applied_non_lwt_fallback— tests that non-LWTSimpleStatementcorrectly falls through to slow pathtest_was_applied_batch_statement— tests thatBatchStatementuses slow path with[applied]column validationTesting
All unit tests pass:
tests/unit/test_resultset.py— 17/17 passed (14 original + 3 new)tests/unit/test_query.py— 6/6 passedtests/unit/test_parameter_binding.py— 37/37 passed