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

   ### Description
   
   Optimizes four worker harness hot paths across `operations.py`, 
`operations.pxd`, `sdk_worker.py`, `statesampler_fast.pyx`, and `opcounters.py`:
   
   1. **`DoOperation` (`operations.py` / `operations.pxd`):** Unpickles 
`self.spec.serialized_fn` once in `__init__` (caching `self._fn_data` and 
`self.fn`) instead of unpickling it up to three separate times across 
`__init__`, `setup()`, and `_get_runtime_performance_hints()`.
   2. **`ShortIdCache.get_short_id` (`sdk_worker.py`):** Adds a lock-free 
`self._info_key_to_short_id.get(key)` fast path before acquiring `self._lock`.
   3. **`ScopedState` (`statesampler_fast.pyx` / `operations.pxd`):** Types 
`scoped_*_state` fields as `statesampler_fast.ScopedState` in `operations.pxd` 
and removes redundant `PyThread_acquire_lock` / `PyThread_release_lock` calls 
around C integer assignments in `ScopedState.__enter__` / `__exit__`.
   4. **`OperationCounters` (`opcounters.py`):** Checks `if 
self._sample_counter < self._next_sample: return False` first in 
`_should_sample()` and resets `self._next_sample = 0` in `restart_sampling()`.
   
   ### Benchmarks & Performance Impact
   
   - **Microbenchmark:** [`bench_13_worker_do_op_and_statesampler` 
(`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/microbenchmarks.py#L237-L248)
 (on branch 
[`perf/py-benchmarks-suite`](https://github.com/damccorm/beam/tree/perf/py-benchmarks-suite))
 — `ShortIdCache.get_short_id` improved by **11.09x (`0.091s` -> `0.0082s`)**, 
`DoOperation` `serialized_fn` unpickling improved by **4.71x**, `ScopedState` 
transitions improved by **1.6x–2.3x**, and `OperationCounters` sampling 
improved by **1.54x**.
   - **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/runners/worker/sdk_worker_test.py 
sdks/python/apache_beam/runners/worker/statesampler_test.py 
sdks/python/apache_beam/runners/worker/opcounters_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