Skip to content

perf: avoid unnecessary dict allocation in execute_async custom_payload#785

Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:perf/guard-custom-payload
Draft

perf: avoid unnecessary dict allocation in execute_async custom_payload#785
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:perf/guard-custom-payload

Conversation

@mykaul
Copy link
Copy Markdown

@mykaul mykaul commented Apr 3, 2026

Motivation

Session.execute_async() unconditionally allocates an empty dict {} on every call (line 2754):

custom_payload = custom_payload if custom_payload else {}

In >99% of calls, neither custom_payload nor execute_as is provided. The empty dict {} is passed to update_custom_payload({}) which short-circuits immediately because {} is falsy (if other: on protocol.py:94). The dict allocation is pure waste on the hot request path.

Summary

  • Remove the unconditional custom_payload = custom_payload if custom_payload else {} allocation
  • Only allocate a dict when execute_as is truthy (the rare DSE proxy-execute case)
  • custom_payload now remains None by default; update_custom_payload(None) correctly short-circuits at if other:

Testing

  • All 30 unit tests in tests/unit/test_cluster.py pass
  • Behavioral change is minimal: custom_payload passed to _create_response_future is None instead of {} — both are falsy and handled identically by update_custom_payload() and encode_message()

Benchmarks

Isolated benchmark measuring the allocation pattern (5M iterations):

Scenario Before After Speedup
Common case (no payload, no execute_as) 280.1 ns/call 87.8 ns/call 3.19x
With custom_payload dict passed 127.4 ns/call 104.0 ns/call 1.22x
With execute_as (DSE proxy, rare) 285.9 ns/call 187.9 ns/call 1.52x

The common case (no custom payload, no execute_as) sees a 3.19x speedup by eliminating one dict() allocation per request on the hot path.

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.

@mykaul mykaul force-pushed the perf/guard-custom-payload branch from e8973a0 to a84f703 Compare April 3, 2026 14:38
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