(improvement) Skip tuple(partial(...)) construction for empty callbacks/errbacks (190ns saving per call - 63%)#803
Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
Draft
Conversation
In _set_final_result and _set_final_exception, skip building the tuple of partial(fn, ...) when the callbacks/errbacks list is empty. Most queries use the synchronous result() path and never register callbacks or errbacks, so this avoids constructing an empty tuple and the associated generator overhead on every query completion.
Author
Benchmark results (CPython 3.14, 500k iterations)
The synchronous |
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
In
_set_final_resultand_set_final_exception, skip building thetuple(partial(fn, ...))when the callbacks/errbacks list is empty. Most queries use the synchronousresult()path and never register callbacks or errbacks.Motivation
Every query completion (
_set_final_result/_set_final_exception) previously constructed atuple(partial(fn, response, *args, **kwargs) for ...)from the callbacks/errbacks list, even when that list is empty. This creates an empty generator and an empty tuple on every query. The synchronousresult()API (the overwhelmingly common usage pattern) never registers callbacks, so this work is wasted.Benchmark (CPython 3.14, per-call)
result())The synchronous
result()API is the common path — this saves 190ns per query completion.Changes
cassandra/cluster.py:_set_final_result: Checkif callbacks:before buildingto_call; setto_call = Noneotherwise_set_final_exception: Same pattern for errbacksif to_call:checkTesting
Unit tests pass (58/58 in test_response_future.py and test_cluster.py). The lock semantics are preserved — the check and tuple construction still happen inside
_callback_lock.