-
Notifications
You must be signed in to change notification settings - Fork 760
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
Improve performance on pointer drop #851
Conversation
let pool: &'static mut ReleasePool = &mut *POOL; | ||
pool.release_pointers(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to release pointers here?
To reduce memory usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two reasons:
- Yes, memory usage - we're now releasing the pointers sooner
- This (Why object creation is slow in PyO3 and how to enhance it #679 (comment)) suggested to me that it's important in the long run we migrate to releasing objects on GIL acquire, not GIL exit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I understand. Thank you for the reference.
Great, thanks!
I'm not sure how the next |
66aaf86
to
ca027cf
Compare
Good idea. I now use both |
Formatting error again 🤦 I'll fix later |
ca027cf
to
8f79760
Compare
Re-pushed with fixed formatting; should be green in CI now I hope. |
8f79760
to
b7b6dcb
Compare
57c9bc9
to
6e4e86b
Compare
Co-Authored-By: Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com>
6e4e86b
to
fe57f64
Compare
Thank you! |
There's a
Mutex
involved each time we drop aPyObject
orPy<T>
, so that we can safely push the reference into the sharedVec
. I have been wondering for a while if we could add a simple check if the GIL is held (as athread_local!
variable) and if so, decrease the refcount immediately.I tried this out - turns out in the simple benchmark I made (
bench_object
) that adding this check makes the benchmark 5x faster.I think decreasing the reference count sooner is also more predictable from a pyo3 user perspective. It helps with some of the issues around #311. (Though this doesn't improve the situation for references.)
If the GIL is not held, then I made it so that the pointers will be released as soon as the GIL is next acquired.
To track when the GIL is held I have added counters to
GILGuard::new()
andGILGuard:drop()
.For this optimization to apply all the time we also need to increment / decrement this counter when we call
assume_gil_acquired()
- which suggests to me that we should wait to finish this off once #800 is solved.TODO:
Also increment / decrement GIL_COUNT when we call(solved by also usingassume_gil_acquired()
GILPool
for the counter)