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

   ## Summary
   
   Avoids allocating, serializing, and parsing `ElementMetadata` proto payloads 
on the wire when all metadata fields have their wire defaults 
(`CausedByDrain.NORMAL`, `ValueKind.INSERT`, no non-root OpenTelemetry 
`Context`), and decodes Windmill message metadata in a single pass.
   
   Per 
[`beam:coder:windowed_value:v2`](model/pipeline/src/main/proto/org/apache/beam/model/pipeline/v1/beam_runner_api.proto),
 `metadata_length` and `metadata` are only present when the `0x80` bit is set 
on the first byte of `PaneInfo`. Previously, enabling 
`element_metadata_supported` unconditionally set `0x80` and 
allocated/serialized/parsed an `ElementMetadata` proto + `byte[]` on **every** 
element, even when all fields were defaults.
   
   ### Key Changes
   - **`FullWindowedValueCoder` & `ValueInSingleWindow.Coder`** 
(`sdks/java/core`):
     - Added `@Internal 
FullWindowedValueCoder.hasNonDefaultElementMetadata(...)` and omit the 
`ElementMetadata` payload (clearing the `0x80` bit in `PaneInfo`) when all 
fields are wire defaults.
     - When non-default metadata *is* present, omit default proto fields 
(`drain` when `NORMAL`, `value_kind` when `INSERT`) to shrink wire payloads.
     - On `decode`, check plain `paneInfo.isElementMetadata()` before volatile 
`isMetadataSupported()`, and skip `OpenTelemetryContextPropagator.read(...)` 
when `traceparent` and `tracestate` are empty.
   - **`PaneInfo` / `PaneInfoCoder`** (`sdks/java/core`):
     - Short-circuit `PaneInfo.withElementMetadata(boolean)` to return `this` 
when the flag is unchanged (eliminates `new PaneInfo(...)` allocation for 
interned `FIRST` panes on encode/decode).
     - Removed duplicate `chooseEncoding(value)` call in `PaneInfoCoder.encode`.
   - **`WindmillSink`, `UngroupedWindmillReader`, & `WindmillKeyedWorkItem`** 
(`runners/google-cloud-dataflow-java/worker`):
     - Avoid allocating `ElementMetadata.Builder` in `WindmillSink.add` when 
metadata is unsupported or all-default; omit default metadata bytes in 
`WindmillSink.encodeMetadata` and write non-default protos directly via 
`metadata.writeTo(stream)`.
     - Decode Windmill message metadata (`PaneInfo`, windows, optional 
`ElementMetadata`) in a single `InputStream` pass instead of 3 separate passes.
     - Parse trailing Windmill `ElementMetadata` via zero-copy 
`ByteString.substring(...)` instead of `parseFrom(InputStream)` (which 
allocates a 4 KB `CodedInputStream` buffer per call), and return 
`ElementMetadata.getDefaultInstance()` when unset.
   - **Tests:**
     - Added wire-elision, round-trip, and `PaneInfo` reuse unit tests in 
`WindowedValueTest`, `PaneInfoTest`, and `UngroupedWindmillReaderTest`.
   
   ### Wire Compatibility
   - **Old producer → New consumer:** Existing wire payloads with `0x80` set 
continue to decode cleanly in all updated decoders.
   - **New producer → Old consumer:** Default elements encode identically to 
`isMetadataSupported() == false` (`0x80` cleared), which existing decoders 
already handle by defaulting to `(NORMAL, null/root, INSERT)`. Non-default 
elements omit default enum fields (`DRAIN_MODE_UNSPECIFIED`, 
`VALUE_KIND_UNSPECIFIED`), which existing decoders already map to `NORMAL` and 
`INSERT`.
   
   ## Benchmark Results (JDK 21, 2 forks, `-prof gc`, `setMetadataSupported()` 
ON)
   
   ### Wire Size (Default Element)
   | Coder | Master | Branch | Delta |
   |---|---:|---:|---:|
   | `FullWindowedValueCoder` | 48 B | **43 B** | **-5 B (-10.4%)** |
   | `ValueInSingleWindow.Coder` | 44 B | **39 B** | **-5 B (-11.4%)** |
   | `WindmillSink` metadata | 20 B | **16 B** | **-4 B (-20.0%)** |
   
   ### Latency & Allocations (lower is better)
   | Benchmark | Master (ns/op) | Branch (ns/op) | Speedup | Alloc (B/op) |
   |---|---:|---:|---:|---:|
   | `windmillKeyedWorkItem_decode_default` | 227.9 ± 38.6 | **132.3 ± 24.1** | 
**1.72×** | 1,212 → **800** (-34%) |
   | `fullWindowedValueCoder_decode_default` | 90.8 ± 26.6 | **57.5 ± 7.6** | 
**1.58×** | 552 → **328** (-41%) |
   | `valueInSingleWindowCoder_encode_default` | 28.4 ± 4.9 | **18.3 ± 2.6** | 
**1.55×** | 232 → **88** (-62%) |
   | `valueInSingleWindowCoder_decode_default` | 75.1 ± 6.1 | **49.7 ± 4.4** | 
**1.51×** | 504 → **280** (-44%) |
   | `fullWindowedValueCoder_encode_default` | 26.6 ± 4.6 | **17.9 ± 2.4** | 
**1.49×** | 208 → **88** (-58%) |
   | `windmillSink_encodeMetadata_default`* | 35.6 ± 4.0 | **25.9 ± 2.3** | 
**1.37×** | 376 → **272** (-28%) |
   | `windmillKeyedWorkItem_decode_drain` (non-default) | 219.7 ± 54.5 | 
**174.3 ± 18.4** | **1.26×** | 1,212 → **1,032** (-15%) |
   | `fullWindowedValueCoder_encode_traced` (non-default) | 94.4 ± 16.4 | 
**76.4 ± 9.7** | **1.24×** | 384 → **336** (-13%) |
   
   *\*`WindmillSink.add()` also skips allocating `ElementMetadata.Builder` + 
`build()` altogether before calling `encodeMetadata`.*
   
   ## Verification
   - `:sdks:java:core`: `spotlessCheck`, `checkstyleMain`/`Test`, 
`spotbugsMain`, 18/18 tests (`WindowedValueTest`, `PaneInfoTest`, 
`MetadataPropagationTest`, `ValueKindTest`).
   - `:runners:google-cloud-dataflow-java:worker`: `spotlessCheck`, 
`checkstyleMain`/`Test`, 26/26 tests (`UngroupedWindmillReaderTest`, 
`WindmillKeyedWorkItemTest`, `WindmillSinkTest`, 
`StreamingGroupAlsoByWindowFnsTest`, 
`StreamingGroupAlsoByWindowsReshuffleDoFnTest`, `WindowingWindmillReaderTest`).
   
   


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