Repository navigation
Conversation
|
/gemini review |
There was a problem hiding this comment.
📋 Review Summary
This PR optimizes hot paths in GroupBy, _GroupAndAggregate, and Partition.ApplyPartitionFnFn within sdks/python/apache_beam/transforms/core.py. Hoisting self._key_func() out of the per-element Map lambda, constructing dynamic namedtuples via tuple.__new__, and avoiding per-element module imports, ABC isinstance checks, and generator frames in ApplyPartitionFnFn.process are clean, well-targeted performance improvements that preserve existing semantics and serialization behavior.
🔍 General Feedback
- All changes maintain picklability for distributed execution (resolving
_dynamic_named_tupleat runtime while caching the type lookup byfield_names). - Transform labels remain pinned to their 2.68 compatibility strings, preserving pipeline update compatibility.
- Validation and error messages in
Partition.ApplyPartitionFnFn.processremain identical forbool, non-intnumbers.Integraltypes, and out-of-bounds indices.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
/gemini review |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #41272 +/- ##
============================================
+ Coverage 57.48% 57.75% +0.26%
Complexity 2492 2492
============================================
Files 1204 1200 -4
Lines 171265 171736 +471
Branches 2103 2103
============================================
+ Hits 98456 99178 +722
+ Misses 70157 69906 -251
Partials 2652 2652
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
/gemini review |
There was a problem hiding this comment.
📋 Review Summary
This PR introduces targeted performance optimizations in GroupBy.expand and Partition.ApplyPartitionFnFn.process within sdks/python/apache_beam/transforms/core.py. Hoisting self._key_func() out of the per-element Map lambda and adding a fast path for type(partition) is not int (along with hoisting import numbers to module level) cleanly eliminate per-element overhead while preserving existing behavior, error handling, and transform label compatibility.
🔍 General Feedback
- Hoisting
key_func = self._key_func()inGroupBy.expandavoids reconstructing the key extractor closure and avoids capturingselfin theMapclosure, matching the existing pattern in_GroupAndAggregate.expand. - Fast-pathing exact
inttypes viatype(partition) is not intinPartition.ApplyPartitionFnFn.processsafely bypasses ABCisinstance(..., numbers.Integral)checks (andboolchecks, sincetype(True) is bool) on the common path while preserving identical validation and conversion forbooland customnumbers.Integralsubclasses.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
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:
Full details of the methodology of generation and review can be found here - Apache Beam Python SDK Performance PRs Methodology & Branches Impact Ranking
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.expandandPartition.ApplyPartitionFnFn.processinsdks/python/apache_beam/transforms/core.py:key_func = self._key_func()outsideMap(lambda x: (key_func(x), x))inGroupBy.expand:Map(lambda x: (self._key_func()(x), x))re-invokedself._key_func()(rebuilding the key extractor closure/list) on every single input element instead of once at graph expansion time.import numbersto module top-level and fast-pathtype(partition) is not intinPartition.ApplyPartitionFnFn.process:import numberslock/module lookup andisinstance(partition, numbers.Integral)ABCInstanceCheckfor standardintpartition indices.Dataflow benchmarks showing improvement:
2026-11-12_12_17_54-34243551289663400222026-11-12_12_17_54-10844522332670306763n1-standard-8,--autoscaling_algorithm=NONE --num_workers=1 --max_num_workers=1) Cloud Dataflow pipeline running40,000,000elements ·Partition(8)→Flatten→GroupBy(bucket=x & 63), built-in transform stage execution time improved from953,244 ms->766,740 ms(1.24xfaster,-19.6%) (total single-worker job vCPU time:2406s->1964s).Additional benchmarks
Strict Single-PR Isolated Microbenchmark (
upstream/mastervsupstream/master+ this PR alone):bench_38_groupby_and_partition_fastpath(c2feb961eef) (on branchperf/py-benchmarks-suite):Partition.ApplyPartitionFnFn.processcalls (bench_38): Improved from624.03 ms->464.85 ms(1.34xfaster,-25.5%,-159.18 ms) in strict isolation.GroupBymulti-field key extractions: Improved from525.56 ms->459.70 ms(1.14xfaster,-12.5%,-65.86 ms) when calling_key_func()once, and 10.67x faster than callingself._key_func()(x)per element insideMap.On-Worker Cloud Dataflow
n2-standard-4Benchmark (p28_ms:19_partition_fastpath_300k): On a Cloud Dataflown2-standard-4worker, 300,000Partition.ApplyPartitionFnFn.processcalls improved from965 ms->649 ms(1.49xfaster,-32.7%).End-to-End Cloud Dataflow Benchmark (
io_coders_transforms&s4-avro-tfrec): Included inrun_io_coders_transforms_pipeline(c2feb961eef) (1.48x faster:49.2s->33.3s) andsuite4_avro_tfrecord_mltransform(1.23x faster:108.8s->88.7s).Per-Piece Ablation Breakdown (
592.01 ms->314.81 ms,1.88xfaster,-277.21 msacross 150kGroupBykey extractions +Partition.ApplyPartitionFnFn.processcalls):key_func = self._key_func()outsideMaplambda inGroupBy.expand55.2%(-153.02 ms)55.2%(-153.02 ms)import numbers+type(partition) is not intfast path inPartition.ApplyPartitionFnFn.process44.8%(-124.19 ms)44.8%(-124.19 ms)Testing
pytest sdks/python/apache_beam/transforms/ptransform_test.py.