Skip to content

perf: optimize was_applied fast path for known LWT statements#13

Open
mykaul wants to merge 1 commit intomasterfrom
perf/optimize-was-applied
Open

perf: optimize was_applied fast path for known LWT statements#13
mykaul wants to merge 1 commit intomasterfrom
perf/optimize-was-applied

Conversation

@mykaul
Copy link
Owner

@mykaul mykaul commented Mar 15, 2026

Summary

  • Add a fast path in ResultSet.was_applied that skips batch detection 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 and isinstance checks are entirely avoided
  • The slow path (isinstance + regex) is preserved for BatchStatement, SimpleStatement batches, and non-LWT queries

Motivation

ResultSet.was_applied previously always ran batch detection on every call: two isinstance checks plus a regex match against the query string. For the most common LWT case (prepared INSERT/UPDATE IF via BoundStatement), the driver already knows from the server PREPARE response whether it's an LWT statement. Leveraging is_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:

  • Cache self.response_future.query in a local variable (avoids repeated attribute lookups)
  • Add fast path: if query.is_lwt() is True and it's not a BatchStatement, skip batch detection and go directly to single-row validation + result extraction
  • Slow path is unchanged for BatchStatement, SimpleStatement, and non-LWT queries

tests/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-LWT SimpleStatement correctly falls through to slow path
  • test_was_applied_batch_statement — tests that BatchStatement uses slow path with [applied] column validation

Testing

All unit tests pass:

  • tests/unit/test_resultset.py — 17/17 passed (14 original + 3 new)
  • tests/unit/test_query.py — 6/6 passed
  • tests/unit/test_parameter_binding.py — 37/37 passed

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
@mykaul mykaul force-pushed the perf/optimize-was-applied branch from 65e2db1 to d3ee068 Compare March 16, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant