wvanbergen / chunky_png

Read/write access to PNG images in pure Ruby.
https://chunkypng.com
MIT License
1.05k stars 101 forks source link

Microoptimize ImageData.combine_chunks #103

Closed lencioni closed 8 years ago

lencioni commented 8 years ago

When doing some memory profiling, I noticed that this line was allocating by far the most memory in my application. Looking for a way to bring this down a little, I discovered that we can avoid building and joining our own array here by just pushing onto the stream directly.

Using memory_profiler in my application, this appears to bring memory allocation down from 1,158,307,467 bytes (499,055 objects) to 1,154,056,455 bytes (498,849 objects), which is a difference of 4,251,012 bytes (roughly 4 MiB). While not the juicy win I'd really like to see here, it seems like a simple enough change for the payoff.

I did see slightly worse memory usage for the smallest of images due to the overhead of allocating the memory for instantiating the Zlib::Inflate class, but I think this is a worthwhile tradeoff.

wvanbergen commented 8 years ago

:balloon: :fireworks:

lencioni commented 8 years ago

@wvanbergen I think I'm done hacking on this for a while so it would be an alright time to cut a new version now.

wvanbergen commented 8 years ago

done

On Oct 28, 2015, at 1:51 PM, Joe Lencioni notifications@github.com wrote:

@wvanbergen https://github.com/wvanbergen I think I'm done hacking on this for a while so it would be an alright time to cut a new version now.

— Reply to this email directly or view it on GitHub https://github.com/wvanbergen/chunky_png/pull/103#issuecomment-151929555.