Skip to content

Protect API objects via GIL instead of atomics #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2022

Conversation

adamreichold
Copy link
Member

Instead of slowing everything down via atomics, utilize that we already hold the GIL when accessing PY_ARRAY_API and PY_UFUNC_API.

@codecov
Copy link

codecov bot commented Jan 26, 2022

Codecov Report

Merging #269 (8ae2634) into main (bc4bf35) will increase coverage by 1.18%.
The diff coverage is 85.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #269      +/-   ##
==========================================
+ Coverage   81.66%   82.84%   +1.18%     
==========================================
  Files          14       13       -1     
  Lines        1554     1551       -3     
==========================================
+ Hits         1269     1285      +16     
+ Misses        285      266      -19     
Impacted Files Coverage Δ
src/npyffi/array.rs 75.00% <75.00%> (-3.05%) ⬇️
src/dtype.rs 94.06% <82.35%> (-0.81%) ⬇️
src/npyiter.rs 79.61% <82.60%> (+0.23%) ⬆️
src/array.rs 85.40% <94.44%> (+0.23%) ⬆️
src/npyffi/mod.rs 100.00% <100.00%> (ø)
src/sum_products.rs 98.27% <100.00%> (+0.12%) ⬆️
src/npyffi/ufunc.rs

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bc4bf35...8ae2634. Read the comment docs.

…dy hold the GIL when accessing PY_ARRAY_API and PY_UFUNC_API.
@adamreichold adamreichold force-pushed the use-gil-instead-of-atomics branch from 793e512 to 8ae2634 Compare January 26, 2022 20:59
@davidhewitt
Copy link
Member

Nice, agree there's no need for an atomic here!

I wonder, do you think it's good enough to make all of these unsafe with the # Safety requirement "the caller must be holding the Python GIL"? That would drop the need to have py entirely. (And you could always use Python::assume_gil_acquired() if needed.)

@adamreichold
Copy link
Member Author

adamreichold commented Jan 28, 2022

I wonder, do you think it's good enough to make all of these unsafe with the # Safety requirement "the caller must be holding the Python GIL"? That would drop the need to have py entirely. (And you could always use Python::assume_gil_acquired() if needed.)

I am actually rather fond of passing the token around as type-level proof of having the lock. I am not sure whether passing a zero-sized type is actually zero-cost in the current Rust ABI, but it should probably not matter at all on architectures with a sufficient number of registers to pass arguments in.

Also note that supplying the token was really mechanic work. There was not a single place where I had to conjure it up out of thing air (using assume_gil_acquired or going via with_gil), but already had safe access to it everywhere I needed it due to this change.

In summary, I would prefer to keep the token around as it significantly improves the maintainability of this code.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me!

@adamreichold adamreichold merged commit 6275f48 into main Jan 28, 2022
@adamreichold adamreichold deleted the use-gil-instead-of-atomics branch January 28, 2022 10:29
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.

2 participants