Skip to content

Make FFI Pointers a bit more typesafe - #6445

Open
Person-93 wants to merge 9 commits into
PyO3:mainfrom
Person-93:ffi-ptr-more-typesafe
Open

Person-93 wants to merge 9 commits into
PyO3:mainfrom
Person-93:ffi-ptr-more-typesafe

Conversation

@Person-93

@Person-93 Person-93 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Objective

Simplify casting back and forth when unsafely converting raw pointers to pyo3 smart pointers. It's a bit error prone and makes the code appear very cluttered.

// this
let args: Borrowed<'py, 'py, PyTuple> =
     unsafe { Borrowed::from_ptr(py, args).cast_unchecked::<PyTuple>() };
let kwargs: Option<Borrowed<'py, 'py, PyDict>> =
    unsafe { Borrowed::from_ptr_or_opt(py, kwargs).map(|kwargs| kwargs.cast_unchecked()) };

// becomes this
let args = unsafe { args.cast::<FfiTypeOf<PyTuple>>().assume_borrowed(py) };
let kwargs = unsafe { kwargs.cast::<FfiTypeOf<PyDict>>().assume_borrowed_or_opt(py)};

The public API of smart pointers is expanded as well. They can be unsafely created from any pointer which implements FfiObj and the T param is the associated `ApiObj.

Changes

  • Add two hidden traits ApiObj and FfiObj
  • Update macros for implementing native type info to optionally implement those traits
  • Update pyclass macro to implement those traits
  • implement existing FfiPtrExt trait for all *mut T where T: FfiObj
  • Remove type casts from a few places in the codebase

@Person-93
Person-93 force-pushed the ffi-ptr-more-typesafe branch from 5b1eb2b to 3f0814e Compare September 21, 2026 04:52
@Person-93

Copy link
Copy Markdown
Contributor Author

I'm having trouble building the test_utils mod. https://github.com/PyO3/pyo3/actions/runs/35670200524/job/106564800184?pr=6445#step:7:1253

@Person-93
Person-93 force-pushed the ffi-ptr-more-typesafe branch from 8638c95 to 24e972d Compare September 22, 2026 16:46
@codspeed

codspeed Bot commented Sep 22, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 11.18%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 2 regressed benchmarks
✅ 141 untouched benchmarks
⏩ 6 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
into_i128_zero 817.2 ns 926.9 ns -11.84%
into_i128_small_pos 932.8 ns 1,042.5 ns -10.52%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Person-93:ffi-ptr-more-typesafe (25864be) with main (1655cdf)

Open in CodSpeed

Footnotes

  1. 6 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Person-93
Person-93 force-pushed the ffi-ptr-more-typesafe branch from fc18b94 to 18197a2 Compare September 22, 2026 18:14
@Person-93 Person-93 added the CI-skip-changelog Skip checking changelog entry label Sep 22, 2026
@Person-93
Person-93 force-pushed the ffi-ptr-more-typesafe branch from 8935abb to 25864be Compare September 22, 2026 18:30
@mejrs

mejrs commented Sep 22, 2026

Copy link
Copy Markdown
Member

These two feel ~similar in terms of how error prone and cluttered they feel. I'm not sure it's worth the churn.

This branch has not been deployed

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

Labels

CI-skip-changelog Skip checking changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants