damccorm opened a new pull request, #41214:
URL: https://github.com/apache/beam/pull/41214
### Description
Optimizes `StrUtf8Coder` and `BytesCoderImpl` in
`sdks/python/apache_beam/coders/coder_impl.py`,
`sdks/python/apache_beam/coders/coder_impl.pxd`, and
`sdks/python/apache_beam/coders/coders.py`:
1. **Override `BytesCoderImpl.estimate_size(self, value, nested=False)`
using `self._get_nested_size(len(value), nested)`:**
- Avoids falling back to `CoderImpl.estimate_size`, which allocates a
`ByteCountingOutputStream` and calls `encode_to_stream` on every size
estimation.
2. **Add Cython `cdef class StrUtf8CoderImpl(CoderImpl)` and wire
`StrUtf8Coder._create_impl`:**
- Replaces the default `CallbackCoderImpl` wrapper on `StrUtf8Coder` with
a native Cython `CoderImpl` implementing `encode_to_stream`,
`decode_from_stream`, `encode`, `decode`, and `estimate_size`.
3. **Short-circuit `type(value) is not bytes` before `isinstance(value,
np.bytes_)` in `BytesCoderImpl.encode_to_stream`:**
- Skips the `isinstance(value, np.bytes_)` check for standard `bytes`
objects on the Cython path.
### Benchmarks & Performance Impact
- **Microbenchmark Rerun:** [`bench_16_str_utf8_and_bytes_coder_impl`
(`c2feb961eef`)](https://github.com/damccorm/beam/blob/c2feb961eef2e7d2410825aab9537c62a95e6790/sdks/python/apache_beam/testing/benchmarks/python_sdk_perf/microbenchmarks.py#L273-L284)
(on branch
[`perf/py-benchmarks-suite`](https://github.com/damccorm/beam/tree/perf/py-benchmarks-suite))
— 500,000 `StrUtf8CoderImpl` encode/decode round-trips +
`BytesCoderImpl.estimate_size` calls improved from **`189.72 ms` -> `66.92 ms`
(`2.84x` faster, `-64.7%`)** in compiled Cython.
- **Per-Change Attribution (Most to Least Impact, 500,000 calls each):**
1. *Most wall-time impact (`57.3%` of savings, `-51.37 ms`, `3.61x`
faster):* Overriding `BytesCoderImpl.estimate_size` with
`self._get_nested_size(len(value), nested)` instead of allocating
`ByteCountingOutputStream` — **`71.03 ms` -> `19.66 ms` (`3.61x` faster)**.
2. *Second (`42.7%` of savings, `-38.32 ms`, `1.49x` faster):* Adding
Cython `cdef class StrUtf8CoderImpl(CoderImpl)` (`encode_to_stream`,
`decode_from_stream`, `encode`, `decode`, `estimate_size`) and `type(value) is
not bytes` short-circuiting — **`117.06 ms` -> `78.74 ms` (`1.49x` faster)**.
- **End-to-End Cloud Dataflow Benchmark (`io_coders_transforms`):** 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)
(3,000,000 records across TFRecordIO, FileSystems, Coders, BatchElements,
Partition, and ApproximateQuantiles on `n2-standard-4`), which improved worker
stage execution time by **1.48x (`-32.4%`, `49.2s` -> `33.3s`)** and reduced
total billable Dataflow vCPU/Memory/PD usage by **`-11.2%` (`717` -> `637`
vCPU-s)**.
### Testing
- Verified with `pytest sdks/python/apache_beam/coders/coders_test.py
sdks/python/apache_beam/coders/fast_coders_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]