-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Release the GIL when decoding the image #1
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
feat: Release the GIL when decoding the image #1
Conversation
|
Hi! Thank you very much for the contribution! I like the idea, but something seems to be off: I'm getting a segmentation fault on Linux while running tests on your branch and getting the following error when running the benchmark script you provided: In order to figure out whether it's just my environment or not, I enabled CI for PRs - could you rebase your fork to the current state of the upstream please? |
|
@dev0x13 Odd, I definitely didn't experience that when testing on my own environment. May I ask what python version you're running? I've sync'd my fork, but it appears the workflows need approval to be run first |
|
@hazza203 So the CI fails too... I'm using Python 3.12 locally, and 3.7 and 3.13 are also used in CI. However, the root cause of the issue seems to be Having the aforementioned fields stored as NumPy arrays is somewhat a feature (yet implemented quite poorly) which was intended to eliminate the need to copy decoded data from C++ layer back to Python. I think it's possible to make these two fields If you feel comfortable doing this yourself, I'll be happy to accept your PR with the needed changes, but if not, I could do it myself considering this a feature request. |
|
Understood, I've given this a shot, but my lack of experience with C++ and pybind is showing. I'm struggling to come up with a working solution for this, so maybe label it as a feature request. I have run all of the tests locally, and most of them are actually passing and don't cause a seg fault. (python3.11). I've identified some clues as to what specifically is failing. It seems to be closely related to the REPORT_METADATA flags, and curiously, not for every image. This leads me to believe that the unsafe code is most likely related to TLDR: Commenting out lines 47, 177, 216 in
We don't use any flags when decoding, so for now, we can continue with my fork. Though it is unsafe, it works for our purposes. But ideally I'd love to get a solution on the main repo. I'd love to see what the solution looks like if you do indeed get around to it. Thanks for the feedback and all of your hard work on this project! |
|
Thank you for the investigation and for the provided clues! |
|
@dev0x13 Sick! Nice work, can't wait to check it out |
Hey there, firstly, thanks a heap for making this package. It's been great for us as we try and optimize some of our image loading workflows. We work with really large images so being able to load them in quicker is a blessing.
Though upon some testing I discovered that this package isn't so great for loading in multiple images at the same time via the use of threads, actually, it performs significantly worse than opencv. Most python image libraries which make use of C-extensions seem to release the GIL at some point to improve multi-threaded performance.
So I've added some handling here to release the GIL, and it works. But I'm not sure if this is the best solution / there may be a more optimal place to do it as I believe there are still some areas for optimization. Also just FYI, I'm not that experienced with writing python-C binding code, this are really my first crack at it.
Even after these changes, it is only on-par with opencv in terms of multi-threaded performance, whereas it still beats it in single threaded performance, so I reckon there are still areas for improvement.
Let me know what you think, if this change is even desired and if there may be some more optimizations we can do! I've added some timings and a test script below:
Benchmarks, using a 15k RGBA image (15000,13000)