Skip to content
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

feature gate deprecated APIs for GILPool #4181

Merged
merged 1 commit into from
May 17, 2024
Merged

Conversation

alex
Copy link
Contributor

@alex alex commented May 12, 2024

No description provided.

@alex alex added the CI-skip-changelog Skip checking changelog entry label May 12, 2024
@alex
Copy link
Contributor Author

alex commented May 12, 2024

Some pieces of this don't feel super idiomatic -- GILGuard::acquire() returns None if the GIL is already acquired, so I didn't have an easy solution like adding a python() method to it.

@alex alex mentioned this pull request May 12, 2024
51 tasks
@adamreichold
Copy link
Member

Some pieces of this don't feel super idiomatic -- GILGuard::acquire() returns None if the GIL is already acquired, so I didn't have an easy solution like adding a python() method to it.

Yeah, that one is one me. My only defence is that this is the result of actually collapsing another layer of indirection, i.e. #3166.

But also have a look at #3685 where I introduced WithGIL as an RAII to use without GILPool, i.e. a type that we would keep in 0.23 when the GILPool is actually fully dropped.

Copy link

codspeed-hq bot commented May 12, 2024

CodSpeed Performance Report

Merging #4181 will improve performances by 11.02%

Comparing alex:remove-gil-pool (16f8a02) with main (1c64a03)

Summary

⚡ 1 improvements
✅ 67 untouched benchmarks

Benchmarks breakdown

Benchmark main alex:remove-gil-pool Change
drop_many_objects 66.9 µs 60.3 µs +11.02%

@alex
Copy link
Contributor Author

alex commented May 12, 2024

I think there's a design question here, which is whether we want a new type, or we should just have GILGuard::acquire always return an instance?

@adamreichold
Copy link
Member

I think there's a design question here, which is whether we want a new type, or we should just have GILGuard::acquire always return an instance?

The separate type seemed like the easier evolution but that approach has landed us in this mess in the first place.

If GILGuard should always return an instance, then I think it should itself become an Option-like enum with a variant containing PyGILState_STATE and a GILPool (or not depending on the feature, but possibly) and a None-like variant when there is nothing to be done.

@adamreichold
Copy link
Member

IIRC, the main point was that GILGuard does not really help for the entry points when we know that GIL is already held and just want to set the correct lifetimes and update our own GIL count tracking. For this, the bare WithGIL type was rather helpful.

src/gil.rs Outdated

#[cfg(not(feature = "gil-refs"))]
let result = {
let _guard = GILGuard::acquire_unchecked();
Copy link
Member

Choose a reason for hiding this comment

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

Ah, exactly, this is calling PyGILState_Ensure for which there is no reason to in the entry points.

@davidhewitt
Copy link
Member

I think a WithGIL new type seems preferable to me; making the GILGuard be purely RAII seems like a better separation of concerns even if it works out a bit more verbose.

@davidhewitt
Copy link
Member

Actually given both types will also be RAII on the internal GIL_COUNT, on reflection I like the enum-like GILGuard too. I'd suggest Held and Assumed for the two variant names, given we use the term assume elsewhere too.

@adamreichold
Copy link
Member

adamreichold commented May 13, 2024

I'd suggest Held and Assumed for the two variant names, given we use the term assume elsewhere too.

I would suggest calling the Held variant Ensured as it is the one which involves a call to PyGILState_Ensure.

One potential downside I would mention (but I have not measured it), is that this will mean that we could end up allocating unused stack space for Python's thread state in every entry point. Probably depends on whether the compiler will SROA-explode the type or not...

@alex
Copy link
Contributor Author

alex commented May 13, 2024

I'll maybe get back to this today, or maybe not for a few days. If anyone else is champing at the bit to do that refactor, don't feel compelled to wait for me. Or I'll get to it later this week :-)

@davidhewitt
Copy link
Member

One potential downside I would mention (but I have not measured it), is that this will mean that we could end up allocating unused stack space for Python's thread state in every entry point. Probably depends on whether the compiler will SROA-explode the type or not...

Ah, true. I think the state returned by PyGILState_Ensure is just an integer, but still a downside.

Actually given both types will also be RAII on the internal GIL_COUNT, on reflection I like the enum-like GILGuard too. I'd suggest Held and Assumed for the two variant names, given we use the term assume elsewhere too.

Thinking further, I realise this is a non-argument as GilGuard can just contain a WithGIL, and WithGIL can then always be the RAII on GIL_COUNT. So I guess the answer is let's go with whatever feels cleaner in code.

@alex
Copy link
Contributor Author

alex commented May 17, 2024

This is rebased and should be ready for review now.

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.

🎉 wonderfully trivial now, thanks!

@davidhewitt davidhewitt added this pull request to the merge queue May 17, 2024
Merged via the queue into PyO3:main with commit fe79f54 May 17, 2024
42 of 43 checks passed
@alex alex deleted the remove-gil-pool branch May 17, 2024 12:39
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.

None yet

3 participants