Add std/png mimic benches

Wuffs' std/png and std/zlib implementations have not been optimized yet,
but even at this early stage, they are roughly on par with their
"mimics": equivalent production-quality C libraries (libpng and zlib).

On a mid-range x86_64 laptop:

wuffs_png_decode_19k_8bpp/clang9     95.8MB/s ± 0%
wuffs_png_decode_40k_24bpp/clang9     100MB/s ± 1%
wuffs_png_decode_77k_8bpp/clang9      359MB/s ± 0%
wuffs_png_decode_552k_32bpp/clang9    119MB/s ± 0%
wuffs_png_decode_4002k_24bpp/clang9   101MB/s ± 0%

wuffs_png_decode_19k_8bpp/gcc10      97.2MB/s ± 0%
wuffs_png_decode_40k_24bpp/gcc10      111MB/s ± 0%
wuffs_png_decode_77k_8bpp/gcc10       316MB/s ± 0%
wuffs_png_decode_552k_32bpp/gcc10     132MB/s ± 0%
wuffs_png_decode_4002k_24bpp/gcc10    116MB/s ± 0%

mimic_png_decode_19k_8bpp            58.1MB/s ± 0%
mimic_png_decode_40k_24bpp           73.3MB/s ± 0%
mimic_png_decode_77k_8bpp             177MB/s ± 0%
mimic_png_decode_552k_32bpp           147MB/s ± 0%
mimic_png_decode_4002k_24bpp          104MB/s ± 0%

wuffs_zlib_decode_10k/clang9   126MB/s ± 0%
wuffs_zlib_decode_100k/clang9  136MB/s ± 0%

wuffs_zlib_decode_10k/gcc10    131MB/s ± 0%
wuffs_zlib_decode_100k/gcc10   159MB/s ± 0%

mimic_zlib_decode_10k          133MB/s ± 0%
mimic_zlib_decode_100k         160MB/s ± 0%
This commit is contained in:
Nigel Tao committed 2020-12-28 15:52:46 +11:00
1 parent 5ad6238f34
commit 88616cc969
3 files changed
+330 -12

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
// Copyright 2020 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "png.h"
const char* //
mimic_png_decode(uint64_t* n_bytes_out,
wuffs_base__io_buffer* dst,
uint32_t wuffs_initialize_flags,
wuffs_base__pixel_format pixfmt,
wuffs_base__io_buffer* src) {
static uint8_t dst_fallback_array_u8[64 * 1024 * 1024];
wuffs_base__io_buffer dst_fallback = ((wuffs_base__io_buffer){
.data = ((wuffs_base__slice_u8){
.ptr = dst_fallback_array_u8,
.len = 64 * 1024 * 1024,
}),
});
if (!dst) {
dst = &dst_fallback;
}
const char* ret = NULL;
png_image pi;
memset(&pi, 0, (sizeof pi));
pi.version = PNG_IMAGE_VERSION;
if (!png_image_begin_read_from_memory(&pi, src->data.ptr + src->meta.ri,
src->meta.wi - src->meta.ri)) {
ret = "mimic_png_decode: png_image_begin_read_from_memory failed";
goto cleanup0;
}
switch (pixfmt.repr) {
case WUFFS_BASE__PIXEL_FORMAT__Y:
pi.format = PNG_FORMAT_GRAY;
break;
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
pi.format = PNG_FORMAT_BGRA;
break;
default:
ret = "mimic_png_decode: unsupported pixfmt";
goto cleanup0;
}
size_t n = PNG_IMAGE_SIZE(pi);
if (n > wuffs_base__io_buffer__writer_length(dst)) {
ret = "mimic_png_decode: image is too large";
goto cleanup0;
}
if (!png_image_finish_read(
&pi, NULL, wuffs_base__io_buffer__writer_pointer(dst), 0, NULL)) {
ret = "mimic_png_decode: png_image_finish_read failed";
goto cleanup0;
}
dst->meta.wi += n;
if (n_bytes_out) {
*n_bytes_out += n;
}
cleanup0:;
png_image_free(&pi);
return ret;
}
+161 -12
View File
@@ -37,7 +37,7 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
"wuffs mimic cflags" to run the mimic benchmarks.
*/
// !! wuffs mimic cflags: -DWUFFS_MIMIC
// !! wuffs mimic cflags: -DWUFFS_MIMIC -lpng
// Wuffs ships as a "single file C library" or "header file library" as per
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
@@ -68,7 +68,7 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
#include "../../../release/c/wuffs-unsupported-snapshot.c"
#include "../testlib/testlib.c"
#ifdef WUFFS_MIMIC
// No mimic library.
#include "../mimiclib/png.c"
#endif
// ---------------- PNG Tests
@@ -133,35 +133,173 @@ test_wuffs_png_decode_frame_config() {
#ifdef WUFFS_MIMIC
// No mimic tests.
const char* //
do_test_mimic_png_decode(const char* filename) {
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
.data = g_src_slice_u8,
});
CHECK_STRING(read_file(&src, filename));
src.meta.ri = 0;
wuffs_base__io_buffer have = ((wuffs_base__io_buffer){
.data = g_have_slice_u8,
});
CHECK_STRING(wuffs_png_decode(
NULL, &have, WUFFS_INITIALIZE__DEFAULT_OPTIONS,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
&src));
src.meta.ri = 0;
wuffs_base__io_buffer want = ((wuffs_base__io_buffer){
.data = g_want_slice_u8,
});
CHECK_STRING(mimic_png_decode(
NULL, &want, WUFFS_INITIALIZE__DEFAULT_OPTIONS,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
&src));
return check_io_buffers_equal("", &have, &want);
}
const char* //
test_mimic_png_decode_19k_8bpp() {
CHECK_FOCUS(__func__);
// libpng automatically applies the "gAMA" chunk (with no matching "sRGB"
// chunk) but Wuffs does not. To make the comparison more like-for-like,
// especially in emitting identical BGRA pixels, patch the source file by
// replacing the "gAMA" with the nonsense "hAMA". ASCII 'g' is 0x67.
return do_test_mimic_png_decode("@25=67=68;test/data/bricks-gray.png");
}
const char* //
test_mimic_png_decode_40k_24bpp() {
CHECK_FOCUS(__func__);
return do_test_mimic_png_decode("test/data/hat.png");
}
const char* //
test_mimic_png_decode_77k_8bpp() {
CHECK_FOCUS(__func__);
return do_test_mimic_png_decode("test/data/bricks-dither.png");
}
const char* //
test_mimic_png_decode_552k_32bpp() {
CHECK_FOCUS(__func__);
return do_test_mimic_png_decode("test/data/hibiscus.primitive.png");
}
const char* //
test_mimic_png_decode_4002k_24bpp() {
CHECK_FOCUS(__func__);
return do_test_mimic_png_decode("test/data/harvesters.png");
}
#endif // WUFFS_MIMIC
// ---------------- PNG Benches
const char* //
bench_wuffs_png_decode_19k_y() {
bench_wuffs_png_decode_19k_8bpp() {
CHECK_FOCUS(__func__);
// libpng automatically applies the "gAMA" chunk (with no matching "sRGB"
// chunk) but Wuffs does not. To make the comparison more like-for-like,
// especially in emitting identical BGRA pixels, patch the source file by
// replacing the "gAMA" with the nonsense "hAMA". ASCII 'g' is 0x67.
return do_bench_image_decode(
&wuffs_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__Y),
"test/data/bricks-gray.png", 0, SIZE_MAX, 80);
"@25=67=68;test/data/bricks-gray.png", 0, SIZE_MAX, 50);
}
const char* //
bench_wuffs_png_decode_40k_rgb() {
bench_wuffs_png_decode_40k_24bpp() {
CHECK_FOCUS(__func__);
return do_bench_image_decode(
&wuffs_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
"test/data/hat.png", 0, SIZE_MAX, 50);
"test/data/hat.png", 0, SIZE_MAX, 30);
}
const char* //
bench_wuffs_png_decode_77k_8bpp() {
CHECK_FOCUS(__func__);
return do_bench_image_decode(
&wuffs_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
"test/data/bricks-dither.png", 0, SIZE_MAX, 50);
}
const char* //
bench_wuffs_png_decode_552k_32bpp() {
CHECK_FOCUS(__func__);
return do_bench_image_decode(
&wuffs_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
"test/data/hibiscus.primitive.png", 0, SIZE_MAX, 4);
}
const char* //
bench_wuffs_png_decode_4002k_24bpp() {
CHECK_FOCUS(__func__);
return do_bench_image_decode(
&wuffs_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
"test/data/harvesters.png", 0, SIZE_MAX, 1);
}
// ---------------- Mimic Benches
#ifdef WUFFS_MIMIC
// No mimic benches.
const char* //
bench_mimic_png_decode_19k_8bpp() {
CHECK_FOCUS(__func__);
// libpng automatically applies the "gAMA" chunk (with no matching "sRGB"
// chunk) but Wuffs does not. To make the comparison more like-for-like,
// especially in emitting identical BGRA pixels, patch the source file by
// replacing the "gAMA" with the nonsense "hAMA". ASCII 'g' is 0x67.
return do_bench_image_decode(
&mimic_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__Y),
"@25=67=68;test/data/bricks-gray.png", 0, SIZE_MAX, 50);
}
const char* //
bench_mimic_png_decode_40k_24bpp() {
CHECK_FOCUS(__func__);
return do_bench_image_decode(
&mimic_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
"test/data/hat.png", 0, SIZE_MAX, 30);
}
const char* //
bench_mimic_png_decode_77k_8bpp() {
CHECK_FOCUS(__func__);
return do_bench_image_decode(
&mimic_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
"test/data/bricks-dither.png", 0, SIZE_MAX, 50);
}
const char* //
bench_mimic_png_decode_552k_32bpp() {
CHECK_FOCUS(__func__);
return do_bench_image_decode(
&mimic_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
"test/data/hibiscus.primitive.png", 0, SIZE_MAX, 4);
}
const char* //
bench_mimic_png_decode_4002k_24bpp() {
CHECK_FOCUS(__func__);
return do_bench_image_decode(
&mimic_png_decode, WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED,
wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL),
"test/data/harvesters.png", 0, SIZE_MAX, 1);
}
#endif // WUFFS_MIMIC
@@ -174,7 +312,11 @@ proc g_tests[] = {
#ifdef WUFFS_MIMIC
// No mimic tests.
test_mimic_png_decode_19k_8bpp,
test_mimic_png_decode_40k_24bpp,
test_mimic_png_decode_77k_8bpp,
test_mimic_png_decode_552k_32bpp,
test_mimic_png_decode_4002k_24bpp,
#endif // WUFFS_MIMIC
@@ -183,12 +325,19 @@ proc g_tests[] = {
proc g_benches[] = {
bench_wuffs_png_decode_19k_y,
bench_wuffs_png_decode_40k_rgb,
bench_wuffs_png_decode_19k_8bpp,
bench_wuffs_png_decode_40k_24bpp,
bench_wuffs_png_decode_77k_8bpp,
bench_wuffs_png_decode_552k_32bpp,
bench_wuffs_png_decode_4002k_24bpp,
#ifdef WUFFS_MIMIC
// No mimic benches.
bench_mimic_png_decode_19k_8bpp,
bench_mimic_png_decode_40k_24bpp,
bench_mimic_png_decode_77k_8bpp,
bench_mimic_png_decode_552k_32bpp,
bench_mimic_png_decode_4002k_24bpp,
#endif // WUFFS_MIMIC
+93
View File
@@ -79,6 +79,20 @@ wuffs_testlib__initialize_global_xxx_slices() {
});
}
uint8_t //
unhex(uint8_t b) {
if (('0' <= b) && (b <= '9')) {
return b - '0';
}
if (('A' <= b) && (b <= 'F')) {
return b + 10 - 'A';
}
if (('a' <= b) && (b <= 'f')) {
return b + 10 - 'a';
}
return 0;
}
char g_fail_msg[65536] = {0};
#define RETURN_FAIL(...) \
@@ -568,6 +582,72 @@ copy_to_io_buffer_from_pixel_buffer(wuffs_base__io_buffer* dst,
return NULL;
}
bool //
skip_read_file_patches(const char** path) {
const char* p = *path;
while (*p == '@') {
p++;
while (*p != ';') {
if (*p == '\x00') {
return false;
}
p++;
}
p++;
}
*path = p;
return true;
}
bool //
apply_read_file_patches(wuffs_base__io_buffer* dst,
const char* original_path,
size_t original_wi) {
const char* p = original_path;
while (*p == '@') {
p++;
uint64_t offset = 0;
while (*p != '=') {
if (*p == '\x00') {
return false;
}
offset = (offset << 4) | unhex(*p);
p++;
}
p++;
uint8_t before = 0;
while (*p != '=') {
if (*p == '\x00') {
return false;
}
before = (before << 4) | unhex(*p);
p++;
}
p++;
uint8_t after = 0;
while (*p != ';') {
if (*p == '\x00') {
return false;
}
after = (after << 4) | unhex(*p);
p++;
}
p++;
size_t i = original_wi + offset;
if ((i >= dst->meta.wi) || (dst->data.ptr[i] != before)) {
return false;
}
dst->data.ptr[i] = after;
}
return true;
}
// read_file loads path into dst.
//
// The path may be preceded by zero or more "@123=45=67;" sub-strings, which
// denote a one-byte patch at offset 0x123, changing the byte at that offset
// from 0x45 to 0x67.
const char* //
read_file(wuffs_base__io_buffer* dst, const char* path) {
if (!dst || !path) {
@@ -576,6 +656,14 @@ read_file(wuffs_base__io_buffer* dst, const char* path) {
if (dst->meta.closed) {
RETURN_FAIL("read_file: dst buffer closed for writes");
}
const char* original_path = path;
size_t original_wi = dst->meta.wi;
if (!skip_read_file_patches(&path)) {
RETURN_FAIL("read_file(\"%s\"): invalid \"@123=45=67;\" patch",
original_path);
}
FILE* f = fopen(path, "r");
if (!f) {
RETURN_FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(errno),
@@ -618,6 +706,11 @@ read_file(wuffs_base__io_buffer* dst, const char* path) {
fclose(f);
dst->meta.pos = 0;
dst->meta.closed = true;
if (!apply_read_file_patches(dst, original_path, original_wi)) {
RETURN_FAIL("read_file(\"%s\"): invalid \"@123=45=67;\" patch",
original_path);
}
return NULL;
}