forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 54
(improvement) (cython only) cache deserializer instances in find_deserializer and m… (hundreds of ns improvements - 6-40x faster!) #741
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/cache-deserializer-lookup
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,212 @@ | ||
| # 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. | ||
|
|
||
| """ | ||
| Benchmarks for find_deserializer / make_deserializers with and without caching. | ||
|
|
||
| Run with: pytest benchmarks/test_deserializer_cache_benchmark.py -v | ||
|
|
||
| Requires the ``pytest-benchmark`` plugin and Cython extensions to be built. | ||
| Skipped automatically when either dependency is unavailable. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| pytest.importorskip("pytest_benchmark") | ||
| pytest.importorskip("cassandra.deserializers") | ||
|
|
||
| from cassandra import cqltypes | ||
| from cassandra.deserializers import ( | ||
| find_deserializer, | ||
| make_deserializers, | ||
| ) | ||
|
mykaul marked this conversation as resolved.
|
||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Reference: original uncached implementations (copied from master) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| _classes = {} | ||
|
|
||
|
|
||
| def _init_classes(): | ||
| """Lazily initialize the class lookup dict from deserializers module.""" | ||
| if not _classes: | ||
| from cassandra import deserializers as mod | ||
|
|
||
| for name in dir(mod): | ||
| obj = getattr(mod, name) | ||
| if isinstance(obj, type): | ||
| _classes[name] = obj | ||
|
|
||
|
|
||
| def find_deserializer_uncached(cqltype): | ||
| """Original implementation without caching.""" | ||
| _init_classes() | ||
|
|
||
| name = "Des" + cqltype.__name__ | ||
| if name in _classes: | ||
| cls = _classes[name] | ||
| elif issubclass(cqltype, cqltypes.ListType): | ||
| from cassandra.deserializers import DesListType | ||
|
|
||
| cls = DesListType | ||
| elif issubclass(cqltype, cqltypes.SetType): | ||
| from cassandra.deserializers import DesSetType | ||
|
|
||
| cls = DesSetType | ||
| elif issubclass(cqltype, cqltypes.MapType): | ||
| from cassandra.deserializers import DesMapType | ||
|
|
||
| cls = DesMapType | ||
| elif issubclass(cqltype, cqltypes.UserType): | ||
| from cassandra.deserializers import DesUserType | ||
|
|
||
| cls = DesUserType | ||
| elif issubclass(cqltype, cqltypes.TupleType): | ||
| from cassandra.deserializers import DesTupleType | ||
|
|
||
| cls = DesTupleType | ||
| elif issubclass(cqltype, cqltypes.DynamicCompositeType): | ||
| from cassandra.deserializers import DesDynamicCompositeType | ||
|
|
||
| cls = DesDynamicCompositeType | ||
| elif issubclass(cqltype, cqltypes.CompositeType): | ||
| from cassandra.deserializers import DesCompositeType | ||
|
|
||
| cls = DesCompositeType | ||
| elif issubclass(cqltype, cqltypes.ReversedType): | ||
| from cassandra.deserializers import DesReversedType | ||
|
|
||
| cls = DesReversedType | ||
| elif issubclass(cqltype, cqltypes.FrozenType): | ||
| from cassandra.deserializers import DesFrozenType | ||
|
|
||
| cls = DesFrozenType | ||
| else: | ||
| from cassandra.deserializers import GenericDeserializer | ||
|
|
||
| cls = GenericDeserializer | ||
|
|
||
| return cls(cqltype) | ||
|
|
||
|
|
||
| def make_deserializers_uncached(ctypes): | ||
| """Original implementation without caching.""" | ||
| from cassandra.deserializers import obj_array | ||
|
|
||
| return obj_array([find_deserializer_uncached(ct) for ct in ctypes]) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Test type sets | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| SIMPLE_TYPES = [ | ||
| cqltypes.Int32Type, | ||
| cqltypes.UTF8Type, | ||
| cqltypes.BooleanType, | ||
| cqltypes.DoubleType, | ||
| cqltypes.LongType, | ||
| ] | ||
|
|
||
| MIXED_TYPES = [ | ||
| cqltypes.Int32Type, | ||
| cqltypes.UTF8Type, | ||
| cqltypes.BooleanType, | ||
| cqltypes.DoubleType, | ||
| cqltypes.LongType, | ||
| cqltypes.FloatType, | ||
| cqltypes.TimestampType, | ||
| cqltypes.UUIDType, | ||
| cqltypes.InetAddressType, | ||
| cqltypes.DecimalType, | ||
| ] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Correctness tests | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class TestDeserializerCacheCorrectness: | ||
| """Verify the cached implementation returns equivalent deserializers.""" | ||
|
|
||
| @pytest.mark.parametrize("cqltype", SIMPLE_TYPES + MIXED_TYPES) | ||
| def test_find_deserializer_returns_correct_type(self, cqltype): | ||
| cached = find_deserializer(cqltype) | ||
| uncached = find_deserializer_uncached(cqltype) | ||
| assert type(cached).__name__ == type(uncached).__name__ | ||
|
|
||
| def test_find_deserializer_cache_hit_same_object(self): | ||
| d1 = find_deserializer(cqltypes.Int32Type) | ||
| d2 = find_deserializer(cqltypes.Int32Type) | ||
| assert d1 is d2 | ||
|
|
||
| def test_make_deserializers_returns_correct_length(self): | ||
| result = make_deserializers(SIMPLE_TYPES) | ||
| assert len(result) == len(SIMPLE_TYPES) | ||
|
|
||
| def test_make_deserializers_cache_hit_same_object(self): | ||
| r1 = make_deserializers(SIMPLE_TYPES) | ||
| r2 = make_deserializers(SIMPLE_TYPES) | ||
| # Should be the exact same cached object | ||
| assert r1 is r2 | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Benchmarks | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class TestFindDeserializerBenchmark: | ||
| """Benchmark find_deserializer cached vs uncached.""" | ||
|
|
||
| # --- Single simple type --- | ||
|
|
||
| @pytest.mark.benchmark(group="find_deser_simple") | ||
| def test_uncached_simple(self, benchmark): | ||
| benchmark(find_deserializer_uncached, cqltypes.Int32Type) | ||
|
|
||
| @pytest.mark.benchmark(group="find_deser_simple") | ||
| def test_cached_simple(self, benchmark): | ||
| # Cache is already warm from correctness tests or previous iterations | ||
| find_deserializer(cqltypes.Int32Type) # ensure warm | ||
| benchmark(find_deserializer, cqltypes.Int32Type) | ||
|
|
||
|
|
||
| class TestMakeDeserializersBenchmark: | ||
| """Benchmark make_deserializers cached vs uncached.""" | ||
|
|
||
| # --- 5 simple types --- | ||
|
|
||
| @pytest.mark.benchmark(group="make_deser_5types") | ||
| def test_uncached_5types(self, benchmark): | ||
| benchmark(make_deserializers_uncached, SIMPLE_TYPES) | ||
|
|
||
| @pytest.mark.benchmark(group="make_deser_5types") | ||
| def test_cached_5types(self, benchmark): | ||
| make_deserializers(SIMPLE_TYPES) # ensure warm | ||
| benchmark(make_deserializers, SIMPLE_TYPES) | ||
|
|
||
| # --- 10 mixed types --- | ||
|
|
||
| @pytest.mark.benchmark(group="make_deser_10types") | ||
| def test_uncached_10types(self, benchmark): | ||
| benchmark(make_deserializers_uncached, MIXED_TYPES) | ||
|
|
||
| @pytest.mark.benchmark(group="make_deser_10types") | ||
| def test_cached_10types(self, benchmark): | ||
| make_deserializers(MIXED_TYPES) # ensure warm | ||
| benchmark(make_deserializers, MIXED_TYPES) | ||
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
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.
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.