• Cezar Sa Espinola's avatar
    image/png: reduce memory allocs encoding images by reusing buffers · 93a18acf
    Cezar Sa Espinola authored
    This change allows greatly reducing memory allocations with a slightly
    performance improvement as well.
    
    Instances of (*png).Encoder can have a optional BufferPool attached to
    them. This allows reusing temporary buffers used when encoding a new
    image. This buffers include instances to zlib.Writer and bufio.Writer.
    
    Also, buffers for current and previous rows are saved in the encoder
    instance and reused as long as their cap() is enough to fit the current
    image row.
    
    A new benchmark was added to demonstrate the performance improvement
    when setting a BufferPool to an Encoder instance:
    
    $ go test -bench BenchmarkEncodeGray -benchmem
    BenchmarkEncodeGray-4                 	    1000	   2349584 ns/op	 130.75 MB/s	  852230 B/op	      32 allocs/op
    BenchmarkEncodeGrayWithBufferPool-4   	    1000	   2241650 ns/op	 137.04 MB/s	     900 B/op	       3 allocs/op
    
    Change-Id: I4488201ae53cb2ad010c68c1e0118ee12beae14e
    Reviewed-on: https://go-review.googlesource.com/34150Reviewed-by: 's avatarNigel Tao <nigeltao@golang.org>
    Run-TryBot: Nigel Tao <nigeltao@golang.org>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    93a18acf
Name
Last commit
Last update
..
color Loading commit data...
draw Loading commit data...
gif Loading commit data...
internal/imageutil Loading commit data...
jpeg Loading commit data...
png Loading commit data...
testdata Loading commit data...
decode_example_test.go Loading commit data...
decode_test.go Loading commit data...
format.go Loading commit data...
geom.go Loading commit data...
geom_test.go Loading commit data...
image.go Loading commit data...
image_test.go Loading commit data...
names.go Loading commit data...
ycbcr.go Loading commit data...
ycbcr_test.go Loading commit data...