Skip to content

perf(spanner): use StringBuilder for generating RequestId#12809

Merged
olavloite merged 4 commits intomainfrom
spanner-optimize-request-id-generation
Apr 16, 2026
Merged

perf(spanner): use StringBuilder for generating RequestId#12809
olavloite merged 4 commits intomainfrom
spanner-optimize-request-id-generation

Conversation

@olavloite
Copy link
Copy Markdown
Contributor

Use a StringBuilder instead of String.format(..) to generate the header value of a RequestId. This significantly reduces the CPU time needed. Generating the header value for 10mio RequestIds using the new/old implementation take:

  • Old: 2750ms
  • New: 203ms

Use a StringBuilder instead of String.format(..) to generate the header value
of a RequestId. This significantly reduces the CPU time needed. Generating the
header value for 10mio RequestIds using the new/old implementation take:

- Old: 2750ms
- New: 203ms
@olavloite olavloite requested review from a team as code owners April 15, 2026 13:25
@olavloite olavloite changed the title chore(spanner): use StringBuilder for generating RequestId perf(spanner): use StringBuilder for generating RequestId Apr 15, 2026
Copy link
Copy Markdown
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 optimizes the generation of Spanner request IDs by pre-calculating static and instance-specific prefixes and replacing String.format with StringBuilder in getHeaderValue and toString. The review feedback suggests using a more precise initial capacity for the StringBuilder instances, calculated from the instancePrefix length, to avoid potential internal array resizes in high-throughput paths.

@olavloite olavloite enabled auto-merge (squash) April 16, 2026 10:14
@olavloite olavloite merged commit 5c821a3 into main Apr 16, 2026
113 of 115 checks passed
@olavloite olavloite deleted the spanner-optimize-request-id-generation branch April 16, 2026 11:01
Neenu1995 pushed a commit that referenced this pull request Apr 17, 2026
Use a StringBuilder instead of String.format(..) to generate the header
value of a RequestId. This significantly reduces the CPU time needed.
Generating the header value for 10mio RequestIds using the new/old
implementation take:

- Old: 2750ms
- New: 203ms

---------

Co-authored-by: cloud-java-bot <cloud-java-bot@google.com>
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.

4 participants