forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 57
perf: optimize time-series write/read hot paths (10's to 100's of ns savings, 2-25x better) #798
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
7
commits into
scylladb:master
Choose a base branch
from
mykaul:perf/timeseries-optimizations
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7b2dd84
benchmarks: add time-series microbenchmarks for serialize, varint, ti…
mykaul f2d9f71
perf: replace calendar.timegm(utctimetuple()) with integer arithmetic…
mykaul ceac92c
perf: modernize varint_pack/varint_unpack with int.to_bytes/int.from_…
mykaul 23afdbe
perf: use time.time_ns() in MonotonicTimestampGenerator for precision…
mykaul f81b455
perf: add Cython-accelerated SerDateType timestamp serializer
mykaul 9be97e6
perf: memoize cql_parameterized_type() on all type classes
mykaul f07d847
perf: skip ColDesc creation in bind() when column encryption is disabled
mykaul 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,106 @@ | ||
| # 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: BoundStatement.bind() fast path without column encryption. | ||
|
|
||
| Measures the improvement from skipping ColDesc namedtuple creation and | ||
| ce_policy checks when column_encryption_policy is None (the common case). | ||
|
|
||
| Run: | ||
| python benchmarks/bench_bind_no_encryption.py | ||
| """ | ||
|
|
||
| import datetime | ||
| import sys | ||
| import timeit | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from cassandra.query import BoundStatement, PreparedStatement | ||
| from cassandra.cqltypes import ( | ||
| DateType, Int32Type, DoubleType, FloatType, UTF8Type, | ||
| BooleanType, LongType, | ||
| ) | ||
|
|
||
|
|
||
| def make_prepared_statement(col_names, col_types): | ||
| """Build a mock PreparedStatement with the given columns.""" | ||
| col_meta = [] | ||
| for name, ctype in zip(col_names, col_types): | ||
| cm = MagicMock() | ||
| cm.name = name | ||
| cm.keyspace_name = 'ks' | ||
| cm.table_name = 'metrics' | ||
| cm.type = ctype | ||
| col_meta.append(cm) | ||
|
|
||
| ps = MagicMock(spec=PreparedStatement) | ||
| ps.column_metadata = col_meta | ||
| ps.routing_key_indexes = None | ||
| ps.protocol_version = 4 | ||
| ps.column_encryption_policy = None | ||
| ps.serial_consistency_level = None | ||
| ps.retry_policy = None | ||
| ps.consistency_level = None | ||
| ps.fetch_size = None | ||
| ps.custom_payload = None | ||
| ps.is_idempotent = False | ||
| return ps | ||
|
|
||
|
|
||
| def bench(): | ||
| schemas = [ | ||
| ( | ||
| "3-col (int, double, text)", | ||
| ['id', 'value', 'tag'], | ||
| [Int32Type, DoubleType, UTF8Type], | ||
| [42, 3.14159, 'sensor-001'], | ||
| ), | ||
| ( | ||
| "5-col time-series", | ||
| ['ts', 'sensor_id', 'value', 'quality', 'tag'], | ||
| [DateType, Int32Type, DoubleType, FloatType, UTF8Type], | ||
| [datetime.datetime(2025, 4, 5, 12, 0, 0, 123456), 42, 3.14, 0.95, 'alpha'], | ||
| ), | ||
| ( | ||
| "8-col wide row", | ||
| ['ts', 'id', 'v1', 'v2', 'v3', 'v4', 'flag', 'name'], | ||
| [DateType, LongType, DoubleType, DoubleType, FloatType, FloatType, BooleanType, UTF8Type], | ||
| [datetime.datetime(2025, 1, 1), 12345678, 1.1, 2.2, 3.3, 4.4, True, 'test-row'], | ||
| ), | ||
| ] | ||
|
|
||
| n = 200_000 | ||
| print(f"=== BoundStatement.bind() no-encryption fast path ({n:,} iters) ===\n") | ||
|
|
||
| for label, col_names, col_types, row in schemas: | ||
| ps = make_prepared_statement(col_names, col_types) | ||
|
|
||
| def do_bind(): | ||
| bs = BoundStatement(ps) | ||
| bs.bind(row) | ||
|
|
||
| # Warmup | ||
| for _ in range(1000): | ||
| do_bind() | ||
|
|
||
| t = timeit.timeit(do_bind, number=n) | ||
| ns_per = t / n * 1e9 | ||
| print(f" {label}:") | ||
| print(f" {ns_per:.1f} ns/call ({n:,} iters)") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(f"Python {sys.version}\n") | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # 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: cql_parameterized_type memoization. | ||
|
|
||
| Measures the cost of building the CQL type string representation | ||
| with and without memoization for various type complexities. | ||
|
|
||
| Run: | ||
| python benchmarks/bench_cql_parameterized_type.py | ||
| """ | ||
|
|
||
| import sys | ||
| import timeit | ||
|
|
||
| from cassandra.cqltypes import ( | ||
| MapType, SetType, ListType, TupleType, | ||
| Int32Type, UTF8Type, FloatType, DoubleType, BooleanType, | ||
| ) | ||
|
|
||
|
|
||
| def bench(): | ||
| # Create parameterized types | ||
| map_type = MapType.apply_parameters([UTF8Type, Int32Type]) | ||
| set_type = SetType.apply_parameters([FloatType]) | ||
| list_type = ListType.apply_parameters([DoubleType]) | ||
| tuple_type = TupleType.apply_parameters([Int32Type, UTF8Type, BooleanType]) | ||
| nested_type = MapType.apply_parameters([ | ||
| UTF8Type, | ||
| ListType.apply_parameters([ | ||
| TupleType.apply_parameters([Int32Type, FloatType, DoubleType]) | ||
| ]) | ||
| ]) | ||
|
|
||
| test_types = [ | ||
| ("Int32Type (simple)", Int32Type), | ||
| ("MapType<text, int>", map_type), | ||
| ("SetType<float>", set_type), | ||
| ("ListType<double>", list_type), | ||
| ("TupleType<int, text, bool>", tuple_type), | ||
| ("MapType<text, list<tuple<int, float, double>>>", nested_type), | ||
| ] | ||
|
|
||
| n = 500_000 | ||
| print(f"=== cql_parameterized_type ({n:,} iters) ===\n") | ||
|
|
||
| for label, typ in test_types: | ||
| # Clear cache to measure uncached | ||
| typ._cql_type_str = None | ||
| # One call to populate cache | ||
| result = typ.cql_parameterized_type() | ||
|
|
||
| # Measure cached (warm) | ||
| t_cached = timeit.timeit(typ.cql_parameterized_type, number=n) | ||
|
|
||
| # Measure uncached (cold) | ||
| def uncached(): | ||
| typ._cql_type_str = None | ||
| return typ.cql_parameterized_type() | ||
| t_uncached = timeit.timeit(uncached, number=n) | ||
|
|
||
| saving_ns = (t_uncached - t_cached) / n * 1e9 | ||
| speedup = t_uncached / t_cached if t_cached > 0 else float('inf') | ||
| print(f" {label}:") | ||
| print(f" result: {result}") | ||
| print(f" uncached: {t_uncached / n * 1e9:.1f} ns, " | ||
| f"cached: {t_cached / n * 1e9:.1f} ns, " | ||
| f"saving: {saving_ns:.1f} ns ({speedup:.1f}x)") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(f"Python {sys.version}\n") | ||
| bench() | ||
Oops, something went wrong.
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.