Skip to content

[Python] Merge runtime type-check wrappers - #40501

Open
Mazen050 wants to merge 2 commits into
apache:masterfrom
Mazen050:fix/runtime-typecheck-wrapper-overhead
Open

Mazen050 wants to merge 2 commits into
apache:masterfrom
Mazen050:fix/runtime-typecheck-wrapper-overhead

Conversation

@Mazen050

Copy link
Copy Markdown

Summary

Fixes #20226

This PR reduces runtime type-checking overhead in the Python SDK by consolidating two nested DoFn wrappers into a single RuntimeTypeCheckWrapperDoFn.

Previously, input and output checks were handled by separate wrappers. The merged wrapper aims to preserve existing type-checking behavior while eliminating one level of Python call indirection per processed element.

Changes

  • Consolidate OutputCheckWrapperDoFn and TypeCheckWrapperDoFn into RuntimeTypeCheckWrapperDoFn.
  • Cache the unbound process function while avoiding storing a bound method on the wrapper, preserving stateful DoFn validation.
  • Preserve input and output type checking, output-shape validation, and transform error labeling.
  • Add regression tests for wrapper application, error reporting, result preservation, and stateful DoFn compatibility.

Tests

  • apache_beam/typehints/typecheck_test.py: 18 passed.
  • apache_beam/transforms/ptransform_test.py: 202 passed, 4 skipped.

Performance

Compared the original and modified implementations using Beam's existing runtime_type_check_microbenchmark.

Configuration: 20 DoFns and three timing repetitions per configuration.

Elements Simple types (before → after) Nested types (before → after)
501 3.22s → 3.06s 3.90s → 3.41s
1,001 5.44s → 5.39s 6.44s → 6.07s
1,501 7.57s → 7.39s 8.93s → 8.83s
2,001 9.91s → 9.46s 11.84s → 11.22s

At 2,001 elements, the measured runtime was approximately 4.5% lower for simple type hints and 5.2% lower for nested type hints in this local run.

These measurements are preliminary and may vary by environment. They demonstrate the results of this benchmark configuration, not a guaranteed improvement for all pipelines.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @claudevdm for label python.

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 56.23%. Comparing base (76f6e6d) to head (6190609).
⚠️ Report is 139 commits behind head on master.

Files with missing lines Patch % Lines
sdks/python/apache_beam/typehints/typecheck.py 94.11% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #40501      +/-   ##
============================================
+ Coverage     56.16%   56.23%   +0.06%     
  Complexity     2288     2288              
============================================
  Files          1124     1124              
  Lines        178336   178366      +30     
  Branches       1489     1489              
============================================
+ Hits         100170   100303     +133     
+ Misses        75645    75542     -103     
  Partials       2521     2521              
Flag Coverage Δ
python 79.68% <94.11%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

python typehints: reduce the number of runtime-type-check wrappers

1 participant