Skip to content

Conversation

@olavloite
Copy link
Collaborator

Optimize the checksum calculation that is used for read/write transactions.

New implementation:

goos: darwin
goarch: arm64
pkg: github.com/googleapis/go-sql-spanner
cpu: Apple M3
BenchmarkChecksumRowIteratorRandom
BenchmarkChecksumRowIteratorRandom/num-rows-1
BenchmarkChecksumRowIteratorRandom/num-rows-1-8         	  134152	      8184 ns/op	   13528 B/op	     123 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-10
BenchmarkChecksumRowIteratorRandom/num-rows-10-8        	   16498	     72459 ns/op	  120690 B/op	    1335 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-100
BenchmarkChecksumRowIteratorRandom/num-rows-100-8       	    1725	    692326 ns/op	 1153975 B/op	   12781 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-1000
BenchmarkChecksumRowIteratorRandom/num-rows-1000-8      	     157	   7566917 ns/op	11375170 B/op	  130466 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-10000
BenchmarkChecksumRowIteratorRandom/num-rows-10000-8     	      14	  83402393 ns/op	112045544 B/op	 1294480 allocs/op

Original implementation:

goos: darwin
goarch: arm64
pkg: github.com/googleapis/go-sql-spanner
cpu: Apple M3
BenchmarkChecksumRowIteratorRandom
BenchmarkChecksumRowIteratorRandom/num-rows-1
BenchmarkChecksumRowIteratorRandom/num-rows-1-8         	   33792	     34931 ns/op	     908 B/op	      54 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-10
BenchmarkChecksumRowIteratorRandom/num-rows-10-8        	    4807	    249917 ns/op	    8768 B/op	     531 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-100
BenchmarkChecksumRowIteratorRandom/num-rows-100-8       	     494	   2408323 ns/op	   87580 B/op	    5301 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-1000
BenchmarkChecksumRowIteratorRandom/num-rows-1000-8      	      44	  25243051 ns/op	  870935 B/op	   53003 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-10000
BenchmarkChecksumRowIteratorRandom/num-rows-10000-8     	       4	 259823406 ns/op	 8691096 B/op	  530018 allocs/op

Optimize the checksum calculation that is used for read/write transactions.

New implementation:

```
goos: darwin
goarch: arm64
pkg: github.com/googleapis/go-sql-spanner
cpu: Apple M3
BenchmarkChecksumRowIteratorRandom
BenchmarkChecksumRowIteratorRandom/num-rows-1
BenchmarkChecksumRowIteratorRandom/num-rows-1-8         	  134152	      8184 ns/op	   13528 B/op	     123 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-10
BenchmarkChecksumRowIteratorRandom/num-rows-10-8        	   16498	     72459 ns/op	  120690 B/op	    1335 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-100
BenchmarkChecksumRowIteratorRandom/num-rows-100-8       	    1725	    692326 ns/op	 1153975 B/op	   12781 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-1000
BenchmarkChecksumRowIteratorRandom/num-rows-1000-8      	     157	   7566917 ns/op	11375170 B/op	  130466 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-10000
BenchmarkChecksumRowIteratorRandom/num-rows-10000-8     	      14	  83402393 ns/op	112045544 B/op	 1294480 allocs/op
```

Original implementation:

```
goos: darwin
goarch: arm64
pkg: github.com/googleapis/go-sql-spanner
cpu: Apple M3
BenchmarkChecksumRowIteratorRandom
BenchmarkChecksumRowIteratorRandom/num-rows-1
BenchmarkChecksumRowIteratorRandom/num-rows-1-8         	   33792	     34931 ns/op	     908 B/op	      54 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-10
BenchmarkChecksumRowIteratorRandom/num-rows-10-8        	    4807	    249917 ns/op	    8768 B/op	     531 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-100
BenchmarkChecksumRowIteratorRandom/num-rows-100-8       	     494	   2408323 ns/op	   87580 B/op	    5301 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-1000
BenchmarkChecksumRowIteratorRandom/num-rows-1000-8      	      44	  25243051 ns/op	  870935 B/op	   53003 allocs/op
BenchmarkChecksumRowIteratorRandom/num-rows-10000
BenchmarkChecksumRowIteratorRandom/num-rows-10000-8     	       4	 259823406 ns/op	 8691096 B/op	  530018 allocs/op
```
@olavloite olavloite requested a review from a team as a code owner December 9, 2025 15:23
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @olavloite, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant performance optimization to the checksum calculation logic used in read/write transactions. By replacing the encoding/gob based serialization with a more direct and efficient binary encoding method, the system achieves faster checksum computations and reduced memory allocations. The changes are supported by new benchmarks demonstrating the improved performance, ensuring the reliability and efficiency of transaction processing.

Highlights

  • Performance Improvement: The checksum calculation for read/write transactions has been optimized, resulting in significant performance gains. Benchmarks show a substantial reduction in ns/op and allocs/op across various row counts, particularly for larger result sets.
  • Checksum Implementation Refactor: The underlying checksum mechanism has been refactored from using encoding/gob for serialization to a custom binary encoding approach directly writing to a crypto/sha256 hash. This change eliminates the overhead associated with gob.
  • Deterministic Hashing for Structs: The new hashing logic for structpb.Value_StructValue now sorts fields before writing them to the hash, ensuring deterministic checksums regardless of map iteration order.
  • New Test Utilities and Benchmarks: New test utilities have been added to testutil/mocked_inmem_server.go for generating random result sets, which are utilized in new benchmarks to thoroughly test the performance of the updated checksum calculation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

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

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 significantly improves the performance of checksum calculations by replacing the encoding/gob-based implementation with a more direct and efficient hashing mechanism. The benchmark results clearly demonstrate the benefits of this change. The new implementation is well-structured and the tests have been updated accordingly. I've found one critical issue that could lead to a panic, and a couple of minor improvement opportunities in the test utility code. Overall, this is a great performance enhancement.

olavloite and others added 4 commits December 9, 2025 16:32
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…-sql-spanner into optimize-checksum-iterator
@olavloite olavloite requested a review from bhatt4982 December 9, 2025 16:39
bhatt4982
bhatt4982 previously approved these changes Dec 10, 2025
Copy link
Contributor

@bhatt4982 bhatt4982 left a comment

Choose a reason for hiding this comment

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

LGTM...

@olavloite olavloite added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Dec 10, 2025
The int and float buffers were defined as global variables, which meant that they
could have been used concurrently if multiple checksumIterators were used at the
same time.
@olavloite olavloite removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Dec 10, 2025
Copy link
Contributor

@bhatt4982 bhatt4982 left a comment

Choose a reason for hiding this comment

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

LGTM...

@olavloite olavloite merged commit 6fb5cc8 into main Dec 11, 2025
43 checks passed
@olavloite olavloite deleted the optimize-checksum-iterator branch December 11, 2025 08:03
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.

2 participants