Repository navigation
perf(bigquery): [T1-10] eliminate LinkedList staging and un-sized transforms in InsertAllRequest, insertAll, and InsertAllResponse - #8
Open
lqiu96 wants to merge 1 commit into
Conversation
…nsforms in InsertAllRequest, insertAll, and InsertAllResponse
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary & Bottleneck
Streaming ingestion ($O(\text{rows})$ :
InsertAllRequest,BigQueryImpl.insertAll, andInsertAllResponse) performs several redundant collection allocations and un-sized transformations that scale with row countInsertAllRequest.Builder.setRows(Iterable<RowToInsert>)copies incoming rows into ajava.util.LinkedList(Lists.newLinkedList), allocating a 24-byteLinkedList$Nodewrapper per row, right beforenew InsertAllRequest(Builder)copies the list again intoImmutableList.copyOf(builder.rows).BigQueryImpl.insertAlltransformsrequest.getRows()usingFluentIterable.from(request.getRows()).transform(...).toList()and a 1-elementboolean[]heap array. BecauseFluentIterable.transformwraps the list in a non-CollectionIterable,.toList()loses the known row count and repeatedly resizes its internalObject[]buffer (~14 array copies for a 10,000-row batch).InsertAllResponse.fromPbbuilds an intermediateHashMappopulated with lazyLists.transformviews overErrorProtowire objects and then copies the map intoImmutableMap.copyOf(insertErrors). Similarly,BigQueryImpl.queryRpc,queryRpcArrow, andqueryArrowpass lazyLists.transformerror views toBigQueryException. These lazy views re-executeBigQueryError.fromPbon every traversal and retain the underlyingErrorProto(GenericJson) wire objects in memory.Changes
java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/InsertAllRequest.java, replaceLists.newLinkedListwithLists.newArrayListinBuilder.setRows.java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java, replace theFluentIterable.from(request.getRows()).transform(...).toList()pipeline andboolean[1]array ininsertAllwith a pre-sizednew ArrayList<>(rows.size())loop and a primitiveboolean allInsertIdsSet.java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/InsertAllResponse.javaandjava-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java, buildImmutableMap.builderWithExpectedSizedirectly inInsertAllResponse.fromPband eagerly materializeImmutableList<BigQueryError>instances usingImmutableList.builderWithExpectedSize.java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/InsertAllResponseTest.java, assert thatInsertAllResponse.fromPbreturns materializedBigQueryErrorinstances across repeated accesses.Expected Impact
LinkedList$Nodeallocations (24 bytes per row) when staging batch rows inInsertAllRequest.Builder.setRows.FluentIterable,Function,boolean[1], and ~14 intermediateObject[]resize copies per 10,000-rowinsertAllbatch.HashMapallocation and re-hashing inInsertAllResponse.fromPb, prevents repeatedBigQueryErrorallocations on inspection, and allowsErrorProtowire DTOs to be garbage-collected immediately.Verification
InsertAllRequestTest,InsertAllResponseTest, andBigQueryImplTest:mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=InsertAllRequestTest,InsertAllResponseTest,BigQueryImplTest -Dclirr.skip=true -Denforcer.skip=true -Dflatten.skip=true -Dcheckstyle.skip=true -Dfmt.skip=true