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

Use Uint8Array for more efficient loading of GIF frame data #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jbaicoianu
Copy link

This change results in an approximately 10x speedup when parsing large GIFs. This is accomplished by using Uint8Array instead of binary strings, and using block memory allocators to increase memory efficiency/reduce GC load.

Using a basic for loop instead of pixels.forEach() to populate the canvas pixeldata also gave a significant performance boost.

@jbaicoianu
Copy link
Author

Coincidentally I also implemented the same changes as #25 before realizing there was a pull request open for it, so +1 for that as well.

@BenV
Copy link

BenV commented Sep 7, 2017

Doesn't appear this will ever get merged, but for anyone else using this branch the deinterlace method also needs to be updated to use TypedArrays, something like:

var deinterlace = function (pixels, width) {
    // Of course this defeats the purpose of interlacing. And it's *probably*
    // the least efficient way it's ever been implemented. But nevertheless...
    var newPixels = new Uint8Array(pixels.length);
    var rows = pixels.length / width;
    var cpRow = function (toRow, fromRow) {
        var fromPixels = pixels.subarray(fromRow * width, (fromRow + 1) * width);
        newPixels.set(fromPixels, toRow * width);
    };
...

@jbaicoianu
Copy link
Author

I haven't fully evaluated it or switched over, but https://github.com/gtk2k/gtk2k.github.io/blob/master/animation_gif/gifsparser.js seems to be a much cleaner and quicker way of parsing GIF frames, and it what A-Frame uses. libgif-js seems to be a dead project, and even with all the optimizations I've made it's still quite slow.

@fleps
Copy link

fleps commented May 8, 2018

@jbaicoianu Does https://github.com/gtk2k/gtk2k.github.io/blob/master/animation_gif/gifsparser.js allow us to achieve the simple GIF pause / resume functionality? I didn't found any instructions on the repo.

@wangdongzuopin
Copy link

How to get the total number of frames

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants