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

   ### Description
   
   Optimizes `FastPrimitivesCoderImpl.encode_to_stream` in 
`sdks/python/apache_beam/coders/coder_impl.py` and 
`sdks/python/apache_beam/coders/coder_impl.pxd`:
   
   1. **Type `list_value=list` and `tuple_value=tuple` in `@cython.locals` and 
split `elif t is list:` / `elif t is tuple:` branches:**
      - Allows Cython to compile `len(...)` and `for e in ...:` into direct 
`PyList_GET_SIZE` / `PyList_GET_ITEM` and `PyTuple_GET_SIZE` / 
`PyTuple_GET_ITEM` C loops instead of generic `PyObject_Size` and 
`PyObject_GetIter` / `PyIter_Next` iterator allocation.
   2. **Check `t is dict` and `t is set` (pointer identity) before `t in 
_ITERABLE_LIKE_TYPES` (`PySet_Contains`):**
      - Avoids hashing `t` and probing `_ITERABLE_LIKE_TYPES` on every `dict` 
or `set` encoded.
   3. **Short-circuit `if not is_compiled and not TYPE_CHECKING:` and inline 
64-bit integer bounds check:**
      - Placing `not is_compiled` first lets Cython (`is_compiled = True`, a 
`cdef bint`) eliminate the branch at C compile time without looking up 
`TYPE_CHECKING` in module globals on every `int`, and replaces 
`fits_in_64_bits(value)` with `-0x8000000000000000 <= value <= 
0x7fffffffffffffff` on the pure-Python path.
   4. **Declare `ENUM_TYPE`, `NESTED_STATE_TYPE`, and `DATACLASS_KW_ONLY_TYPE` 
as `cdef unsigned char` in `coder_impl.pxd`:**
      - Avoids module global lookups and `PyLong` unboxing for those type tags 
in Cython.
   
   ### Benchmarks & Performance Impact
   
   - **Microbenchmark Rerun:** [`bench_15_fast_primitives_coder_impl` 
(`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/microbenchmarks.py#L262-L270)
 (on branch 
[`perf/py-benchmarks-suite`](https://github.com/damccorm/beam/tree/perf/py-benchmarks-suite))
 — 300,000 `FastPrimitivesCoderImpl` encode + decode round-trips on `([1, 2, 3, 
4, 5], ('a', 'b', 42), 9876543210)` improved from **`297.33 ms` -> `258.03 ms` 
(`1.15x` faster, `-13.2%`)** in compiled Cython.
   - **Per-Change Attribution (Most to Least Impact, in compiled Cython):**
     1. *Most impact (`-40.25 ms` across 300,000 calls):* Splitting `elif t is 
list:` (`list_value=list`) and `elif t is tuple:` (`tuple_value=tuple`) for 
C-level `PyList_GET_ITEM` / `PyTuple_GET_ITEM` iteration on `([1, 2, 3, 4, 5], 
(10, 20, 30, 40))` — **`139.84 ms` -> `99.59 ms` (`1.40x` faster, `-28.8%`)**.
     2. *Second (`-20.46 ms` across 300,000 calls):* Checking `elif t is dict:` 
and `elif t is set:` before `elif t in _ITERABLE_LIKE_TYPES:` 
(`PySet_Contains`) on `{1: 2, 3: 4, 5: 6}` — **`99.51 ms` -> `79.05 ms` 
(`1.26x` faster, `-20.6%`)**.
     3. *Least impact (`-4.11 ms` across 500,000 calls):* Short-circuiting `if 
not is_compiled and not TYPE_CHECKING:` on `int` encoding (`9876543210`) — 
**`44.18 ms` -> `40.07 ms` (`1.10x` faster, `-9.3%`)**.
   - **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