Skip to content

Make the ffi crate no_std#6022

Open
Person-93 wants to merge 7 commits intoPyO3:mainfrom
Person-93:no_std_ffi
Open

Make the ffi crate no_std#6022
Person-93 wants to merge 7 commits intoPyO3:mainfrom
Person-93:no_std_ffi

Conversation

@Person-93
Copy link
Copy Markdown
Contributor

@Person-93 Person-93 commented May 8, 2026

I've added no_std to the python-ffi crate.

Most of the changes in this PR are a search-and-replace from std:: to core::. The only other change I've made is in the datetime file. It used the Once type from std. I've updated it to use AtomicBool instead.

This is the first PR from this converstaion: #5856 (comment)

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 8, 2026

Merging this PR will improve performance by 13.7%

⚡ 1 improved benchmark
✅ 104 untouched benchmarks
⏩ 1 skipped benchmark1

Performance Changes

Benchmark BASE HEAD Efficiency
bench_pyclass_create 4.6 µs 4.1 µs +13.7%

Comparing Person-93:no_std_ffi (48b06f9) with main (f57bda7)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

@Person-93
Copy link
Copy Markdown
Contributor Author

The ffi crate currently uses std::thread::park to work around the interpreter calling pthread_exit for python versions older than 3.14.

#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
struct HangThread;
#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
impl Drop for HangThread {
fn drop(&mut self) {
loop {
std::thread::park(); // Block forever.
}
}
}

I can think of two ways to handle this.

  • move the usage into the main crate
  • use a feature-flag to conditionally use a separate crate that can sleep in no_std

The first way is technically a breaking change if anyone is using the PyGILState_Ensure method from the ffi crate as they would have to deal with pinned stack objects some other way, but I think it's simpler, so I will go with that one unless you object.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant