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
GifImagePlugin seek-related optimizations #6075
GifImagePlugin seek-related optimizations #6075
Conversation
- Add: Added `self.__prev_offset` to always hold the offset of the frame just before the current frame. - Add: Seeking beyond the last frame automatically computes `GifImagePlugin.n_frames`. - Change: Optimized `GifImagePlugin.seek()`, `GifImagePlugin.n_frames` and `GifImagePlugin.is_animated` when seeking back to the current frame before invoking the operation, after an `EOFError` is raised. - Change: Replaced all internal calls to `GifImagePlugin.tell()` with direct references to `self.__frame`.
- Add: Included test for `n_frames` optimization.
For what it's worth, I realized the deficiencies in the course of working with a large (2188 frames, ~8 MiB) GIF image on a project I'm developing. |
Could you upload a copy of your test image? Not to be included in PR, but just as a comment here, so that we can run your speed tests in the same way you are. |
Oh, sorry. Thought it would be unnecessary since it should apply to any GIF. |
Just added some explanation in the PR message, thought it would be useful. |
Hmm... I see. 🤔 Thanks But can the part of |
Indeed, there's a fundamental problem. Invoking I guess i didn't realize all these earlier because the changes seemed to work fine with my use case. |
So, my first commit in #6077 helped I've now added a commit to #6077 that will check if the next byte (or lack thereof) ends the GIF image, and if so, just stay on the current frame. This should eliminate the reset for most cases - I could deliberately create an image that had some data past the last frame and so still needed to be reset, but I would hope that is rare, since I don't know why a GIF writer would do that. Testing those changes, |
I've pushed another commit - when performing seeks for So somehow, I think that should cover all of the significant differences in speeds from your tests here. |
Great! Thanks I'll try it out. |
1890f8a
to
aaf18a7
Compare
Closing. See #6077 instead. |
@radarhere |
To be fair, there were 4 failures in our test suite. |
I see... thanks for your rapid responses. |
Changes proposed in this pull request:
self.__prev_offset
to always hold the offset of the frame just before the current frame.GifImagePlugin.seek()
,GifImagePlugin.n_frames
andGifImagePlugin.is_animated
when seeking back to the current frame before invoking the operation, after anEOFError
is raised.GifImagePlugin.n_frames
.GifImagePlugin.n_frames
now callsGifImagePlugin._seek()
directly instead ofGifImagePlugin.seek()
..seek()
GifImagePlugin.tell()
with direct references toself.__frame
.Extra Explanation
Concerning
.n_frames
for example, when a GIF (say 57 frames) is currently on the mid frame (say 28), invoking.n_frames
for the first time would've resulted in the following chain of frame changes:Now, it goes thus:
Tests
The test image has 57 frames.
Results
Formerly:
Now: