From 24e7ee0104e10c49473e8915e6c17ff157ce9fe5 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 14 Oct 2018 22:35:42 +1100 Subject: [PATCH] Flush LZW output if stalled on input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta wuffs_gif_decode_1k_bw/clang 295MB/s ± 0% 292MB/s ± 1% -0.89% (p=0.008 n=5+5) wuffs_gif_decode_1k_color/clang 163MB/s ± 0% 143MB/s ± 0% -12.15% (p=0.008 n=5+5) wuffs_gif_decode_10k/clang 179MB/s ± 0% 150MB/s ± 1% -16.37% (p=0.008 n=5+5) wuffs_gif_decode_100k/clang 155MB/s ± 0% 152MB/s ± 0% -1.84% (p=0.016 n=5+4) wuffs_gif_decode_1000k/clang 159MB/s ± 0% 156MB/s ± 1% -2.26% (p=0.008 n=5+5) wuffs_gif_decode_1k_bw/gcc 313MB/s ± 0% 250MB/s ± 1% -20.20% (p=0.008 n=5+5) wuffs_gif_decode_1k_color/gcc 173MB/s ± 0% 172MB/s ± 1% -1.01% (p=0.008 n=5+5) wuffs_gif_decode_10k/gcc 184MB/s ± 1% 189MB/s ± 0% +2.34% (p=0.008 n=5+5) wuffs_gif_decode_100k/gcc 173MB/s ± 1% 166MB/s ± 0% -3.88% (p=0.008 n=5+5) wuffs_gif_decode_1000k/gcc 179MB/s ± 1% 171MB/s ± 0% -4.07% (p=0.008 n=5+5) wuffs_lzw_decode_20k/clang 185MB/s ± 0% 168MB/s ± 3% -9.34% (p=0.008 n=5+5) wuffs_lzw_decode_100k/clang 260MB/s ± 0% 252MB/s ± 1% -3.36% (p=0.008 n=5+5) wuffs_lzw_decode_20k/gcc 211MB/s ± 0% 209MB/s ± 0% -1.01% (p=0.008 n=5+5) wuffs_lzw_decode_100k/gcc 300MB/s ± 0% 307MB/s ± 0% +2.16% (p=0.008 n=5+5) --- release/c/wuffs-unsupported-snapshot.h | 25 ++++++++++++++++--------- std/lzw/decode_lzw.wuffs | 14 +++++++++++++- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 242603d5..f23197d6 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -9571,15 +9571,22 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, v_n_bits |= 24; } else { while (v_n_bits < v_width) { - { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); - if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) { - status = wuffs_base__suspension__short_read; - goto suspend; + while (((uint64_t)(io1_a_src - iop_a_src)) <= 0) { + if (v_j > 0) { + self->private_impl.f_flush_j = v_j; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + status = wuffs_lzw__decoder__flush(self, a_dst); + if (status) { + goto suspend; + } + v_j = 0; } - uint8_t t_0 = *iop_a_src++; - v_bits |= (((uint32_t)(t_0)) << v_n_bits); + status = wuffs_base__suspension__short_read; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2); } + v_bits |= + (((uint32_t)(wuffs_base__load_u8be(iop_a_src))) << v_n_bits); + (iop_a_src += 1, wuffs_base__return_empty_struct()); v_n_bits += 8; } } @@ -9654,7 +9661,7 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, } v_bits = ((v_bits) & ((1 << (v_n_bits)) - 1)); self->private_impl.f_flush_j = v_j; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); status = wuffs_lzw__decoder__flush(self, a_dst); if (status) { goto suspend; @@ -9675,7 +9682,7 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, } v_bits = ((v_bits) & ((1 << (v_n_bits)) - 1)); self->private_impl.f_flush_j = v_j; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); status = wuffs_lzw__decoder__flush(self, a_dst); if (status) { goto suspend; diff --git a/std/lzw/decode_lzw.wuffs b/std/lzw/decode_lzw.wuffs index 960336db..b085cbf4 100644 --- a/std/lzw/decode_lzw.wuffs +++ b/std/lzw/decode_lzw.wuffs @@ -76,7 +76,19 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) { post n_bits >= width, { assert n_bits < 12 via "a < b: a < c; c <= b"(c:width) - bits |= (args.src.read_u8!??() as base.u32) << n_bits + while args.src.available() <= 0, + inv n_bits < 12, + post args.src.available() >= 1, + { + if j > 0 { + this.flush_j = j + this.flush!??(dst:args.dst) + j = 0 + } + yield status "$short read" + } + bits |= (args.src.peek_u8() as base.u32) << n_bits + args.src.skip_fast!(actual:1, worst_case:1) n_bits += 8 } }