cgif
cgif copied to clipboard
Optimize frame comparison
Context
I have been creating lots of animated charts lately and I have found that the performance of cgif_addframe
is very slow for my use case.
The GIFs I'm creating have large dimensions and many frames. Not very much changes from frame-to-frame. I enable the flag CGIF_FRAME_GEN_USE_DIFF_WINDOW
to get small file sizes.
I profiled it and found that most of the time was spent in the cmpPixel
function. This function is used in two places:
- The checking for the area that actually changed in
doWidthHeightOptim
. This is done when theCGIF_FRAME_GEN_USE_DIFF_WINDOW
flag is set. - The checking for identical frames, which is done when
CGIF_GEN_KEEP_IDENT_FRAMES
is not set.
Optimization
This pull request changes the library to use a faster comparison if both the current and previous frame use the global palette. When both use the global palette, it compares the pixel data directly. This can allow it to compare whole rows or the whole image with a single memcmp
. I tried to keep to the project code style.
Results
These changes make my program run 30 times faster.
Considering just the time spent in CGIF it's 80 times faster.
Questions
There are a couple of things I'm unsure about.
The first is a query about the original behavior of cmpPixel
. It looks at the frame before and the current frame, to check if they use the global palette. Is there a case where both the previous frame and the current frame both use the global palette, but some frame before that has used a local palette? If so, it seems like there could be a problem with the approach used in cmpPixel
. If it's not correct, maybe the CGIF
structure needs to contain a flag indicating whether a local palette has ever been used in an earlier frame, and check that instead of the previous frame. If that was a problem before, then this still PR maintains the same behavior.
Second I'm not sure if my optimization is okay with transIndex
.
Any feedback gratefully received.
@dloebl If it would be helpful I could e-mail you an example of the GIFs I'm making. (They're about 50kb).