damccorm opened a new pull request, #41271: URL: https://github.com/apache/beam/pull/41271
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 `MetricName` in `sdks/python/apache_beam/metrics/metricbase.py`: 1. **Cache `_cached_fast_name` and `_cached_hash` on `MetricName`, and fast-path `self is other` in `__eq__`:** - Previously, `hash(metric_name)` built and hashed `(self.namespace, self.name, self.urn) + tuple(self.labels.items())` on every `MetricsContainer` dictionary lookup, and `fast_name()` formatted strings on every call. 2. **Initialize `_typed_metric_names` dict on `MetricName` (and reset cached fields in `__getstate__`/`__setstate__`).** **Dataflow benchmarks showing improvement:** - Before the change: [`2026-11-12_13_27_21-13885815863092652873`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_13_27_21-13885815863092652873?project=apache-beam-testing) - After the change: [`2026-11-12_13_27_21-16378524632386198380`](https://console.cloud.google.com/dataflow/jobs/us-central1/2026-11-12_13_27_21-16378524632386198380?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 (`240,000,000` metric ops) · 2× `ParDo(Metrics.counter / MetricName.__hash__ & fast_name)`, built-in transform stage execution time improved from **`2,255,354 ms` -> `893,556 ms` (`2.52x` faster, `-60.4%`)** (total single-worker job vCPU time: `3441s` -> `1945s`). **Additional benchmarks** - **Strict Single-PR Isolated Microbenchmark (`upstream/master` vs `upstream/master` + this PR alone):** [`bench_30_metric_name_cache_hash` (`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/microbenchmarks.py#L443-L450) (on branch [`perf/py-benchmarks-suite`](https://github.com/damccorm/beam/tree/perf/py-benchmarks-suite)) — 1,000,000 `hash(metric_name)` and `fast_name()` calls improved from **`540.16 ms` -> `129.57 ms` (`4.17x` faster, `-76.0%`, `-410.59 ms`)** in strict isolation. - **Per-Change Attribution (Most to Least Impact, 1,000,000 calls each in strict isolation):** 1. *Most impact (`65.6%` of savings, `-275.43 ms`):* Caching `_cached_fast_name` on `MetricName` — **`325.42 ms` -> `49.99 ms` (`6.51x` faster, `-84.6%`)**. 2. *Second (`34.4%` of savings, `-144.49 ms`):* Caching `_cached_hash` on `MetricName` — **`263.74 ms` -> `119.25 ms` (`2.21x` faster, `-54.8%`)**. - **On-Worker Cloud Dataflow `n2-standard-4` Benchmark (`p28_ms:12_metric_name_hash_fastname_1m` & `subbench_ms:03_metrics_container_lookup_ms`):** On a Cloud Dataflow `n2-standard-4` worker, 1,000,000 `hash(mn)` + `mn.fast_name()` calls improved from **`1,056 ms` -> `334 ms` (`3.16x` faster, `-68.4%`)**, and 512,000 `MetricsContainer.get_counter(MetricName).inc()` updates improved from **`517 ms` -> `180 ms` (`2.87x` faster)**. ### Testing - Verified with `pytest sdks/python/apache_beam/metrics/metricbase_test.py sdks/python/apache_beam/metrics/execution_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]
