Have script/bench-*-png convert to BGRA

On a mid-range x86_64 laptop:

Compared to the previous commit:

go_decode_19k_8bpp                      39.3MB/s ± 1%  39.4MB/s ± 1%     ~     (p=0.548 n=5+5)
go_decode_40k_24bpp                     46.5MB/s ± 1%  46.7MB/s ± 1%     ~     (p=0.095 n=5+5)
go_decode_77k_8bpp                      78.3MB/s ± 0%  78.3MB/s ± 0%     ~     (p=0.286 n=4+5)
go_decode_552k_32bpp_verify_checksum     120MB/s ± 1%   118MB/s ± 0%     ~     (p=0.056 n=5+5)
go_decode_4002k_24bpp                   50.7MB/s ± 0%  50.5MB/s ± 0%   -0.58%  (p=0.016 n=5+4)

rust_decode_19k_8bpp                    88.3MB/s ± 0%  89.8MB/s ± 0%   +1.76%  (p=0.008 n=5+5)
rust_decode_40k_24bpp                    133MB/s ± 0%   122MB/s ± 0%   -8.58%  (p=0.008 n=5+5)
rust_decode_77k_8bpp                     180MB/s ± 0%   158MB/s ± 0%  -12.04%  (p=0.008 n=5+5)
rust_decode_552k_32bpp_verify_checksum   146MB/s ± 0%   136MB/s ± 0%   -6.59%  (p=0.016 n=4+5)
rust_decode_4002k_24bpp                  134MB/s ± 0%   122MB/s ± 0%   -9.02%  (p=0.008 n=5+5)

Compared to libpng (the C library):

libpng_decode_19k_8bpp                                 58.0MB/s ± 0%  1.00x
libpng_decode_40k_24bpp                                73.1MB/s ± 0%  1.00x
libpng_decode_77k_8bpp                                  177MB/s ± 0%  1.00x
libpng_decode_552k_32bpp_verify_checksum                146MB/s ± 0%  1.00x
libpng_decode_4002k_24bpp                               104MB/s ± 0%  1.00x

go_decode_19k_8bpp/go1.16                              39.4MB/s ± 1%  0.68x
go_decode_40k_24bpp/go1.16                             46.7MB/s ± 1%  0.64x
go_decode_77k_8bpp/go1.16                              78.3MB/s ± 0%  0.44x
go_decode_552k_32bpp_verify_checksum/go1.16             118MB/s ± 0%  0.81x
go_decode_4002k_24bpp/go1.16                           50.5MB/s ± 0%  0.49x

rust_decode_19k_8bpp/rust1.48                          89.8MB/s ± 0%  1.55x
rust_decode_40k_24bpp/rust1.48                          122MB/s ± 0%  1.67x
rust_decode_77k_8bpp/rust1.48                           158MB/s ± 0%  0.89x
rust_decode_552k_32bpp_verify_checksum/rust1.48         136MB/s ± 0%  0.93x
rust_decode_4002k_24bpp/rust1.48                        122MB/s ± 0%  1.17x
This commit is contained in:
Nigel Tao committed 2021-03-25 12:38:02 +11:00
1 parent 5dd7ede975
commit 7a73708a56
2 files changed
+54 -25

No files matched your search

+16 -9
View File
@@ -99,13 +99,13 @@ func main1() error {
start := time.Now() start := time.Now()
iters := uint64(tc.itersUnscaled) * iterscale iters := uint64(tc.itersUnscaled) * iterscale
bgra := strings.HasSuffix(tc.benchname, "_77k_8bpp") expandPalette := strings.HasSuffix(tc.benchname, "_77k_8bpp")
numBytes, err := decode(tc.src, bgra) numBytes, err := decode(tc.src, expandPalette)
if err != nil { if err != nil {
return err return err
} }
for j := uint64(1); j < iters; j++ { for j := uint64(1); j < iters; j++ {
decode(tc.src, bgra) decode(tc.src, expandPalette)
} }
elapsedNanos := time.Since(start) elapsedNanos := time.Since(start)
@@ -124,7 +124,7 @@ func main1() error {
return nil return nil
} }
func decode(src []byte, bgra bool) (numBytes uint64, retErr error) { func decode(src []byte, expandPalette bool) (numBytes uint64, retErr error) {
m, err := png.Decode(bytes.NewReader(src)) m, err := png.Decode(bytes.NewReader(src))
if err != nil { if err != nil {
return 0, err return 0, err
@@ -133,25 +133,32 @@ func decode(src []byte, bgra bool) (numBytes uint64, retErr error) {
b := m.Bounds() b := m.Bounds()
n := uint64(b.Dx()) * uint64(b.Dy()) n := uint64(b.Dx()) * uint64(b.Dy())
// Go converts to RGBA (as that's what Go's image/draw standard library is if expandPalette {
// optimized for); Wuffs converts to BGRA. The difference isn't important,
// as we just want measure how long it takes.
if bgra {
dst := image.NewRGBA(b) dst := image.NewRGBA(b)
draw.Draw(dst, b, m, b.Min, draw.Src) draw.Draw(dst, b, m, b.Min, draw.Src)
m = dst m = dst
} }
switch m.(type) { pix := []byte(nil)
switch m := m.(type) {
case *image.Gray: case *image.Gray:
n *= 1 n *= 1
case *image.NRGBA: case *image.NRGBA:
n *= 4 n *= 4
pix = m.Pix
case *image.RGBA: case *image.RGBA:
n *= 4 n *= 4
pix = m.Pix
default: default:
return 0, fmt.Errorf("unexpected image type %T", m) return 0, fmt.Errorf("unexpected image type %T", m)
} }
// Convert RGBA => BGRA.
if pix != nil {
for i, iEnd := 0, len(pix)/4; i < iEnd; i += 4 {
pix[(4*i)+0], pix[(4*i)+2] = pix[(4*i)+2], pix[(4*i)+0]
}
}
return n, nil return n, nil
} }
+38 -16
View File
@@ -46,13 +46,15 @@ fn main() {
print!("# https://godoc.org/golang.org/x/perf/cmd/benchstat tool. To install it, first\n"); print!("# https://godoc.org/golang.org/x/perf/cmd/benchstat tool. To install it, first\n");
print!("# install Go, then run \"go get golang.org/x/perf/cmd/benchstat\".\n"); print!("# install Go, then run \"go get golang.org/x/perf/cmd/benchstat\".\n");
let mut dst = vec![0u8; 64 * 1024 * 1024]; let mut dst0 = vec![0u8; 64 * 1024 * 1024];
let mut dst1 = vec![0u8; 64 * 1024 * 1024];
// The various magic constants below are copied from test/c/std/png.c // The various magic constants below are copied from test/c/std/png.c
for i in 0..(1 + REPS) { for i in 0..(1 + REPS) {
bench( bench(
"19k_8bpp", "19k_8bpp",
&mut dst[..], &mut dst0[..],
&mut dst1[..],
include_bytes!("../../../test/data/bricks-gray.no-ancillary.png"), include_bytes!("../../../test/data/bricks-gray.no-ancillary.png"),
i == 0, // warm_up i == 0, // warm_up
160 * 120 * 1, // want_num_bytes = 19_200 160 * 120 * 1, // want_num_bytes = 19_200
@@ -61,7 +63,8 @@ fn main() {
bench( bench(
"40k_24bpp", "40k_24bpp",
&mut dst[..], &mut dst0[..],
&mut dst1[..],
include_bytes!("../../../test/data/hat.png"), include_bytes!("../../../test/data/hat.png"),
i == 0, // warm_up i == 0, // warm_up
90 * 112 * 4, // want_num_bytes = 40_320 90 * 112 * 4, // want_num_bytes = 40_320
@@ -70,7 +73,8 @@ fn main() {
bench( bench(
"77k_8bpp", "77k_8bpp",
&mut dst[..], &mut dst0[..],
&mut dst1[..],
include_bytes!("../../../test/data/bricks-dither.png"), include_bytes!("../../../test/data/bricks-dither.png"),
i == 0, // warm_up i == 0, // warm_up
160 * 120 * 4, // want_num_bytes = 76_800 160 * 120 * 4, // want_num_bytes = 76_800
@@ -79,7 +83,8 @@ fn main() {
bench( bench(
"552k_32bpp_verify_checksum", "552k_32bpp_verify_checksum",
&mut dst[..], &mut dst0[..],
&mut dst1[..],
include_bytes!("../../../test/data/hibiscus.primitive.png"), include_bytes!("../../../test/data/hibiscus.primitive.png"),
i == 0, // warm_up i == 0, // warm_up
312 * 442 * 4, // want_num_bytes = 551_616 312 * 442 * 4, // want_num_bytes = 551_616
@@ -88,7 +93,8 @@ fn main() {
bench( bench(
"4002k_24bpp", "4002k_24bpp",
&mut dst[..], &mut dst0[..],
&mut dst1[..],
include_bytes!("../../../test/data/harvesters.png"), include_bytes!("../../../test/data/harvesters.png"),
i == 0, // warm_up i == 0, // warm_up
1165 * 859 * 4, // want_num_bytes = 4_002_940 1165 * 859 * 4, // want_num_bytes = 4_002_940
@@ -99,7 +105,8 @@ fn main() {
fn bench( fn bench(
name: &str, // Benchmark name. name: &str, // Benchmark name.
dst: &mut [u8], // Destination buffer. dst0: &mut [u8], // Destination buffer #0.
dst1: &mut [u8], // Destination buffer #1.
src: &[u8], // Source data. src: &[u8], // Source data.
warm_up: bool, // Whether this is a warm up rep. warm_up: bool, // Whether this is a warm up rep.
want_num_bytes: u64, // Expected num_bytes per iteration. want_num_bytes: u64, // Expected num_bytes per iteration.
@@ -110,7 +117,7 @@ fn bench(
let start = Instant::now(); let start = Instant::now();
for _ in 0..iters { for _ in 0..iters {
let n = decode(&mut dst[..], src); let n = decode(&mut dst0[..], &mut dst1[..], src);
if n != want_num_bytes { if n != want_num_bytes {
panic!("num_bytes: got {}, want {}", n, want_num_bytes); panic!("num_bytes: got {}, want {}", n, want_num_bytes);
} }
@@ -136,17 +143,32 @@ fn bench(
} }
// decode returns the number of bytes processed. // decode returns the number of bytes processed.
fn decode(dst: &mut [u8], src: &[u8]) -> u64 { fn decode(dst0: &mut [u8], dst1: &mut [u8], src: &[u8]) -> u64 {
let decoder = png::Decoder::new(src); let decoder = png::Decoder::new(src);
let (info, mut reader) = decoder.read_info().unwrap(); let (info, mut reader) = decoder.read_info().unwrap();
let num_bytes = info.buffer_size() as u64; let num_bytes = info.buffer_size() as u64;
reader.next_frame(dst).unwrap(); reader.next_frame(dst0).unwrap();
if info.color_type == png::ColorType::RGB { if info.color_type == png::ColorType::Grayscale {
// If the PNG image is RGB (not RGBA) then Rust's png crate will decode // No conversion necessary.
// to 3 bytes per pixel. Wuffs' std/png benchmarks decode to 4 bytes return num_bytes;
// per pixel (and in BGRA order, not RGB or RGBA). We'll hand-wave the } else if info.color_type == png::ColorType::RGB {
// difference away and say that we decoded 33% more pixels than we did. // Convert RGB => BGRA.
for i in 0..((num_bytes / 3) as usize) {
dst1[(4 * i) + 0] = dst0[(3 * i) + 2];
dst1[(4 * i) + 1] = dst0[(3 * i) + 1];
dst1[(4 * i) + 2] = dst0[(3 * i) + 0];
dst1[(4 * i) + 3] = 0xFF;
}
return (num_bytes / 3) * 4; return (num_bytes / 3) * 4;
} else if info.color_type == png::ColorType::RGBA {
// Convert RGBA => BGRA.
for i in 0..((num_bytes / 4) as usize) {
let d = dst0[(4 * i) + 0];
dst0[(4 * i) + 0] = dst0[(4 * i) + 2];
dst0[(4 * i) + 2] = d;
}
return num_bytes;
} }
num_bytes // Returning 0 should lead to a panic (when want_num_bytes != 0).
0
} }