Skip to content

perf(spanner): build ExecuteSqlRequest on the raw protobuf message - #18379

Open
olavloite wants to merge 1 commit into
mainfrom
spanner-build-request-raw
Open

olavloite wants to merge 1 commit into
mainfrom
spanner-build-request-raw

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Building an ExecuteSqlRequest through proto-plus costs about 14 us per query, because every keyword argument goes through a descriptor lookup and type coercion. The new _make_execute_sql_request() helper builds the request on the underlying protobuf message and wraps it, which takes 2 us.

The helper lives in _helpers.py so the sync and async snapshots share one copy. Behaviour is unchanged: the tests compare both the message and its serialized bytes against the proto-plus constructor.

The sync snapshot.py hunk was mirrored by hand, since regenerating that file today would delete unrelated live code. I will open a separate PR to fix that existing error.

Building an ExecuteSqlRequest through proto-plus costs about 14 us per
query, because every keyword argument goes through a descriptor lookup and
type coercion. The new _make_execute_sql_request() helper builds the
request on the underlying protobuf message and wraps it, which takes 2 us.

The helper lives in _helpers.py so the sync and async snapshots share one
copy. Behaviour is unchanged: the tests compare both the message and its
serialized bytes against the proto-plus constructor.

The sync snapshot.py hunk was mirrored by hand, since regenerating that
file today would delete unrelated live code. I will open a separate PR
to fix that existing error.
@olavloite
olavloite requested a review from a team as a code owner September 15, 2026 14:23

@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 optimizes query execution performance by introducing a fast builder helper, _make_execute_sql_request, which constructs ExecuteSqlRequest directly on the underlying protobuf message to bypass proto-plus overhead. This helper is integrated into both the synchronous and asynchronous execute_sql methods, and comprehensive unit tests are added. Feedback on the changes suggests explicitly checking if param_types is not None instead of if param_types to prevent an empty dictionary from being treated as falsy, ensuring consistent behavior with the proto-plus constructor.

Comment thread packages/google-cloud-spanner/google/cloud/spanner_v1/_helpers.py
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