Skip to content

perf(spanner): optimize query option merging and prevent in-place mutation - #18358

Open
olavloite wants to merge 1 commit into
mainfrom
spanner-optimize-query-options-merging
Open

perf(spanner): optimize query option merging and prevent in-place mutation#18358
olavloite wants to merge 1 commit into
mainfrom
spanner-optimize-query-options-merging

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Short-circuit query option merging when options are unset to eliminate allocations on the hot path, make field handling generic, and merge into a fresh protobuf to avoid in-place mutation of base options.

@olavloite
olavloite requested a review from a team as a code owner September 13, 2026 13:18

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new helper function _to_query_options to normalize query options (either dictionaries or QueryOptions objects) and refactors _merge_query_options to utilize this helper, simplifying the merging logic. It also adds comprehensive unit tests to cover various edge cases. The review feedback suggests avoiding direct access to the private _pb attribute in favor of the public proto-plus API type(options).pb(options), and optimizing the merging logic in _merge_query_options to prevent unnecessary double allocations.

Comment thread packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py Outdated
Comment thread packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py Outdated
@olavloite
olavloite force-pushed the spanner-optimize-query-options-merging branch from c63b25c to a5947df Compare September 13, 2026 13:26
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a helper function _to_query_options to normalize query options and refactors _merge_query_options to use it, along with adding comprehensive unit tests. The review feedback suggests optimizing _to_query_options by avoiding allocations when dictionary values are empty and simplifying control flow, as well as optimizing the merging path in _merge_query_options by directly using protobuf's CopyFrom and MergeFrom to bypass proto-plus constructor overhead.

Comment thread packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py
Comment thread packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py Outdated
…ation

Short-circuit query option merging when options are unset to eliminate
allocations on the hot path, make field handling generic, and merge into
a fresh protobuf to avoid in-place mutation of base options.
@olavloite
olavloite force-pushed the spanner-optimize-query-options-merging branch from a5947df to 3e476a1 Compare September 13, 2026 13:34
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new helper function _to_query_options to normalize query options (either a dictionary or a QueryOptions object) and refactors _merge_query_options to utilize this helper, simplifying the merging logic. Additionally, comprehensive unit tests have been added to verify the behavior of _to_query_options and various edge cases of _merge_query_options. I have no feedback to provide as the changes are well-implemented and thoroughly tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant