Skip to content

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
mykaul:perf/optimize-was-applied
Draft

perf: optimize was_applied fast path for known LWT statements (~1.5us, 1.1x speedup)#797
mykaul wants to merge 2 commits intoscylladb:masterfrom
mykaul:perf/optimize-was-applied

Conversation

@mykaul
Copy link
Copy Markdown

@mykaul mykaul commented Apr 5, 2026

Summary

  • Add fast path in ResultSet.was_applied for statements where query.is_lwt() is True (BoundStatement/PreparedStatement)
  • Skips the expensive batch_regex.match() call and isinstance(query, BatchStatement) check for the common single-LWT case
  • Non-LWT and batch statements fall through to the existing slow path unchanged

Benchmark

Measured with min() of timeit.repeat(repeat=7, number=200_000) on a quiet machine (load <1).

Path Per-call Speedup
Slow path (non-LWT, regex check) 7536 ns baseline
Fast path (known LWT, skip regex) 6924 ns 1.1x

Savings: 612 ns/call

Tests

  • 4 new focused tests: test_was_applied_lwt_fast_path, test_was_applied_non_lwt_fallback, test_was_applied_batch_statement
  • Updated existing test_was_applied to use explicit non-LWT query to exercise the slow (regex) path
  • Full unit test suite passes (648 passed)

mykaul added 2 commits April 5, 2026 18:36
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.
@mykaul mykaul force-pushed the perf/optimize-was-applied branch from 8a3b2ed to f4ec874 Compare April 5, 2026 17:35
@mykaul mykaul changed the title perf: optimize was_applied fast path for known LWT statements perf: optimize was_applied fast path for known LWT statements (~1.5us, 1.1x speedup) Apr 7, 2026
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