forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 57
perf: Skip redundant __init__ assignments and remove dead attributes in ResponseFuture (-32ns/call - 4.7% improvement for the common case, +43ns (+4.5%) for the non-common case, - code cleanup also) #806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mykaul
wants to merge
2
commits into
scylladb:master
Choose a base branch
from
mykaul:perf/response-future-init-cleanup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Copyright ScyllaDB, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Micro-benchmark: isinstance dispatch order in _create_response_future. | ||
|
|
||
| Measures the cost of checking BoundStatement first vs SimpleStatement first | ||
| in the isinstance chain that dispatches query types to message constructors. | ||
|
|
||
| For prepared-statement workloads (the perf-critical case), BoundStatement is | ||
| the most common type. Checking it first saves one wasted isinstance call. | ||
|
|
||
| Run: | ||
| python benchmarks/micro/bench_isinstance_dispatch.py | ||
| """ | ||
|
|
||
| import sys | ||
| import timeit | ||
|
|
||
| from cassandra.query import SimpleStatement, BoundStatement, BatchStatement, Statement | ||
|
|
||
|
|
||
| class _FakeGraphStatement(Statement): | ||
| """Stand-in for GraphStatement to avoid importing DSE dependencies.""" | ||
| pass | ||
|
|
||
|
|
||
| def make_bound_statement(): | ||
| """Create a minimal BoundStatement-like object for benchmarking.""" | ||
| # We only need isinstance() to work; no actual prepared statement needed. | ||
| bs = object.__new__(BoundStatement) | ||
| return bs | ||
|
|
||
|
|
||
| def make_simple_statement(): | ||
| return SimpleStatement("SELECT * FROM t") | ||
|
|
||
|
|
||
| def make_batch_statement(): | ||
| return BatchStatement() | ||
|
|
||
|
|
||
| def bench(): | ||
| bound = make_bound_statement() | ||
| simple = make_simple_statement() | ||
| batch = make_batch_statement() | ||
| graph = _FakeGraphStatement() | ||
|
|
||
| # Simulate typical workload mix: ~80% BoundStatement, ~15% SimpleStatement, | ||
| # ~4% BatchStatement, ~1% GraphStatement | ||
| queries = ([bound] * 80 + [simple] * 15 + [batch] * 4 + [graph] * 1) | ||
|
|
||
| def dispatch_simple_first(): | ||
| """Original order: SimpleStatement checked first.""" | ||
| for q in queries: | ||
| if isinstance(q, SimpleStatement): | ||
| pass | ||
| elif isinstance(q, BoundStatement): | ||
| pass | ||
| elif isinstance(q, BatchStatement): | ||
| pass | ||
| elif isinstance(q, _FakeGraphStatement): | ||
| pass | ||
|
|
||
| def dispatch_bound_first(): | ||
| """Optimized order: BoundStatement checked first.""" | ||
| for q in queries: | ||
| if isinstance(q, BoundStatement): | ||
| pass | ||
| elif isinstance(q, SimpleStatement): | ||
| pass | ||
| elif isinstance(q, BatchStatement): | ||
| pass | ||
| elif isinstance(q, _FakeGraphStatement): | ||
| pass | ||
|
|
||
| n = 200_000 | ||
| t_simple_first = timeit.timeit(dispatch_simple_first, number=n) | ||
| t_bound_first = timeit.timeit(dispatch_bound_first, number=n) | ||
|
|
||
| total_calls = n * len(queries) | ||
| print(f"=== isinstance dispatch order (100 queries x {n} iters = {total_calls:,} dispatches) ===") | ||
| print(f"SimpleStatement first: {t_simple_first:.3f}s ({t_simple_first / total_calls * 1e9:.1f} ns/dispatch)") | ||
| print(f"BoundStatement first: {t_bound_first:.3f}s ({t_bound_first / total_calls * 1e9:.1f} ns/dispatch)") | ||
|
|
||
| if t_bound_first < t_simple_first: | ||
| speedup = t_simple_first / t_bound_first | ||
| saving_ns = (t_simple_first - t_bound_first) / total_calls * 1e9 | ||
| print(f"Speedup: {speedup:.2f}x ({saving_ns:.1f} ns/dispatch saved)") | ||
| else: | ||
| print(f"No improvement (ratio: {t_simple_first / t_bound_first:.2f}x)") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(f"Python {sys.version}") | ||
| bench() |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.