From 010df42d75d08dcb55bdc06f285fc3a0038f2edc Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 22 Dec 2018 14:05:34 +1100 Subject: [PATCH] Have std/lzw always rewind excess bits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I'm not sure of the underlying reason for the lzw/gcc benchmark anomaly (the gif/gcc numbers don't change), but it's the same mystery as commit ecb3995 `Tweak how std/lzw yields "short read"`. name old speed new speed delta wuffs_lzw_decode_20k/clang 223MB/s ± 0% 214MB/s ± 0% -3.96% (p=0.008 n=5+5) wuffs_lzw_decode_100k/clang 385MB/s ± 0% 369MB/s ± 0% -4.15% (p=0.008 n=5+5) wuffs_lzw_decode_20k/gcc 186MB/s ± 1% 232MB/s ± 1% +24.41% (p=0.008 n=5+5) wuffs_lzw_decode_100k/gcc 224MB/s ± 1% 384MB/s ± 1% +71.81% (p=0.008 n=5+5) wuffs_gif_decode_1k_bw/clang 238MB/s ± 1% 236MB/s ± 0% ~ (p=0.222 n=5+5) wuffs_gif_decode_1k_color/clang 124MB/s ± 0% 118MB/s ± 1% -4.75% (p=0.008 n=5+5) wuffs_gif_decode_10k_bgra/clang 596MB/s ± 0% 562MB/s ± 0% -5.71% (p=0.008 n=5+5) wuffs_gif_decode_10k_indexed/clang 159MB/s ± 0% 149MB/s ± 0% -6.42% (p=0.008 n=5+5) wuffs_gif_decode_100k_artificial/clang 492MB/s ± 1% 487MB/s ± 0% -0.98% (p=0.008 n=5+5) wuffs_gif_decode_100k_realistic/clang 192MB/s ± 0% 183MB/s ± 0% -5.07% (p=0.008 n=5+5) wuffs_gif_decode_1000k/clang 195MB/s ± 0% 185MB/s ± 0% -4.93% (p=0.008 n=5+5) wuffs_gif_decode_anim_screencap/clang 1.01GB/s ± 1% 1.02GB/s ± 0% +1.25% (p=0.008 n=5+5) wuffs_gif_decode_1k_bw/gcc 247MB/s ± 0% 250MB/s ± 0% +1.26% (p=0.008 n=5+5) wuffs_gif_decode_1k_color/gcc 131MB/s ± 2% 133MB/s ± 0% ~ (p=0.151 n=5+5) wuffs_gif_decode_10k_bgra/gcc 541MB/s ± 1% 543MB/s ± 1% ~ (p=0.548 n=5+5) wuffs_gif_decode_10k_indexed/gcc 167MB/s ± 0% 167MB/s ± 1% ~ (p=0.730 n=4+5) wuffs_gif_decode_100k_artificial/gcc 469MB/s ± 0% 465MB/s ± 1% -0.78% (p=0.016 n=4+5) wuffs_gif_decode_100k_realistic/gcc 190MB/s ± 0% 190MB/s ± 0% -0.34% (p=0.032 n=4+5) wuffs_gif_decode_1000k/gcc 193MB/s ± 1% 192MB/s ± 1% ~ (p=0.556 n=4+5) wuffs_gif_decode_anim_screencap/gcc 995MB/s ± 1% 995MB/s ± 1% ~ (p=0.841 n=5+5) --- release/c/wuffs-unsupported-snapshot.h | 19 +++---- std/lzw/decode_lzw.wuffs | 19 +++---- test/c/std/lzw.c | 76 ++++++++++++++++++++------ 3 files changed, 78 insertions(+), 36 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 401b4c27..e1907eb6 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -7358,17 +7358,16 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, } } label_1_break:; - if (v_j > 0) { - while (v_n_bits >= 8) { - v_n_bits -= 8; - if (iop_a_src > io0_a_src) { - (iop_a_src--, wuffs_base__return_empty_struct()); - } else { - status = wuffs_lzw__error__internal_error_inconsistent_i_o; - goto exit; - } + while (v_n_bits >= 8) { + v_n_bits -= 8; + if (iop_a_src > io0_a_src) { + (iop_a_src--, wuffs_base__return_empty_struct()); + } else { + status = wuffs_lzw__error__internal_error_inconsistent_i_o; + goto exit; } - v_bits = ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_n_bits)); + } + if (v_j > 0) { self->private_impl.f_flush_j = v_j; WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); status = wuffs_lzw__decoder__auto_flush(self, a_dst); diff --git a/std/lzw/decode_lzw.wuffs b/std/lzw/decode_lzw.wuffs index 9fba9e7a..3ff7e5a5 100644 --- a/std/lzw/decode_lzw.wuffs +++ b/std/lzw/decode_lzw.wuffs @@ -233,18 +233,17 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) { } } - if j > 0 { - // Rewind args.src, if we've read too many bits. - while n_bits >= 8 { - n_bits -= 8 - if args.src.can_undo_byte() { - args.src.undo_byte!() - } else { - return "?internal error: inconsistent I/O" - } + // Rewind args.src, if we've read too many bits. + while n_bits >= 8 { + n_bits -= 8 + if args.src.can_undo_byte() { + args.src.undo_byte!() + } else { + return "?internal error: inconsistent I/O" } - bits = bits.low_bits(n:n_bits) + } + if j > 0 { this.flush_j = j this.auto_flush!??(dst:args.dst) } diff --git a/test/c/std/lzw.c b/test/c/std/lzw.c index a6f0904a..cdcf3b9d 100644 --- a/test/c/std/lzw.c +++ b/test/c/std/lzw.c @@ -163,20 +163,6 @@ const char* do_test_wuffs_lzw_decode(const char* src_filename, return check_io_buffers_equal("", &got, &want); } -const char* test_wuffs_lzw_decode_many_big_reads() { - CHECK_FOCUS(__func__); - return do_test_wuffs_lzw_decode("test/data/bricks-gray.indexes.giflzw", 14731, - "test/data/bricks-gray.indexes", 19200, 0, - 4096); -} - -const char* test_wuffs_lzw_decode_many_small_writes_reads() { - CHECK_FOCUS(__func__); - return do_test_wuffs_lzw_decode("test/data/bricks-gray.indexes.giflzw", 14731, - "test/data/bricks-gray.indexes", 19200, 41, - 43); -} - const char* test_wuffs_lzw_decode_bricks_dither() { CHECK_FOCUS(__func__); return do_test_wuffs_lzw_decode("test/data/bricks-dither.indexes.giflzw", @@ -191,12 +177,69 @@ const char* test_wuffs_lzw_decode_bricks_nodither() { 19200, 0, 0); } +const char* test_wuffs_lzw_decode_many_big_reads() { + CHECK_FOCUS(__func__); + return do_test_wuffs_lzw_decode("test/data/bricks-gray.indexes.giflzw", 14731, + "test/data/bricks-gray.indexes", 19200, 0, + 4096); +} + +const char* test_wuffs_lzw_decode_many_small_writes_reads() { + CHECK_FOCUS(__func__); + return do_test_wuffs_lzw_decode("test/data/bricks-gray.indexes.giflzw", 14731, + "test/data/bricks-gray.indexes", 19200, 41, + 43); +} + const char* test_wuffs_lzw_decode_pi() { CHECK_FOCUS(__func__); return do_test_wuffs_lzw_decode("test/data/pi.txt.giflzw", 50550, "test/data/pi.txt", 100003, 0, 0); } +const char* test_wuffs_lzw_decode_output_empty() { + CHECK_FOCUS(__func__); + + wuffs_base__io_buffer got = ((wuffs_base__io_buffer){ + .data = global_got_slice, + }); + wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ + .data = global_src_slice, + }); + + // Set up src to be 20 bytes long, starting with the 9-bit end code 0x101. + // Decoding should consume only 2 of those bytes. + src.meta.wi = 20; + int i; + for (i = 0; i < src.meta.wi; i++) { + src.data.ptr[i] = 0; + } + src.data.ptr[0] = 0x01; + src.data.ptr[1] = 0x01; + + wuffs_lzw__decoder dec = ((wuffs_lzw__decoder){}); + const char* z = + wuffs_lzw__decoder__check_wuffs_version(&dec, sizeof dec, WUFFS_VERSION); + if (z) { + RETURN_FAIL("check_wuffs_version: \"%s\"", z); + } + wuffs_lzw__decoder__set_literal_width(&dec, 8); + + z = wuffs_lzw__decoder__decode(&dec, wuffs_base__io_buffer__writer(&got), + wuffs_base__io_buffer__reader(&src)); + if (z) { + RETURN_FAIL("decode: \"%s\"", z); + } + + if (got.meta.wi != 0) { + RETURN_FAIL("got.meta.wi: got %d, want 0", (int)(got.meta.wi)); + } + if (src.meta.ri != 2) { + RETURN_FAIL("src.meta.ri: got %d, want 2", (int)(src.meta.ri)); + } + return NULL; +} + // ---------------- LZW Benches const char* do_bench_wuffs_lzw_decode(const char* filename, @@ -261,10 +304,11 @@ const char* bench_wuffs_lzw_decode_100k() { // The empty comments forces clang-format to place one element per line. proc tests[] = { - test_wuffs_lzw_decode_many_big_reads, // - test_wuffs_lzw_decode_many_small_writes_reads, // test_wuffs_lzw_decode_bricks_dither, // test_wuffs_lzw_decode_bricks_nodither, // + test_wuffs_lzw_decode_many_big_reads, // + test_wuffs_lzw_decode_many_small_writes_reads, // + test_wuffs_lzw_decode_output_empty, // test_wuffs_lzw_decode_pi, // NULL,