sjvanrossum opened a new pull request, #40791:
URL: https://github.com/apache/beam/pull/40791

   Optimizes `Row` construction, field verification, `RowWithGetters` field 
caching, and `Row` utility methods by eliminating intermediate object 
allocations (`RowPosition`, `FieldAccessDescriptor`, `RowFieldMatcher`, 
`CapturingRowCases`, `TreeMap`, and `Stream` pipelines) on hot paths.
   
   Previously, `Row.Builder.build()` wrapped its positional `List<Object>` into 
a `FieldOverrides` tree (allocating a `FieldAccessNode`, a 
`FieldAccessDescriptor` per field, and a `RowPosition` per field/nested 
element) and traversed the schema through `RowFieldMatcher` + 
`CapturingRowCases`. Similarly, `SchemaVerification.verifyFieldValue` 
constructed a single-field `Schema`, `FieldOverrides`, `CapturingRowCases`, 
`RowFieldMatcher`, and `RowPosition` on every single field verification call. 
Replaced `RowFieldMatcher` and `CapturingRowCases` with direct static 
`RowUtils.captureRow(Schema, List<Object>)` and 
`RowUtils.captureValue(FieldType, Object, String)` methods, and simplified 
`FieldOverrides` (`Row.FieldValueBuilder`) to apply overrides directly via 
`captureValue`.
   
   Replaced the `TreeMap<Integer, Object>` (and per-lookup boxed `Integer` key 
+ anonymous `Function` allocation in `computeIfAbsent`) in `RowWithGetters` 
with a flat `Object[]` indexed by `fieldIdx` (using a `NULL_VALUE` sentinel to 
cache `null` results without re-invoking getters). Extended 
`RowWithGetters.cacheFieldType` to also cache `ROW` and `LOGICAL_TYPE` fields 
in addition to `MAP`, `ARRAY`, and `ITERABLE`, avoiding repeated nested 
`RowWithGetters` / logical type conversions when a field is accessed multiple 
times.
   
   Pre-sized `Row.Builder`'s internal `ArrayList` to `schema.getFieldCount()` 
and used `Collections.addAll` in `addValues(Object...)`. Replaced 
`IntStream`/`Stream` pipelines and name-based lookups with indexed loops in 
`Row.getBaseValues()`, `Row.sorted()`, `Row.toSnakeCase()`, 
`Row.toCamelCase()`, `Row.nullRow()`, and `SchemaUtils.toPrettyRowString()`.
   
   Added JMH benchmarks covering flat and complex `Row` construction, `Row` 
`equals`/`hashCode`, repeated `RowWithGetters` field reads, and `RowCoder` / 
`SchemaCoder` encoding and decoding.
   
   > [!NOTE]
   > This PR stacks on top of #40789 and is part of a [series of 
patches](https://github.com/apache/beam/compare/master...sjvanrossum:beam:row-coder-opt)
 driving the following improvements:
   > | Benchmark | Base (`ops/us`) | Patch series (`ops/us`) | vs base |
   > | :--- | ---: | ---: | ---: |
   > | `buildFlatRow` | `1.070 ± 0.011` | **`26.722 ± 0.174`** | **24.97x 
(+2,397.4%)** |
   > | `buildComplexRow` | `0.860 ± 0.012` | **`9.550 ± 0.195`** | **11.10x 
(+1,010.5%)** |
   > | `complexRowEqualsAndHashCode` | `1.481 ± 0.036` | **`12.391 ± 0.400`** | 
**8.37x (+736.7%)** |
   > | `readFlatRowBoxedGetters` | `148.151 ± 2.005` | **`501.611 ± 2.892`** | 
**3.39x (+238.6%)** |
   > | `readFlatRowPrimitiveGetters` | `148.151 ± 2.005` | **`503.336 ± 
3.551`** | **3.40x (+239.7%)** |
   > | `encodeFlatRow` | `7.987 ± 0.068` | **`19.758 ± 0.280`** | **2.47x 
(+147.4%)** |
   > | `decodeFlatRow` | `11.425 ± 0.351` | **`12.524 ± 0.068`** | **1.10x 
(+9.6%)** |
   > | `encodeComplexRow` | `8.217 ± 0.130` | **`12.388 ± 0.538`** | **1.51x 
(+50.8%)** |
   > | `decodeComplexRow` | `3.954 ± 0.095` | **`4.178 ± 0.047`** | **1.06x 
(+5.7%)** |
   > | `encodePojoViaSchemaCoder` | `4.932 ± 0.053` | **`7.412 ± 0.159`** | 
**1.50x (+50.3%)** |
   > | `decodePojoViaSchemaCoder` | `2.616 ± 0.030` | **`3.755 ± 0.019`** | 
**1.44x (+43.5%)** |
   > | `readRowWithGettersRepeatedly` | `27.326 ± 0.642` | **`121.171 ± 
1.810`** | **4.43x (+343.4%)** |
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to