Skip to content

perf(spanner): prune lock and begin event allocations on single-use snapshots - #18420

Open
olavloite wants to merge 1 commit into
mainfrom
spanner-remove-lock-for-single-use
Open

olavloite wants to merge 1 commit into
mainfrom
spanner-remove-lock-for-single-use

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Single-use snapshots (multi_use=False) are thread-confined and execute at most a single read or query operation. Previously, _SnapshotBase unconditionally instantiated both a Lock and an Event, and acquired locks during inline begin operations even when multi-use was not enabled.
This change optimizes single-use snapshots:

  • Only allocate self._lock and self._transaction_begin_event when multi_use=True.
  • For single-use snapshots, prevent sequential re-use in _wait_for_transaction_begin() via lightweight flag checks without lock synchronization.
  • Skip lock acquisition in inline begin and precommit token updates when self._lock is None.
  • Explicitly pass multi_use=True in Transaction.init to ensure transactions retain lock synchronization.
  • Document concurrency semantics: single-use snapshots are thread-confined, while multi-use snapshots/transactions synchronize concurrent access.

…napshots

Single-use snapshots (multi_use=False) are thread-confined and execute at most
a single read or query operation. Previously, _SnapshotBase unconditionally
instantiated both a Lock and an Event, and acquired locks during inline begin
operations even when multi-use was not enabled.
This change optimizes single-use snapshots:
- Only allocate self._lock and self._transaction_begin_event when multi_use=True.
- For single-use snapshots, prevent sequential re-use in _wait_for_transaction_begin()
  via lightweight flag checks without lock synchronization.
- Skip lock acquisition in inline begin and precommit token updates when
  self._lock is None.
- Explicitly pass multi_use=True in Transaction.__init__ to ensure transactions
  retain lock synchronization.
- Document concurrency semantics: single-use snapshots are thread-confined,
  while multi-use snapshots/transactions synchronize concurrent access.
@olavloite
olavloite requested a review from a team as a code owner September 18, 2026 08:19

@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 single-use snapshots by avoiding the allocation of locks and events since they are thread-confined and do not run concurrently. The multi_use parameter is now passed to the base snapshot constructor, and lock-dependent methods have been updated to safely handle None values. Comprehensive unit tests have been added to verify this new behavior. I have no feedback to provide as the changes are correct and well-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