alt.hn

6/23/2026 at 12:56:16 PM

Lossless GIF recompression via exhaustive search

https://blog.arusekk.pl/posts/lossless-gif-recompression/

by ZacnyLos

6/23/2026 at 3:41:07 PM

> Here I should note that Python is not the best choice for CPU-bound software. I want to take the opportunity to learn Zig.

For optimizing CPU-bound operations in Python, there’s some low hanging fruit with numba. I would recommend this as a 5-minute solution to you limiting your algorithm because regular Python is too slow. I regularly tell people that if their Python program is slow enough to take several minutes, you could probably learn numba before it finishes.

by gsquaredxc

6/23/2026 at 9:24:52 PM

For exhaustive searches, a library like or-tools or clingo can speed things up a lot. But first you have to figure out how to express your problem declaratively.

by EliasWatson

6/23/2026 at 5:16:50 PM

PyPy is an option too.

by sltkr

6/23/2026 at 4:25:02 PM

Nice advice

by davidkwast

6/23/2026 at 5:29:00 PM

Neat. I tested it and here were my results:

ffmpeg conversion: 1,515 bytes, 0.05s

zgif conversion: 1,479 bytes, 90.1s

I used cursor to port it to rust as well, and it got the conversion time down to 20s. Still likely not worth it as even with the rust port it's a 400x increase in processing time (that scales exponentially) for a ~2% decrease in size.

by jjcm

6/23/2026 at 5:49:00 PM

> We need to be careful here, because finding the actual best compression for an arbitrary format can be equivalent to solving the halting problem.

I don't see why that would be relevant here. The goal is to find the best compression of a given source among all possible DEFLATE streams, not to find the best compression of a given source among all possible ways to compress data. There are only so many DEFLATE streams shorter than the original source, you "simply" try them all, and then you've halted. That said:

> Now, Zopfli is the software that performs an exhaustive search across all possible syntactically valid streams

Maybe I don't understand LZ77 as well as I thought, but surely the search space is way too big to exhaust?

by BigTTYGothGF

6/23/2026 at 7:00:20 PM

> We need to be careful here, because finding the actual best compression for an arbitrary format can be equivalent to solving the halting problem.

That sentence was pasted unmodified from the LLM output.

by ghssds

6/23/2026 at 6:48:03 PM

Too big to exhaust... the concluding sentences of the article contain the details. "Hybrid search with pruning" is the current state of the codebase.

by dj_axl

6/23/2026 at 9:07:42 PM

That's where OP ended up, I was disputing the description of zopfli.

by BigTTYGothGF

6/23/2026 at 4:28:11 PM

Love the goal of supporting ancient browsers. What I’m missing in the article about images is images. For example the achieved results and some table of the amount of space saved using the compression methods described. Nonetheless interesting read.

by Stitch4223

6/23/2026 at 4:55:23 PM

I agree, it would be way more interesting to see a table of original GIF sizes versus the improved size. I'm left wondering how bad the plain greedy version is on a typical image.

by richard_todd

6/23/2026 at 3:06:30 PM

Cool effort! I appreciated the background into the why, as initially I thought this would have to do with optimizing animated GIFs and didn't realize there was a usecase for single frame GIFs anymore.

by kernelbugs

6/23/2026 at 3:18:35 PM

> didn't realize there was a usecase for single frame GIFs anymore

If the purpose is supporting NCSA Mosaic… I’m content to say that there isn’t. Definitely not “anymore”.

by chrismorgan