damccorm opened a new pull request, #41279: URL: https://github.com/apache/beam/pull/41279
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 `Index.partition_fn` (`dataframe/partitionings.py`) and `DataFrameBatchConverter.explode_batch` / `SeriesBatchConverter` (`typehints/pandas_type_compatibility.py`): 1. **Fast-path non-null columns and `NamedTuple._make` in `DataFrameBatchConverter.explode_batch`:** - Uses `iter(series)` directly when `not nulls.any()` and constructs rows positionally via `map(user_type._make, zip(*iterators))` instead of building and unpacking a `{column: value}` dict per row. 2. **Single-pass `df.groupby(pids, sort=False)` in `Index.partition_fn`:** - Replaces the $O(P \cdot N)$ loop `for key in range(num_partitions): yield key, df[hashes % num_partitions == key]` (which constructed a boolean mask and scanned the full DataFrame $P$ times) with a single-pass `df.groupby(pids, sort=False)`. **Dataflow benchmarks showing improvement:** - Before the change: [`2026-11-12_12_18_07-14933235308757022869`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_12_18_07-14933235308757022869?project=apache-beam-testing) - After the change: [`2026-11-12_12_18_08-17650989079603368963`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_12_18_08-17650989079603368963?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 `30,000,000` schema `PerfSchemaRow` elements · `to_dataframe(pcoll)` → filter → `to_pcollection(df)`, built-in transform stage execution time improved from **`556,120 ms` -> `425,425 ms` (`1.31x` faster, `-23.5%`)** (total single-worker job vCPU time: `1578s` -> `1740s`). **Additional benchmarks** - **Strict Single-PR Isolated Microbenchmark (`upstream/master` vs `upstream/master` + this PR alone):** [`bench_50_dataframe_partition_and_explode` (`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/microbenchmarks.py#L684-L694) (on branch [`perf/py-benchmarks-suite`](https://github.com/damccorm/beam/tree/perf/py-benchmarks-suite)) — 100 iterations of `Index.partition_fn(df, 16)` + `DataFrameBatchConverter.explode_batch(df)` on a 5,000-row DataFrame improved from **`736.27 ms` -> `308.46 ms` (`2.39x` faster, `-58.1%`, `-427.81 ms`)** in strict isolation. - **Per-Change Attribution (Most to Least Impact, 100 iterations on 5,000-row DataFrame in strict isolation):** 1. *Most wall-time impact (`83.8%` of savings, `-322.90 ms`):* `DataFrameBatchConverter.explode_batch` using `iter(series)` on non-null columns and `map(user_type._make, ...)` — **`705.70 ms` -> `382.80 ms` (`1.84x` faster, `-45.8%`)**. 2. *Second (`16.2%` of savings, `-62.58 ms`, scales with partition count $P$):* Single-pass `df.groupby(pids, sort=False)` in `Index.partition_fn(df, 16)` — **`144.90 ms` -> `82.32 ms` (`1.76x` faster, `-43.2%`)**. - **On-Worker Cloud Dataflow `n2-standard-4` Benchmark (`p28_ms:28_dataframe_partition_explode_100x5k` & `suite3_parquet_dataframe`):** On a Cloud Dataflow `n2-standard-4` worker, 100 iterations of `partition_fn` + `explode_batch` on 5,000-row DataFrames improved from **`1,261 ms` -> `529 ms` (`2.38x` faster, `-58.0%`)**, and `stage_df_partition_wall_ms` in `suite3_parquet_dataframe` improved from **`2,678 ms` -> `2,210 ms` (`1.21x` faster)**. ### Testing - Verified with `pytest sdks/python/apache_beam/dataframe/partitionings_test.py sdks/python/apache_beam/typehints/pandas_type_compatibility_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]
