damccorm opened a new pull request, #41272: URL: https://github.com/apache/beam/pull/41272
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 `GroupBy` / `_GroupAndAggregate` and `Partition.ApplyPartitionFnFn` in `sdks/python/apache_beam/transforms/core.py`: 1. **Hoist `import numbers`, fast-path `type(partition) is not int`, and return `[pvalue.TaggedOutput(...)]` instead of a generator in `Partition.ApplyPartitionFnFn.process`:** - Previously, `Partition.ApplyPartitionFnFn.process` executed `import numbers` and `isinstance(partition, numbers.Integral)` (an ABC `InstanceCheck`) and allocated a generator frame on every element. 2. **Hoist `key_func = self._key_func()` outside `Map(lambda x: (key_func(x), x))` in `GroupBy.expand` and use `tuple.__new__` in `GroupBy._key_func` and `_GroupAndAggregate.expand`.** **Dataflow benchmarks showing improvement:** - Before the change: [`2026-11-12_12_17_54-3424355128966340022`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_12_17_54-3424355128966340022?project=apache-beam-testing) - After the change: [`2026-11-12_12_17_54-10844522332670306763`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_12_17_54-10844522332670306763?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` elements · `Partition(8)` → `Flatten` → `GroupBy(bucket=x & 63)`, built-in transform stage execution time improved from **`953,244 ms` -> `766,740 ms` (`1.24x` faster, `-19.6%`)** (total single-worker job vCPU time: `2406s` -> `1964s`). **Additional benchmarks** - **Strict Single-PR Isolated Microbenchmark (`upstream/master` vs `upstream/master` + this PR alone):** [`bench_38_groupby_and_partition_fastpath` (`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/microbenchmarks.py#L533-L540) (on branch [`perf/py-benchmarks-suite`](https://github.com/damccorm/beam/tree/perf/py-benchmarks-suite)): - **300,000 `Partition.ApplyPartitionFnFn.process` calls (`bench_38`):** Improved from **`624.03 ms` -> `464.85 ms` (`1.34x` faster, `-25.5%`, `-159.18 ms`)** in strict isolation. - **300,000 `GroupBy` multi-field key extractions:** Improved from **`525.56 ms` -> `459.70 ms` (`1.14x` faster, `-12.5%`, `-65.86 ms`)** when calling `_key_func()` once, and **10.67x faster** than calling `self._key_func()(x)` per element inside `Map`. - **On-Worker Cloud Dataflow `n2-standard-4` Benchmark (`p28_ms:19_partition_fastpath_300k`):** On a Cloud Dataflow `n2-standard-4` worker, 300,000 `Partition.ApplyPartitionFnFn.process` calls improved from **`965 ms` -> `649 ms` (`1.49x` faster, `-32.7%`)**. - **End-to-End Cloud Dataflow Benchmark (`io_coders_transforms` & `s4-avro-tfrec`):** Included in [`run_io_coders_transforms_pipeline` (`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/dataflow_e2e_benchmarks.py#L305-L335) (**1.48x faster: `49.2s` -> `33.3s`**) and `suite4_avro_tfrecord_mltransform` (**1.23x faster: `108.8s` -> `88.7s`**). ### Testing - Verified with `pytest sdks/python/apache_beam/transforms/ptransform_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]
