Skip to content

perf(bigquery): [T2-4] avoid defensive HashMap copy for ImmutableMap in InsertAllRequest.RowToInsert - #26

Open
lqiu96 wants to merge 1 commit into
mainfrom
perf/bq-t2-04-rowtoinsert-zero-copy-map
Open

lqiu96 wants to merge 1 commit into
mainfrom
perf/bq-t2-04-rowtoinsert-zero-copy-map

Conversation

@lqiu96

@lqiu96 lqiu96 commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Summary & Bottleneck

  • In java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/InsertAllRequest.java (RowToInsert constructor), every non-ImmutableMap row passed to RowToInsert.of(...) or Builder.addRow(...) is defensively copied into new HashMap<>(content) and wrapped in Collections.unmodifiableMap(...), even when content is empty or when the caller has just constructed a single-use per-row Map<String, Object> solely for streaming ingestion.
  • For an insertAll batch of $R$ rows and $C$ columns, copying a single-use HashMap into a second HashMap per row allocates $R$ extra HashMap instances, $R$ extra entry table arrays, and $R \times C$ duplicate HashMap$Node objects.

Changes

  • Updated InsertAllRequest.RowToInsert (java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/InsertAllRequest.java) to return ImmutableMap.of() when content.isEmpty() instead of allocating a new HashMap and UnmodifiableMap wrapper.
  • Added RowToInsert.ofWithoutCopy(String id, Map<String, ?> content), RowToInsert.ofWithoutCopy(Map<String, ?> content), Builder.addRowWithoutCopy(String id, Map<String, ?> content), and Builder.addRowWithoutCopy(Map<String, ?> content) so callers constructing single-use row maps can wrap them directly in Collections.unmodifiableMap(content) (or ImmutableMap.copyOf(content) when already an ImmutableMap) without a defensive HashMap copy.
  • Added unit tests in java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/InsertAllRequestTest.java covering RowToInsert.ofWithoutCopy and Builder.addRowWithoutCopy.

Expected Impact

  • Eliminates $R$ HashMap instances, $R$ entry table arrays, and $R \times C$ HashMap$Node allocations per insertAll batch when callers use ofWithoutCopy or addRowWithoutCopy, and eliminates map/wrapper allocations for empty row maps.

Verification

  • Verified with mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=InsertAllRequestTest,BigQueryImplTest (157 tests, 0 failures).

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.

1 participant