FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

perf(spanner): optimize row popping in StreamedResultSet by olavloite · Pull Request #18316 · googleapis/google-cloud-python · GitHub

Repository navigation

perf(spanner): optimize row popping in StreamedResultSet - #18316

Merged
olavloite merged 1 commit into
mainfrom
spanner-row-popping
Sep 10, 2026
Merged

olavloite merged 1 commit into
mainfrom
spanner-row-popping

Conversation

olavloite commented Sep 9, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

Eliminates an O(N^2) bottleneck when consuming query results:

  • Previously, StreamedResultSet yielded rows by calling iter_rows.pop(0) in a loop. Because Python lists are contiguous arrays, popping index 0 shifts all remaining elements in memory on every row yielded, causing quadratic overhead for larger result sets.
  • Replaces pop(0) with direct iteration (for row in iter_rows: yield row) and detaches the list directly (iter_rows, self._rows = self._rows, []), allowing row iteration to run in linear O(N) time at C speed.

Benchmark Performance Verification

Performance verification of this PR branch (spanner-row-popping) compared against the 7-day nightly baseline of the Spanner Python client running on GCE (n2-standard-2, 2 vCPUs, closed-loop single worker):

Results Summary

Benchmark Scenario Metric PR #18316 (spanner-row-popping) Baseline (main) Delta Speedup
Narrow Result Set
read-narrow-result-set
(200,000 rows, narrow columns)
Mean
P50
P90
P99
300.82 ms
374.98 ms
474.97 ms
497.50 ms
2,866.85 ms
730.31 ms
2,866.85 ms
2,866.85 ms
-89.51%
-48.65%
-83.43%
-82.65%
~9.5x faster
~1.9x faster
~6.0x faster
~5.8x faster
Wide Result Set
read-large-result-set
(100,000 rows, wide columns)
Mean
P50
P90
P99
2,223.01 ms
2,147.62 ms
2,336.65 ms
2,482.95 ms
2,565.57 ms
2,603.18 ms
2,725.26 ms
2,565.57 ms
-13.35%
-17.50%
-14.26%
-3.22%
~1.15x faster
~1.21x faster
~1.17x faster
~1.03x faster

Observations

  • Narrow Result Sets (~90% latency reduction / ~10x throughput): With 200,000 rows and few columns, row popping in StreamedResultSet was the dominant CPU bottleneck. Removing that overhead yields a massive performance leap.
  • Wide Result Sets (~13% - 17.5% latency reduction): While complex column deserialization accounts for more relative CPU time in wide rows, the row popping optimization still delivers a consistent ~13% to ~17.5% speedup across Mean, P50, and P90.

Eliminates an O(N^2) bottleneck when consuming query results:
- Previously, StreamedResultSet yielded rows by calling `iter_rows.pop(0)` in a loop. Because Python lists are contiguous arrays, popping index 0 shifts all remaining elements in memory on every row yielded, causing quadratic overhead for larger result sets.
- Replaces `pop(0)` with direct iteration (`for row in iter_rows: yield row`) and detaches the list directly (`iter_rows, self._rows = self._rows, []`), allowing row iteration to run in linear O(N) time at C speed.
olavloite requested a review from a team as a code owner September 9, 2026 09:33

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Code Review

This pull request optimizes row streaming iteration in both the synchronous and asynchronous implementations of StreamedResultSet by replacing an inefficient O(N^2) list-popping loop with a standard O(N) for-loop. It also introduces comprehensive unit tests covering large batches, stepwise consumption, chunk boundaries, early termination, and mid-stream errors. As there are no review comments, I have no feedback to provide.

olavloite added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Sep 9, 2026
yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Sep 9, 2026
olavloite added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Sep 9, 2026
yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

(The system test failures are unrelated)

olavloite added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Sep 9, 2026
yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Sep 9, 2026
olavloite merged commit e66103d into main Sep 10, 2026
47 checks passed
olavloite deleted the spanner-row-popping branch September 10, 2026 10:12
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL