blob: 5dc14c005f3e9b4e9744f3323ab503dc9678d7a2 [file] [log] [blame]
Nigel Taod2075ce2018-04-25 15:26:29 +10001// Copyright 2018 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Nigel Tao670b45b2019-02-02 08:10:08 +110015// ----------------
16
Nigel Taod2075ce2018-04-25 15:26:29 +100017// This file contains a hand-written C benchmark of different strategies for
18// decoding PNG data.
19//
20// For a PNG image with width W and height H, the H rows can be decompressed
21// one-at-a-time or all-at-once. Roughly speaking, this corresponds to H versus
22// 1 call into the zlib decoder. The former (call it "fragmented dst") requires
23// less scratch-space memory than the latter ("full dst"): 2 * bytes_per_row
24// instead of H * bytes_per row, but the latter can be faster.
25//
26// The zlib-compressed data can be split into multiple IDAT chunks. Similarly,
27// these chunks can be decompressed separately ("fragmented IDAT") or together
28// ("full IDAT"), again providing a memory vs speed trade-off.
29//
30// This program reports the speed of combining the independent frag/full dst
31// and frag/full IDAT techniques.
32//
Nigel Tao95d02ee2019-01-12 12:34:40 +110033// For example, with gcc 7.3 (and -O3) as of January 2019:
Nigel Taod2075ce2018-04-25 15:26:29 +100034//
35// On ../test/data/hat.png (90 × 112 pixels):
36// name time/op relative
Nigel Taoc6a48b32019-01-19 10:50:40 +110037// FragDstFragIDAT/gcc 289µs ± 1% 1.00x
38// FragDstFullIDAT/gcc 288µs ± 0% 1.00x
39// FullDstFragIDAT/gcc 149µs ± 1% 1.93x
40// FullDstFullIDAT/gcc 148µs ± 1% 1.95x
Nigel Taod2075ce2018-04-25 15:26:29 +100041//
Nigel Tao95d02ee2019-01-12 12:34:40 +110042// On ../test/data/hibiscus.regular.png (312 × 442 pixels):
Nigel Taod2075ce2018-04-25 15:26:29 +100043// name time/op relative
Nigel Taoc6a48b32019-01-19 10:50:40 +110044// FragDstFragIDAT/gcc 2.49ms ± 0% 1.00x
45// FragDstFullIDAT/gcc 2.49ms ± 0% 1.00x
46// FullDstFragIDAT/gcc 2.08ms ± 0% 1.20x
47// FullDstFullIDAT/gcc 2.02ms ± 1% 1.23x
Nigel Taod2075ce2018-04-25 15:26:29 +100048//
49// On ../test/data/harvesters.png (1165 × 859 pixels):
50// name time/op relative
Nigel Taoc6a48b32019-01-19 10:50:40 +110051// FragDstFragIDAT/gcc 15.6ms ± 2% 1.00x
52// FragDstFullIDAT/gcc 15.4ms ± 0% 1.01x
53// FullDstFragIDAT/gcc 14.4ms ± 0% 1.08x
54// FullDstFullIDAT/gcc 14.1ms ± 0% 1.10x
Nigel Taod2075ce2018-04-25 15:26:29 +100055
56#include <errno.h>
57#include <inttypes.h>
58#include <stdio.h>
59#include <string.h>
60#include <sys/time.h>
61#include <unistd.h>
62
Nigel Taoa0bafff2018-07-14 08:59:36 +100063// Wuffs ships as a "single file C library" or "header file library" as per
64// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
65//
66// To use that single file as a "foo.c"-like implementation, instead of a
67// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
68// compiling it.
69#define WUFFS_IMPLEMENTATION
70
Jimmy Casey3c50ada2018-07-26 17:12:19 +000071// If building this program in an environment that doesn't easily accommodate
Nigel Taod2075ce2018-04-25 15:26:29 +100072// relative includes, you can use the script/inline-c-relative-includes.go
73// program to generate a stand-alone C file.
Nigel Tao95d02ee2019-01-12 12:34:40 +110074#include "../release/c/wuffs-unsupported-snapshot.c"
Nigel Taod2075ce2018-04-25 15:26:29 +100075
76// The order matters here. Clang also defines "__GNUC__".
77#if defined(__clang__)
78const char* cc = "clang";
79const char* cc_version = __clang_version__;
80#elif defined(__GNUC__)
81const char* cc = "gcc";
82const char* cc_version = __VERSION__;
83#elif defined(_MSC_VER)
84const char* cc = "cl";
85const char* cc_version = "???";
86#else
87const char* cc = "cc";
88const char* cc_version = "???";
89#endif
90
Nigel Tao2914bae2020-02-26 09:40:30 +110091static inline uint32_t //
92load_u32be(uint8_t* p) {
Nigel Taod2075ce2018-04-25 15:26:29 +100093 return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |
94 ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);
95}
96
97// Limit the input PNG image (and therefore its IDAT data) to (64 MiB - 1 byte)
98// compressed, in up to 1024 IDAT chunks, and 256 MiB and 16384 × 16384 pixels
99// uncompressed. This is a limitation of this program (which uses the Wuffs
100// standard library), not a limitation of Wuffs per se.
101#define DST_BUFFER_SIZE (256 * 1024 * 1024)
102#define SRC_BUFFER_SIZE (64 * 1024 * 1024)
103#define MAX_DIMENSION (16384)
104#define MAX_IDAT_CHUNKS (1024)
105
106uint8_t dst_buffer[DST_BUFFER_SIZE] = {0};
107size_t dst_len = 0;
108uint8_t src_buffer[SRC_BUFFER_SIZE] = {0};
109size_t src_len = 0;
110uint8_t idat_buffer[SRC_BUFFER_SIZE] = {0};
111// The n'th IDAT chunk data (where n is a zero-based count) is in
112// idat_buffer[i:j], where i = idat_splits[n+0] and j = idat_splits[n+1].
113size_t idat_splits[MAX_IDAT_CHUNKS + 1] = {0};
114uint32_t num_idat_chunks = 0;
115
Nigel Tao53760ce2019-02-16 14:18:25 +1100116#define WORK_BUFFER_SIZE WUFFS_ZLIB__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
117#if WORK_BUFFER_SIZE > 0
118uint8_t work_buffer[WORK_BUFFER_SIZE];
119#else
120// Not all C/C++ compilers support 0-length arrays.
121uint8_t work_buffer[1];
122#endif
123
Nigel Taod2075ce2018-04-25 15:26:29 +1000124uint32_t width = 0;
125uint32_t height = 0;
126uint64_t bytes_per_pixel = 0;
127uint64_t bytes_per_row = 0;
128uint64_t bytes_per_frame = 0;
129
Nigel Tao2914bae2020-02-26 09:40:30 +1100130const char* //
131read_stdin() {
Nigel Taod2075ce2018-04-25 15:26:29 +1000132 while (src_len < SRC_BUFFER_SIZE) {
133 const int stdin_fd = 0;
134 ssize_t n = read(stdin_fd, src_buffer + src_len, SRC_BUFFER_SIZE - src_len);
135 if (n > 0) {
136 src_len += n;
137 } else if (n == 0) {
138 return NULL;
139 } else if (errno == EINTR) {
140 // No-op.
141 } else {
142 return strerror(errno);
143 }
144 }
145 return "input is too large";
146}
147
Nigel Tao2914bae2020-02-26 09:40:30 +1100148const char* //
149process_png_chunks(uint8_t* p, size_t n) {
Nigel Taod2075ce2018-04-25 15:26:29 +1000150 while (n > 0) {
151 // Process the 8 byte chunk header.
152 if (n < 8) {
153 return "invalid PNG chunk";
154 }
155 uint32_t chunk_len = load_u32be(p + 0);
156 uint32_t chunk_type = load_u32be(p + 4);
157 p += 8;
158 n -= 8;
159
160 // Process the chunk payload.
161 if (n < chunk_len) {
162 return "short PNG chunk data";
163 }
164 switch (chunk_type) {
165 case 0x49484452: // "IHDR"
166 if (chunk_len != 13) {
167 return "invalid PNG IDAT chunk";
168 }
169 width = load_u32be(p + 0);
170 height = load_u32be(p + 4);
171 if ((width == 0) || (height == 0)) {
172 return "image dimensions are too small";
173 }
174 if ((width > MAX_DIMENSION) || (height > MAX_DIMENSION)) {
175 return "image dimensions are too large";
176 }
177 if (p[8] != 8) {
178 return "unsupported PNG bit depth";
179 }
180 if (bytes_per_pixel != 0) {
181 return "duplicate PNG IHDR chunk";
182 }
183 // Process the color type, as per the PNG spec table 11.1.
184 switch (p[9]) {
185 case 0:
186 bytes_per_pixel = 1;
187 break;
188 case 2:
189 bytes_per_pixel = 3;
190 break;
191 case 3:
192 bytes_per_pixel = 1;
193 break;
194 case 4:
195 bytes_per_pixel = 2;
196 break;
197 case 6:
198 bytes_per_pixel = 4;
199 break;
200 default:
201 return "unsupported PNG color type";
202 }
203 if (p[12] != 0) {
204 return "unsupported PNG interlacing";
205 }
206 break;
207
208 case 0x49444154: // "IDAT"
209 if (num_idat_chunks == MAX_IDAT_CHUNKS - 1) {
210 return "too many IDAT chunks";
211 }
212 memcpy(idat_buffer + idat_splits[num_idat_chunks], p, chunk_len);
213 idat_splits[num_idat_chunks + 1] =
214 idat_splits[num_idat_chunks] + chunk_len;
215 num_idat_chunks++;
216 break;
217 }
218 p += chunk_len;
219 n -= chunk_len;
220
221 // Process (and ignore) the 4 byte chunk footer (a checksum).
222 if (n < 4) {
223 return "invalid PNG chunk";
224 }
225 p += 4;
226 n -= 4;
227 }
228 return NULL;
229}
230
Nigel Tao2914bae2020-02-26 09:40:30 +1100231const char* //
232decode_once(bool frag_dst, bool frag_idat) {
Nigel Tao53760ce2019-02-16 14:18:25 +1100233 wuffs_zlib__decoder dec;
Nigel Tao5f8d2da2020-01-10 22:06:50 +1100234 wuffs_base__status status =
Nigel Tao53760ce2019-02-16 14:18:25 +1100235 wuffs_zlib__decoder__initialize(&dec, sizeof dec, WUFFS_VERSION, 0);
Nigel Tao5f8d2da2020-01-10 22:06:50 +1100236 if (!wuffs_base__status__is_ok(&status)) {
237 return wuffs_base__status__message(&status);
Nigel Tao95d02ee2019-01-12 12:34:40 +1100238 }
Nigel Taod2075ce2018-04-25 15:26:29 +1000239
Nigel Tao95d02ee2019-01-12 12:34:40 +1100240 wuffs_base__io_buffer dst = ((wuffs_base__io_buffer){
241 .data = ((wuffs_base__slice_u8){
242 .ptr = dst_buffer,
243 .len = bytes_per_frame,
244 }),
245 });
246 wuffs_base__io_buffer idat = ((wuffs_base__io_buffer){
247 .data = ((wuffs_base__slice_u8){
248 .ptr = idat_buffer,
249 .len = SRC_BUFFER_SIZE,
250 }),
251 .meta = ((wuffs_base__io_buffer_meta){
252 .wi = idat_splits[num_idat_chunks],
253 .ri = 0,
254 .pos = 0,
255 .closed = true,
256 }),
257 });
Nigel Taod2075ce2018-04-25 15:26:29 +1000258
259 uint32_t i = 0; // Number of dst fragments processed, if frag_dst.
260 if (frag_dst) {
Nigel Tao95d02ee2019-01-12 12:34:40 +1100261 dst.data.len = bytes_per_row;
Nigel Taod2075ce2018-04-25 15:26:29 +1000262 }
263
264 uint32_t j = 0; // Number of IDAT fragments processed, if frag_idat.
265 if (frag_idat) {
Nigel Tao95d02ee2019-01-12 12:34:40 +1100266 idat.meta.wi = idat_splits[1];
267 idat.meta.closed = (num_idat_chunks == 1);
Nigel Taod2075ce2018-04-25 15:26:29 +1000268 }
269
270 while (true) {
Nigel Tao5f8d2da2020-01-10 22:06:50 +1100271 status = wuffs_zlib__decoder__transform_io(&dec, &dst, &idat,
272 ((wuffs_base__slice_u8){
273 .ptr = work_buffer,
274 .len = WORK_BUFFER_SIZE,
275 }));
Nigel Taod2075ce2018-04-25 15:26:29 +1000276
Nigel Tao5f8d2da2020-01-10 22:06:50 +1100277 if (wuffs_base__status__is_ok(&status)) {
Nigel Taod2075ce2018-04-25 15:26:29 +1000278 break;
279 }
Nigel Tao5f8d2da2020-01-10 22:06:50 +1100280 if ((status.repr == wuffs_base__suspension__short_write) && frag_dst &&
Nigel Taod2075ce2018-04-25 15:26:29 +1000281 (i < height - 1)) {
282 i++;
Nigel Tao95d02ee2019-01-12 12:34:40 +1100283 dst.data.len = bytes_per_row * (i + 1);
Nigel Taod2075ce2018-04-25 15:26:29 +1000284 continue;
285 }
Nigel Tao5f8d2da2020-01-10 22:06:50 +1100286 if ((status.repr == wuffs_base__suspension__short_read) && frag_idat &&
Nigel Taod2075ce2018-04-25 15:26:29 +1000287 (j < num_idat_chunks - 1)) {
288 j++;
Nigel Tao95d02ee2019-01-12 12:34:40 +1100289 idat.meta.wi = idat_splits[j + 1];
290 idat.meta.closed = (num_idat_chunks == j + 1);
Nigel Taod2075ce2018-04-25 15:26:29 +1000291 continue;
292 }
Nigel Tao5f8d2da2020-01-10 22:06:50 +1100293 return wuffs_base__status__message(&status);
Nigel Taod2075ce2018-04-25 15:26:29 +1000294 }
295
Nigel Tao95d02ee2019-01-12 12:34:40 +1100296 if (dst.meta.wi != bytes_per_frame) {
Nigel Taod2075ce2018-04-25 15:26:29 +1000297 return "unexpected number of bytes decoded";
298 }
299 return NULL;
300}
301
Nigel Tao2914bae2020-02-26 09:40:30 +1100302const char* //
303decode(bool frag_dst, bool frag_idat) {
Nigel Taod2075ce2018-04-25 15:26:29 +1000304 int reps;
305 if (bytes_per_frame < 100000) {
306 reps = 1000;
307 } else if (bytes_per_frame < 1000000) {
308 reps = 100;
309 } else if (bytes_per_frame < 10000000) {
310 reps = 10;
311 } else {
312 reps = 1;
313 }
314
315 struct timeval bench_start_tv;
316 gettimeofday(&bench_start_tv, NULL);
317
318 int i;
319 for (i = 0; i < reps; i++) {
320 const char* msg = decode_once(frag_dst, frag_idat);
321 if (msg) {
322 return msg;
323 }
324 }
325
326 struct timeval bench_finish_tv;
327 gettimeofday(&bench_finish_tv, NULL);
328 int64_t micros =
329 (int64_t)(bench_finish_tv.tv_sec - bench_start_tv.tv_sec) * 1000000 +
330 (int64_t)(bench_finish_tv.tv_usec - bench_start_tv.tv_usec);
331 uint64_t nanos = 1;
332 if (micros > 0) {
333 nanos = (uint64_t)(micros)*1000;
334 }
335
336 printf("Benchmark%sDst%sIDAT/%s\t%8d\t%8" PRIu64 " ns/op\n",
337 frag_dst ? "Frag" : "Full", //
338 frag_idat ? "Frag" : "Full", //
339 cc, reps, nanos / reps);
340
341 return NULL;
342}
343
Nigel Tao2914bae2020-02-26 09:40:30 +1100344int //
345fail(const char* msg) {
Nigel Taod2075ce2018-04-25 15:26:29 +1000346 const int stderr_fd = 2;
347 write(stderr_fd, msg, strnlen(msg, 4095));
348 write(stderr_fd, "\n", 1);
349 return 1;
350}
351
Nigel Tao2914bae2020-02-26 09:40:30 +1100352int //
353main(int argc, char** argv) {
Nigel Taod2075ce2018-04-25 15:26:29 +1000354 const char* msg = read_stdin();
355 if (msg) {
356 return fail(msg);
357 }
Nigel Tao95d02ee2019-01-12 12:34:40 +1100358 if ((src_len < 8) ||
359 strncmp((const char*)(src_buffer), "\x89PNG\x0D\x0A\x1A\x0A", 8)) {
Nigel Taod2075ce2018-04-25 15:26:29 +1000360 return fail("invalid PNG");
361 }
362 msg = process_png_chunks(src_buffer + 8, src_len - 8);
363 if (msg) {
364 return fail(msg);
365 }
366 if (bytes_per_pixel == 0) {
367 return fail("missing PNG IHDR chunk");
368 }
369 if (num_idat_chunks == 0) {
370 return fail("missing PNG IDAT chunk");
371 }
372 // The +1 here is for the per-row filter byte.
373 bytes_per_row = (uint64_t)width * bytes_per_pixel + 1;
374 bytes_per_frame = (uint64_t)height * bytes_per_row;
375 if (bytes_per_frame > DST_BUFFER_SIZE) {
376 return fail("decompressed data is too large");
377 }
378
379 printf("# %s version %s\n#\n", cc, cc_version);
380 printf(
381 "# The output format, including the \"Benchmark\" prefixes, is "
382 "compatible with the\n"
383 "# https://godoc.org/golang.org/x/perf/cmd/benchstat tool. To install "
384 "it, first\n"
385 "# install Go, then run \"go get golang.org/x/perf/cmd/benchstat\".\n");
386
387 int i;
388 for (i = 0; i < 5; i++) {
389 msg = decode(true, true);
390 if (msg) {
391 return fail(msg);
392 }
393 msg = decode(true, false);
394 if (msg) {
395 return fail(msg);
396 }
397 msg = decode(false, true);
398 if (msg) {
399 return fail(msg);
400 }
401 msg = decode(false, false);
402 if (msg) {
403 return fail(msg);
404 }
405 }
406
407 return 0;
408}