Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions benchmarks/micro/bench_custom_payload_guard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python
"""
Benchmark: guard update_custom_payload calls vs unconditional method calls.

In the common case both query.custom_payload and custom_payload are None,
so the method calls are pure overhead (enter function, check 'if other:',
return). Guarding with a truthiness check at the call site avoids the
function-call overhead entirely.
"""

import sys
import time

ITERS = 5_000_000


class FakeMessage:
custom_payload = None

def update_custom_payload(self, other):
if other:
if not self.custom_payload:
self.custom_payload = {}
self.custom_payload.update(other)


class FakeQuery:
custom_payload = None


def bench_unconditional(msg, query, custom_payload, n):
"""Two unconditional method calls (old path)."""
t0 = time.perf_counter_ns()
for _ in range(n):
msg.update_custom_payload(query.custom_payload)
msg.update_custom_payload(custom_payload)
return (time.perf_counter_ns() - t0) / n


def bench_guarded(msg, query, custom_payload, n):
"""Guarded: skip calls when payloads are None/falsy (new path)."""
t0 = time.perf_counter_ns()
for _ in range(n):
if query.custom_payload:
msg.update_custom_payload(query.custom_payload)
if custom_payload:
msg.update_custom_payload(custom_payload)
return (time.perf_counter_ns() - t0) / n


def main():
print(f"Python {sys.version}\n")

msg = FakeMessage()
query = FakeQuery()
custom_payload = None # common case: no execute_as

ns_old = bench_unconditional(msg, query, custom_payload, ITERS)
ns_new = bench_guarded(msg, query, custom_payload, ITERS)
saving = ns_old - ns_new
speedup = ns_old / ns_new if ns_new else float('inf')

print(f"=== update_custom_payload guard ({ITERS:,} iters, common case: both None) ===\n")
print(f" Unconditional calls (old): {ns_old:.1f} ns")
print(f" Guarded calls (new): {ns_new:.1f} ns")
print(f" Saving: {saving:.1f} ns ({speedup:.2f}x)")


if __name__ == "__main__":
main()
12 changes: 9 additions & 3 deletions cassandra/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2751,8 +2751,9 @@ def execute_async(self, query, parameters=None, trace=False, custom_payload=None
... log.exception("Operation failed:")

"""
custom_payload = custom_payload if custom_payload else {}
if execute_as:
if not custom_payload:
custom_payload = {}
custom_payload[_proxy_execute_key] = execute_as.encode()

future = self._create_response_future(
Expand Down Expand Up @@ -3018,8 +3019,13 @@ def _create_response_future(self, query, parameters, trace, custom_payload,
continuous_paging_options)

message.tracing = trace
message.update_custom_payload(query.custom_payload)
message.update_custom_payload(custom_payload)
# Guard: skip method calls when payloads are None (common case).
# update_custom_payload() already has an internal 'if other:' guard,
# but avoiding the function-call overhead entirely saves ~100-200ns.
if query.custom_payload:
message.update_custom_payload(query.custom_payload)
if custom_payload:
message.update_custom_payload(custom_payload)
message.allow_beta_protocol_version = self.cluster.allow_beta_protocol_version

spec_exec_plan = spec_exec_policy.new_plan(query.keyspace or self.keyspace, query) if query.is_idempotent and spec_exec_policy else None
Expand Down
Loading