Skip to content

Commit ce1a8f7

Browse files
olavloitecloud-java-bot
authored andcommitted
perf(spanner): use StringBuilder for generating RequestId (#12809)
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>
1 parent 058b80e commit ce1a8f7

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/XGoogSpannerRequestId.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,20 @@ public class XGoogSpannerRequestId {
4242
@VisibleForTesting
4343
static final long VERSION = 1; // The version of the specification being implemented.
4444

45+
private static final String STATIC_PREFIX = VERSION + "." + RAND_PROCESS_ID + ".";
46+
4547
private final long nthClientId;
4648
private final long nthRequest;
4749
private long nthChannelId;
4850
private long attempt;
51+
private final String instancePrefix;
4952

5053
XGoogSpannerRequestId(long nthClientId, long nthChannelId, long nthRequest, long attempt) {
5154
this.nthClientId = nthClientId;
5255
this.nthChannelId = nthChannelId;
5356
this.nthRequest = nthRequest;
5457
this.attempt = attempt;
58+
this.instancePrefix = STATIC_PREFIX + nthClientId + ".";
5559
}
5660

5761
public static XGoogSpannerRequestId of(
@@ -122,26 +126,27 @@ private static String generateRandProcessId() {
122126

123127
/** Returns the string representation of this RequestId as it should be sent to Spanner. */
124128
public String getHeaderValue() {
125-
return String.format(
126-
"%d.%s.%d.%d.%d.%d",
127-
XGoogSpannerRequestId.VERSION,
128-
XGoogSpannerRequestId.RAND_PROCESS_ID,
129-
this.nthClientId,
130-
this.nthChannelId,
131-
this.nthRequest,
132-
this.attempt);
129+
return new StringBuilder(this.instancePrefix.length() + 48)
130+
.append(this.instancePrefix)
131+
.append(this.nthChannelId)
132+
.append('.')
133+
.append(this.nthRequest)
134+
.append('.')
135+
.append(this.attempt)
136+
.toString();
133137
}
134138

135139
@Override
136140
public String toString() {
137-
return String.format(
138-
"%d.%s.%d.%s.%d.%d",
139-
XGoogSpannerRequestId.VERSION,
140-
XGoogSpannerRequestId.RAND_PROCESS_ID,
141-
this.nthClientId,
142-
this.nthChannelId < 0 ? "x" : String.valueOf(this.nthChannelId),
143-
this.nthRequest,
144-
this.attempt);
141+
StringBuilder sb = new StringBuilder(this.instancePrefix.length() + 48);
142+
sb.append(this.instancePrefix);
143+
if (this.nthChannelId < 0) {
144+
sb.append('x');
145+
} else {
146+
sb.append(this.nthChannelId);
147+
}
148+
sb.append('.').append(this.nthRequest).append('.').append(this.attempt);
149+
return sb.toString();
145150
}
146151

147152
public String debugToString() {

0 commit comments

Comments
 (0)