perf: replace KeyError exception with dict.get() in BoundStatement.bind()#754
Closed
mykaul wants to merge 1 commit intoscylladb:masterfrom
Closed
perf: replace KeyError exception with dict.get() in BoundStatement.bind()#754mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul wants to merge 1 commit intoscylladb:masterfrom
Conversation
…nd() Replace try/except KeyError with dict.get() + sentinel pattern in the per-column binding loop of BoundStatement.bind(). This loop runs once per column per execute() call for dict-style bindings, making it a hot path. Using dict.get() avoids the overhead of raising and catching KeyError for every missing/optional column. The sentinel object (_BIND_SENTINEL) is necessary to distinguish a missing key from an explicit None value in the bound dict.
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
try/except KeyErrorwithdict.get()+ sentinel in the per-column binding loop ofBoundStatement.bind()session.execute()call for dict-style bindings, making it a hot path_BIND_SENTINEL = object()distinguishes missing keys from explicitNonevaluesMotivation
When binding a dict to a prepared statement, the driver iterates over every column in the prepared statement metadata and looks up the value in the user-supplied dict. The previous code used
try: values_dict[col.name] / except KeyError:which raises and catches aKeyErrorfor every missing/optional column. While CPython 3.11+ made try/except cheaper when no exception fires, the exception does fire for every omitted column, and raising exceptions is inherently expensive (~5-10x slower than a dict.get() + identity check).For workloads using protocol v4+ (the common case), omitted columns are bound as
UNSET_VALUE, so theKeyErrorpath fires frequently for partial-column updates.Changes
cassandra/query.py: Add module-level_BIND_SENTINEL = object()sentinel, replace the try/except block withdict.get(col.name, _BIND_SENTINEL)+is notcheckTesting
tests/unit/test_query.py,tests/unit/test_cluster.py,tests/unit/test_resultset.py)