Skip to content

perf(bigquery): avoid String.toLowerCase() allocation in FieldList.getIndex - #15006

Open
lqiu96 wants to merge 1 commit into
googleapis:mainfrom
lqiu96:perf/bq-t1-02-fieldlist-getindex
Open

lqiu96 wants to merge 1 commit into
googleapis:mainfrom
lqiu96:perf/bq-t1-02-fieldlist-getindex

Conversation

@lqiu96

@lqiu96 lqiu96 commented Nov 13, 2026

Copy link
Copy Markdown
Member

Summary

Avoids per-cell String.toLowerCase() allocations when reading rows by column name (FieldValueList.get(String) -> FieldList.getIndex(String)).

Every row in a result set shares the same FieldList schema, so getIndex(name) runs once per cell lookup. Previously, FieldList used a CaseInsensitiveMap that called key.toString().toLowerCase() on every get(name) call:

  • For any column name containing uppercase letters (for example, "UserName"), .toLowerCase() allocated a new String on every cell read across every row.
  • Even for lowercase column names, .toLowerCase() scanned every character of the name on every lookup.

Changes

  • FieldList:
    • Pre-size a HashMap with Maps.newHashMapWithExpectedSize(size * 2) and populate both name.toLowerCase(Locale.ROOT) and exact-case name (when different) once when building the schema.
    • Preserve last-writer-wins behavior if two fields differ only by case (for example, "FOO" at index 0 followed by "foo" at index 1).
    • In getIndex(String), probe nameIndex.get(name) first so exact schema names and all-lowercase names hit with zero allocations, falling back to name.toLowerCase(Locale.ROOT) only on a miss.
  • FieldListTest:
    • Added testGetIndexExactLowerAndMixedCase and testGetIndexCaseCollisionPreservesLastWriterWins.

Verification

  • mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=FieldListTest (6 passed)
  • mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=SchemaTest (3 passed)
  • mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=FieldValueListTest (5 passed)
  • mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=FieldTest (12 passed)

@lqiu96
lqiu96 requested review from a team as code owners November 13, 2026 21:20

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 field index lookups in FieldList by pre-storing both exact-case and lowercase field names in a single map, avoiding string allocation and lowercasing on every lookup when exact or lowercase matches are found. It also ensures that the existing last-writer-wins behavior is preserved in case of case collisions. Unit tests have been added to verify these changes. I have no feedback to provide.

@lqiu96
lqiu96 force-pushed the perf/bq-t1-02-fieldlist-getindex branch from 872ca0b to 2277d13 Compare November 13, 2026 21:24

This branch has not been deployed

No deployments
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