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

   Note: this PR was auto-generated by tooling. Before moving it out of draft, 
the author has done a full review of the PR, specifically evaluating it on the 
following criteria:
   
   - The PR is an appropriate fix for the issue (e.g. we're not changing a test 
to avoid a bug)
   - The PR is a simple fix to the issue (e.g. we're not adding infra when we 
could just modify a test)
   - The PR is not covering for other problems (e.g. if we're bumping timeouts, 
it is not because we're covering for a performance problem)
   
   Full details of the methodology of generation and review can be found here - 
[Apache Beam Python SDK Performance PRs Methodology & Branches Impact 
Ranking](https://docs.google.com/document/d/1Edrmzp-qFHYs2j7dg9uFyV_jEMgq4QKt_JQDuuk-lmI/edit?usp=drivesdk)
   
   If you have concerns with the PR or with the underlying methodology used to 
generate this, please comment below - they will be addressed by the author not 
the tooling :)
   
   **Summary of changes:**
   
   Optimizes Cython `ByteCountingOutputStream` (`stream.pyx`, `stream.pxd`) and 
`slow_stream.get_varint_size` (`slow_stream.py`):
   
   1. **Skip `malloc(1024)` / `free()` in `OutputStream.__cinit__` when 
`type(self) is ByteCountingOutputStream` and add `cpdef` methods for 
`write_bigendian_double`, `write_bigendian_float`, and `size`:**
      - Previously, instantiating Cython `ByteCountingOutputStream()` (called 
on every `CoderImpl.estimate_size` fallback) inherited `OutputStream.__cinit__` 
and allocated an unused 1 KB C heap buffer via `malloc(1024)`.
   2. **Fast-path `0 <= v <= 0x7F` and replace the `while` shift loop in 
`slow_stream.get_varint_size` with `(v.bit_length() + 6) // 7`.**
   
   **Dataflow benchmarks showing improvement:**
   
   - Before the change: 
[`2026-11-12_12_17_54-13438616851834391456`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_12_17_54-13438616851834391456?project=apache-beam-testing)
   - After the change: 
[`2026-11-12_12_17_56-8327665822289286431`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_12_17_56-8327665822289286431?project=apache-beam-testing)
   - Summary of perf improvement: In a single-worker (`n1-standard-8`, 
`--autoscaling_algorithm=NONE --num_workers=1 --max_num_workers=1`) Cloud 
Dataflow pipeline running `40,000,000` schema `PerfSchemaRow` KV elements ยท 
`CombinePerKey(CountCombineFn)` (`ByteCountingOutputStream`), built-in 
transform stage execution time improved from **`563,227 ms` -> `521,894 ms` 
(`1.08x` faster, `-7.3%`)** (total single-worker job vCPU time: `1723s` -> 
`1817s`).
   
   **Additional benchmarks**
   
   - **Strict Single-PR Isolated Microbenchmark (`upstream/master` vs 
`upstream/master` + this PR alone):** [`bench_26_cython_byte_counting_stream` 
(`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/microbenchmarks.py#L402-L410)
 (on branch 
[`perf/py-benchmarks-suite`](https://github.com/damccorm/beam/tree/perf/py-benchmarks-suite))
 โ€” 500,000 `ByteCountingOutputStream` + `get_varint_size` calls improved from 
**`120.45 ms` -> `93.35 ms` (`1.29x` faster, `-22.5%`, `-27.10 ms`)** in 
compiled Cython.
   - **Per-Change Attribution (Most to Least Impact, 500,000 calls each in 
strict isolation):**
     1. *Most CPU impact (`85.9%` of savings, `-22.48 ms`):* `get_varint_size` 
using `0 <= v <= 0x7F` and `(v.bit_length() + 6) // 7` โ€” **`80.90 ms` -> `58.42 
ms` (`1.38x` faster, `-27.8%`)**.
     2. *Second (`14.1%` of CPU savings, `-3.68 ms`, plus eliminating 500,000 1 
KB `malloc`/`free` pairs):* Skipping `malloc(1024)` in 
`ByteCountingOutputStream.__cinit__` and `cpdef` float/double methods โ€” 
**`47.35 ms` -> `43.67 ms` (`1.08x` faster, `-7.8%`)**.
   - **On-Worker Cloud Dataflow `n2-standard-4` Benchmark 
(`p28_ms:08_cython_byte_counting_stream_500k`):** On a Cloud Dataflow 
`n2-standard-4` worker, 500,000 `ByteCountingOutputStream` + `get_varint_size` 
calls improved from **`204 ms` -> `146 ms` (`1.40x` faster, `-28.4%`)**.
   
   ### Testing
   - Verified with `pytest sdks/python/apache_beam/coders/stream_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