Skip to content

Commit 59d1d1a

Browse files
perf(bigtable): build the entry proto once in createResource
MutateRowsBatchingDescriptor.createResource() built the same MutateRowsRequest.Entry twice for every element added to a bulk-mutation batcher: once through countBytes() to read the serialized size, and once directly to read the mutation count. RowMutationEntry.toProto() memoizes nothing, so the second construction was pure overhead, and BatcherImpl.add() calls createResource for every accepted element. Build the proto once and read both values from it. Behaviour-preserving. Measured, this halves the bytes allocated per element. MutateRowsBatchingDescriptorTest had no coverage of createResource or createEmptyResource; both are added.
1 parent 9337a93 commit 59d1d1a

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/mutaterows/MutateRowsBatchingDescriptor.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.api.gax.batching.BatchResource;
2121
import com.google.api.gax.batching.BatchingDescriptor;
2222
import com.google.api.gax.batching.BatchingRequestBuilder;
23+
import com.google.bigtable.v2.MutateRowsRequest;
2324
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
2425
import com.google.cloud.bigtable.data.v2.models.MutateRowsException;
2526
import com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation;
@@ -101,8 +102,8 @@ public long countBytes(RowMutationEntry entry) {
101102

102103
@Override
103104
public BatchResource createResource(RowMutationEntry element) {
104-
long byteCount = countBytes(element);
105-
return MutateRowsBatchResource.create(1, byteCount, element.toProto().getMutationsCount());
105+
MutateRowsRequest.Entry proto = element.toProto();
106+
return MutateRowsBatchResource.create(1, proto.getSerializedSize(), proto.getMutationsCount());
106107
}
107108

108109
@Override

‎java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/mutaterows/MutateRowsBatchingDescriptorTest.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.api.gax.rpc.DeadlineExceededException;
2828
import com.google.api.gax.rpc.InternalException;
2929
import com.google.api.gax.rpc.UnavailableException;
30+
import com.google.bigtable.v2.MutateRowsRequest;
3031
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
3132
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
3233
import com.google.cloud.bigtable.data.v2.models.MutateRowsException;
@@ -64,6 +65,34 @@ public void countBytesTest() {
6465
assertThat(underTest.countBytes(request)).isEqualTo(bytes);
6566
}
6667

68+
@Test
69+
public void createResourceTest() {
70+
RowMutationEntry request =
71+
RowMutationEntry.create(ROW_KEY)
72+
.setCell(FAMILY, QUALIFIER, VALUE)
73+
.setCell(FAMILY, QUALIFIER, "another-value")
74+
.deleteFamily(FAMILY);
75+
MutateRowsRequest.Entry proto = request.toProto();
76+
77+
MutateRowsBatchingDescriptor underTest = new MutateRowsBatchingDescriptor();
78+
BatchResource resource = underTest.createResource(request);
79+
80+
assertThat(resource.getElementCount()).isEqualTo(1);
81+
assertThat(resource.getByteCount()).isEqualTo(proto.getSerializedSize());
82+
assertThat(((MutateRowsBatchResource) resource).getMutationCount())
83+
.isEqualTo(proto.getMutationsCount());
84+
}
85+
86+
@Test
87+
public void createEmptyResourceTest() {
88+
MutateRowsBatchingDescriptor underTest = new MutateRowsBatchingDescriptor();
89+
BatchResource resource = underTest.createEmptyResource();
90+
91+
assertThat(resource.getElementCount()).isEqualTo(0);
92+
assertThat(resource.getByteCount()).isEqualTo(0);
93+
assertThat(((MutateRowsBatchResource) resource).getMutationCount()).isEqualTo(0);
94+
}
95+
6796
@Test
6897
public void requestBuilderTest() {
6998
MutateRowsBatchingDescriptor underTest = new MutateRowsBatchingDescriptor();

0 commit comments

Comments
 (0)