Commit f04f594a authored by Nao YONASHIRO's avatar Nao YONASHIRO Committed by Nigel Tao

image/png: hoist repetitive pixels per byte out of loop in Encode

The existing implementation has calculated pixels per byte for each pixel.
reduce the calculation of pixels per byte.

name                        old time/op    new time/op     delta
EncodeGray-4                  2.16ms ± 1%     2.16ms ± 1%    -0.28%  (p=0.000 n=86+84)
EncodeGrayWithBufferPool-4    1.99ms ± 0%     1.97ms ± 0%    -0.72%  (p=0.000 n=97+92)
EncodeNRGBOpaque-4            6.51ms ± 1%     6.48ms ± 1%    -0.45%  (p=0.000 n=90+85)
EncodeNRGBA-4                 7.33ms ± 1%     7.28ms ± 0%    -0.69%  (p=0.000 n=89+87)
EncodePaletted-4              5.10ms ± 1%     2.29ms ± 0%   -55.11%  (p=0.000 n=90+85)
EncodeRGBOpaque-4             6.51ms ± 1%     6.51ms ± 0%      ~     (p=0.311 n=94+88)
EncodeRGBA-4                  24.3ms ± 2%     24.1ms ± 1%    -0.87%  (p=0.000 n=91+91)

name                        old speed      new speed       delta
EncodeGray-4                 142MB/s ± 1%    143MB/s ± 1%    +0.26%  (p=0.000 n=86+85)
EncodeGrayWithBufferPool-4   154MB/s ± 0%    156MB/s ± 0%    +0.73%  (p=0.000 n=97+92)
EncodeNRGBOpaque-4           189MB/s ± 1%    190MB/s ± 1%    +0.44%  (p=0.000 n=90+86)
EncodeNRGBA-4                168MB/s ± 1%    169MB/s ± 0%    +0.69%  (p=0.000 n=89+87)
EncodePaletted-4            60.3MB/s ± 1%  134.2MB/s ± 0%  +122.74%  (p=0.000 n=90+85)
EncodeRGBOpaque-4            189MB/s ± 1%    189MB/s ± 0%      ~     (p=0.326 n=94+88)
EncodeRGBA-4                50.6MB/s ± 2%   51.1MB/s ± 1%    +0.87%  (p=0.000 n=91+91)

name                        old alloc/op   new alloc/op    delta
EncodeGray-4                   852kB ± 0%      852kB ± 0%    +0.00%  (p=0.000 n=100+100)
EncodeGrayWithBufferPool-4    1.49kB ± 2%     1.47kB ± 1%    -0.88%  (p=0.000 n=95+90)
EncodeNRGBOpaque-4             860kB ± 0%      860kB ± 0%    +0.00%  (p=0.003 n=98+58)
EncodeNRGBA-4                  864kB ± 0%      864kB ± 0%    +0.00%  (p=0.021 n=100+99)
EncodePaletted-4               849kB ± 0%      849kB ± 0%    +0.00%  (p=0.040 n=100+100)
EncodeRGBOpaque-4              860kB ± 0%      860kB ± 0%      ~     (p=0.062 n=66+98)
EncodeRGBA-4                  3.32MB ± 0%     3.32MB ± 0%    -0.00%  (p=0.044 n=99+99)

name                        old allocs/op  new allocs/op   delta
EncodeGray-4                    32.0 ± 0%       32.0 ± 0%      ~     (all equal)
EncodeGrayWithBufferPool-4      3.00 ± 0%       3.00 ± 0%      ~     (all equal)
EncodeNRGBOpaque-4              32.0 ± 0%       32.0 ± 0%      ~     (all equal)
EncodeNRGBA-4                   32.0 ± 0%       32.0 ± 0%      ~     (all equal)
EncodePaletted-4                36.0 ± 0%       36.0 ± 0%      ~     (all equal)
EncodeRGBOpaque-4               32.0 ± 0%       32.0 ± 0%      ~     (all equal)
EncodeRGBA-4                    614k ± 0%       614k ± 0%      ~     (all equal)

Change-Id: I716bde2dc80d8111d75d3d765fc09223f770d5a5
Reviewed-on: https://go-review.googlesource.com/c/go/+/187417Reviewed-by: default avatarEmmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: default avatarNigel Tao <nigeltao@golang.org>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 7be97af2
......@@ -426,10 +426,11 @@ func (e *encoder) writeImage(w io.Writer, m image.Image, cb int, level int) erro
var a uint8
var c int
pixelsPerByte := 8 / bitsPerPixel
for x := b.Min.X; x < b.Max.X; x++ {
a = a<<uint(bitsPerPixel) | pi.ColorIndexAt(x, y)
c++
if c == 8/bitsPerPixel {
if c == pixelsPerByte {
cr[0][i] = a
i += 1
a = 0
......@@ -437,7 +438,7 @@ func (e *encoder) writeImage(w io.Writer, m image.Image, cb int, level int) erro
}
}
if c != 0 {
for c != 8/bitsPerPixel {
for c != pixelsPerByte {
a = a << uint(bitsPerPixel)
c++
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment