Home/Blog/LastPNG: How organizing coding agents helps make PNG encoding 16× faster

LastPNG: How organizing coding agents helps make PNG encoding 16× faster

I had two ideas I wanted to try over a weekend. The first was about PNG. Say I’m happy with the file size my encoder gives me. How much of the time it spends encoding is actually necessary to get there?

The second idea was about coding agents. Agents are good at research. They can dig up information, make connections, and usually give me a few ideas when I explicitly ask, “How would you do this better?” Once they start implementing an idea, though, they rarely stop to ask themselves that same question. I wanted to see what would happen if asking that question became part of the job.

Give the agent a colleague

Before I put the agents to work, I needed a way to judge an improvement. At Uploadcare we resize images on demand. On a cache miss, the client waits while we encode the PNG. If we serve that file a million times, a few bytes saved on every response add up. So for every change I wanted both numbers: how much faster did it run, and what happened to the file size?

We spent about two hours getting the benchmark into shape. For comparison, we included libpng with both zlib and zlib-ng, the Rust png crate, and two optimizers that spend more CPU time looking for smaller files: OxiPNG and ZopfliPNG. The input set mixed screenshots, maps, scans, sprites and photographs. Besides total time and bytes, I wanted to be able to pick one image and find out what the winning encoder had done to it.

With the benchmark ready, I asked the main agent to bring in a colleague and ask it, “How would you do this better?” The colleague would propose an idea. The main agent would test it, then take the results back to its colleague and ask again. I no longer had to step in after every experiment to prompt the next idea.

I asked the agents to assign each optimization to a compression level based on its effect on file size and encoding speed. Over those iterations, they found changes that let the encoder reach the same file size in less time.

How LastPNG compares

I called the encoder LastPNG, which sounds a bit like “fast PNG” or “Rust PNG”. The comparison I care about most is with libpng using zlib at level 6, the default compression setting for most apps. On an Apple M4 Pro, LastPNG at level 2 encodes 4,079 images in 9.18 seconds and produces 914 MB of PNGs. libpng produces roughly the same number of bytes and takes nearly sixteen times as long.

The other presets give that number some context.

  • Level 1 encodes raw pixels at 4.34 GB/s on a single Apple M4 Pro core with an 8.23:1 compression ratio. That’s 1.65× the throughput of the fastest preset in Rust’s png crate, with almost 10% fewer output bytes.
  • Level 3 (the default) produces 837 MB, about 6% smaller than the output of libpng with zlib-ng at level 9, in roughly one-eleventh of the time.
  • Level 5 produces 777 MB in 277 seconds. OxiPNG level 1 gets down to 771 MB in 386 seconds. At its highest setting, OxiPNG saves another 2% over its level 1 (756 MB total), but encoding takes nearly three hours instead of six and a half minutes.

Both charts use a logarithmic scale for encoding time. The gap from 10 to 100 seconds is the same width as the gap from 100 to 1,000 seconds.

LastPNG level 2 encodes 4,079 images in about one-sixteenth of the time libpng takes with zlib at level 6, with nearly identical output sizes.LastPNG level 2 encodes 4,079 images in about one-sixteenth of the time libpng takes with zlib at level 6, with nearly identical output sizes.

Those are sums of single-threaded encode times with pixels already in memory. The benchmark harness decodes each result and checks dimensions, alpha and visible colors outside the timer.

At these speeds, PNG can compete with newer lossless formats when encoding time matters. I compared LastPNG with QOI, WebP, JPEG XL and AVIF on the 4,035 images that all five encoders could handle.

Lossless encoding time and total output size for LastPNG, QOI, WebP, JPEG XL and AVIF on 4,035 images.Lossless encoding time and total output size for LastPNG, QOI, WebP, JPEG XL and AVIF on 4,035 images.

At the far left of the chart, LastPNG level 1 delivers 2.9× the throughput of QOI at almost the same output size. JPEG XL at effort 1 produces files about 10% smaller than LastPNG level 2, but takes 1.8× as long to encode them. Further to the right, lossless WebP offers a range of smaller sizes, and JPEG XL at efforts 7–9 produces the smallest files in the comparison. AVIF preset 3 takes about 18 times as long as LastPNG level 6 to save just 0.2% more space.

What it took

My initial hunch was that a compressor could make better use of the data if it knew how the bytes were arranged into pixels and rows. I chose libdeflate to test that idea. It paid off, and I kept a copy of the library in the repository so I could patch it for the cases I found. One patch lets LastPNG use the compressor’s match information when choosing a PNG filter for each row. Different filters can turn the same pixels into different byte sequences, some of which match data the compressor has already seen. At level 6, LastPNG uses a 1,584-parameter neural network to predict which filter will compress the row best based on hints about those matches and a few properties of the image. I’m still not happy with the results, so I consider level 6 experimental.

Try it on your own images

LastPNG has a Rust crate and a C API. If you use Pillow, install the lastpng plugin from PyPI. The wheels bundle the native libraries for macOS 11 or later (Intel and Apple silicon), Windows (x86-64), and Linux (x86-64 and ARM64, with glibc or musl).

python -m pip install lastpng
from PIL import Image
import LastPNGImagePlugin

with Image.open("input.png") as image:
    image.save("output.png", format="LASTPNG", level=2)

LastPNG may turn an RGBA screenshot into a palette image if it can represent the same colors that way. Alpha and visible colors are preserved exactly. RGB values under fully transparent pixels may change.

Try that against your usual PNG save. You can also try LastPNG on Uploadcare’s servers by requesting the same image with -/format/png/ in its CDN URL.

If your usual encoder produces a smaller file and finishes sooner, I’d love to see the image and the settings you used. That’s exactly the sort of result I’d take back to my two colleagues.

Ready to get started?

Join developers who use Uploadcare to build file handling quickly and reliably.

Sign up for free