perf: optimize was_applied fast path for known LWT statements (~1.5us, 1.1x speedup)#797
Draft
mykaul wants to merge 2 commits intoscylladb:masterfrom
Draft
perf: optimize was_applied fast path for known LWT statements (~1.5us, 1.1x speedup)#797mykaul wants to merge 2 commits intoscylladb:masterfrom
mykaul wants to merge 2 commits intoscylladb:masterfrom
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
Benchmark shows ~1.1x speedup for known-LWT fast path vs regex batch detection slow path.
8a3b2ed to
f4ec874
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_appliedfor statements wherequery.is_lwt()is True (BoundStatement/PreparedStatement)batch_regex.match()call andisinstance(query, BatchStatement)check for the common single-LWT caseBenchmark
Measured with
min()oftimeit.repeat(repeat=7, number=200_000)on a quiet machine (load <1).Savings: 612 ns/call
Tests
test_was_applied_lwt_fast_path,test_was_applied_non_lwt_fallback,test_was_applied_batch_statementtest_was_appliedto use explicit non-LWT query to exercise the slow (regex) path