drj11/pypng — pull request #11 by e98cuenc (2012-09-24 07:51:28+00:00)
Title: Optimize iterstraight

This took me a while, as I had some false starts. Reading a 800x800 image was taking ~150ms on my laptop, the main offenders being:

iterstraight - 50ms
cpngfilters.undo_filter_paeth - 37ms
{built-in method decompress} - 16ms
{method 'read' of 'file' objects} - 9ms
...

iterstraight spends most of its time extending the a array to then pass a slice of it. Very often (depends on the IDAT chunks) it can just pass a slice the "some" array. I take advantage of this to create a fast-path for this case (the while loop).

This takes iterstraight from 50ms down to 2ms, putting undo_filter_* back as the top offenders.

I took a second look at undo_filter_*, but I have not found any way to make them faster. The C code seems relatively clean and straightforward, so it will take someone better than me to improve upon this.