Skip to content

perf(bigquery): [T1-7] reuse flyweight boolean FieldValue singletons in FieldValue.fromPb - #13

Open
lqiu96 wants to merge 1 commit into
mainfrom
perf/bq-t1-07-fieldvalue-boolean-flyweights
Open

lqiu96 wants to merge 1 commit into
mainfrom
perf/bq-t1-07-fieldvalue-boolean-flyweights

Conversation

@lqiu96

@lqiu96 lqiu96 commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Summary & Bottleneck

When decoding BigQuery table rows and query results, every boolean cell ("true" or "false") previously allocated a new 24-byte FieldValue wrapper in FieldValue.fromPb / FieldValue.of ($O(\text{rows} \times \text{boolean_cols})$ allocations), even though FieldValue is deeply immutable and stores the canonical "true" or "false" string value.

  • In addition, FieldValue.getBooleanValue() executed stringValue.equalsIgnoreCase("true") twice on every true cell (once in checkState and a second time inside Boolean.parseBoolean(stringValue)).

Changes

  • In java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/FieldValue.java:
    • Add reusable immutable flyweight singletons for primitive "true" and "false" cells (TRUE_PRIMITIVE, FALSE_PRIMITIVE, TRUE_PRIMITIVE_INT64_TIMESTAMPS, and FALSE_PRIMITIVE_INT64_TIMESTAMPS).
    • Return the flyweight singletons in FieldValue.of(Attribute, Object, Boolean) (invoked by FieldValue.fromPb) and in readResolve() when attribute == Attribute.PRIMITIVE, useInt64Timestamps != null, and value is "true" or "false".
    • Simplify getBooleanValue() to check "true".equalsIgnoreCase(stringValue) and "false".equalsIgnoreCase(stringValue) once before throwing IllegalStateException.
  • In java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/FieldValueTest.java:
    • Add unit test testBooleanFlyweightsAndGetBooleanValue verifying singleton reuse across FieldValue.of and FieldValue.fromPb as well as case-insensitive and invalid boolean parsing.

Expected Impact

  • Eliminates 100% of FieldValue wrapper allocations on boolean columns across query results and table data reads.
  • Eliminates redundant case-insensitive string comparisons in FieldValue.getBooleanValue().

Verification

  • Ran unit tests in FieldValueTest and FieldValueListTest:
    mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=FieldValueTest,FieldValueListTest

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