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

   ### Description
   
   Optimizes `AbstractComponentCoderImpl.estimate_size`, `TupleCoderImpl`, and 
`SequenceCoderImpl` in `sdks/python/apache_beam/coders/coder_impl.py` and 
`sdks/python/apache_beam/coders/coder_impl.pxd`:
   
   1. **`AbstractComponentCoderImpl.get_estimated_size_and_observables`:** 
Types loop variables (`i=int, last=int, num_coders=int`) in `coder_impl.pxd`, 
guards `if child_observables: observables += child_observables` to avoid 
extending an empty list on every component, and forwards `nested=nested` from 
`estimate_size`.
   2. **`TupleCoderImpl._extract_components` and 
`_construct_from_components`:** Avoids copying `tuple(value)` / 
`tuple(components)` when the input is already a `tuple` (or `list` for 
extraction).
   3. **`SequenceCoderImpl.encode_to_stream`:** Short-circuits `type(value) is 
tuple or type(value) is list` before calling `hasattr(value, "__len__")`.
   
   ### Benchmarks & Performance Impact
   
   - **Isolated Microbenchmark Rerun (`upstream/master` vs `upstream/master` + 
this PR alone):** [`bench_18_tuple_and_sequence_coder_impl` 
(`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/microbenchmarks.py#L299-L311)
 (on branch 
[`perf/py-benchmarks-suite`](https://github.com/damccorm/beam/tree/perf/py-benchmarks-suite))
 — 300,000 `SequenceCoderImpl.encode` and `TupleCoderImpl.estimate_size` calls 
improved from **`209.90 ms` -> `148.70 ms` (`1.41x` faster, `-29.2%`, `-61.20 
ms`)** in compiled Cython.
   - **Per-Change Attribution (Most to Least Impact, 300,000 calls each in 
compiled Cython):**
     1. *Most impact (`79.4%` of savings, `-41.63 ms`):* Optimizing 
`AbstractComponentCoderImpl.get_estimated_size_and_observables` (`if 
child_observables:` guard, typed loop locals, and 
`TupleCoderImpl._extract_components` tuple pass-through) — **`126.00 ms` -> 
`84.37 ms` (`1.49x` faster, `-33.0%`)**.
     2. *Least impact (`20.6%` of savings, `-10.81 ms`):* Short-circuiting 
`type(value) is tuple or type(value) is list` before `hasattr(value, 
"__len__")` in `SequenceCoderImpl.encode_to_stream` — **`63.62 ms` -> `52.81 
ms` (`1.20x` faster, `-17.0%`)**.
   - **End-to-End Cloud Dataflow Benchmark (`core_window_row_combine`):** 
Included in [`run_core_window_row_combine_pipeline` 
(`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/dataflow_e2e_benchmarks.py#L274-L302)
 (3,000,000 generated events -> 9,000,000 sliding-windowed elements on a single 
`n2-standard-4` worker), which improved end-to-end worker stage execution time 
by **1.80x (`-44.5%`, `599.6s` -> `332.9s`)** and reduced total billable 
Dataflow vCPU/Memory/PD usage by **`-26.1%` (`1,134` -> `838` vCPU-s)**.
   
   ### Testing
   - Verified with `pytest sdks/python/apache_beam/coders/coders_test.py 
sdks/python/apache_beam/coders/fast_coders_test.py`.


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