damccorm opened a new pull request, #41270: URL: https://github.com/apache/beam/pull/41270
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 `_TFRecordUtil` in `sdks/python/apache_beam/io/tfrecordio.py`: 1. **Precompile `_LEN_STRUCT = struct.Struct("<Q")`, `_CRC_STRUCT = struct.Struct("<I")`, and `_HEADER_STRUCT = struct.Struct("<QI")` at class level:** - Eliminates per-record dynamic format string construction `struct.unpack("<%dsI" % length, buf)` (which bypassed Python's internal `struct` cache whenever record lengths varied and copied the payload) by slicing `data = buf[:-4]` and calling `cls._CRC_STRUCT.unpack_from(buf, length)`. **Dataflow benchmarks showing improvement:** - Before the change: [`2026-11-12_12_17_55-3626311894436938139`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_12_17_55-3626311894436938139?project=apache-beam-testing) - After the change: [`2026-11-12_12_17_54-6926661661971716491`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_12_17_54-6926661661971716491?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` `bytes` records · `WriteToTFRecord` → `ReadAllFromTFRecord`, built-in transform stage execution time improved from **`875,004 ms` -> `804,166 ms` (`1.09x` faster, `-8.1%`)** (total single-worker job vCPU time: `2583s` -> `2623s`). **Additional benchmarks** - **Strict Single-PR Isolated Microbenchmark (`upstream/master` vs `upstream/master` + this PR alone):** [`bench_49_tfrecord_struct_fastpath` (`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/microbenchmarks.py#L672-L681) (on branch [`perf/py-benchmarks-suite`](https://github.com/damccorm/beam/tree/perf/py-benchmarks-suite)): - **Variable-length payloads (200,000 `write_record` + `read_record` round-trips):** Improved from **`697.71 ms` -> `575.00 ms` (`1.21x` faster, `-17.6%`, `-122.71 ms`)** in strict isolation. - **Fixed 64-byte payloads (`bench_49`, 200,000 round-trips):** Improved from **`644.09 ms` -> `558.64 ms` (`1.15x` faster, `-13.3%`, `-85.45 ms`)** in strict isolation. - **On-Worker Cloud Dataflow `n2-standard-4` Benchmark (`p28_ms:27_tfrecord_struct_200k`):** On a Cloud Dataflow `n2-standard-4` worker, 200,000 TFRecord write+read round-trips improved from **`1,131 ms` -> `981 ms` (`1.15x` faster, `-13.3%`)**. - **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) (`3M` records, **1.48x faster: `49.2s` -> `33.3s`**) and `suite4_avro_tfrecord_mltransform` (`2M` records, **1.23x faster: `108.8s` -> `88.7s`**). ### Testing - Verified with `pytest sdks/python/apache_beam/io/tfrecordio_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]
