rust-cpython icon indicating copy to clipboard operation
rust-cpython copied to clipboard

Use GILProtected for capsule caching?

Open SimonSapin opened this issue 4 years ago • 2 comments

The py_capsule and py_capsule_fn macros generate code that cache the pointer obtained from a capsule in a static mut item, with a std::sync::Once in a second static item for thread-safety.

Although the synchronization cost of Once is small, it seems unnecessary here since code touching this is always holding the GIL. Could this cache be implemented with a single static item of type cpython::GILProtected<std::cell::Cell<_>> instead?

SimonSapin avatar Jul 06 '21 15:07 SimonSapin

This is probably a question for @gracinet.

markbt avatar Aug 11 '21 12:08 markbt

I’ve submitted https://github.com/dgrunwald/rust-cpython/pull/268. Cell turned out not to be enough, this uses OnceCell instead. The fact that no cache-related unsafe code is needed anymore reinforces my belief that this approach is sound.

SimonSapin avatar Aug 12 '21 10:08 SimonSapin