From ccdd74a17f13baa1e9f118574630c9b4ac256e3c Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 6 Nov 2017 14:03:11 +1100 Subject: [PATCH] Remove some suspensions from decode_huffman_fast. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta puffs_adler32_10k/clang 2.45GB/s ± 0% 2.45GB/s ± 0% -0.08% (p=0.006 n=10+9) puffs_adler32_100k/clang 2.46GB/s ± 0% 2.45GB/s ± 0% -0.12% (p=0.000 n=10+9) puffs_flate_decode_1k/clang 137MB/s ± 0% 133MB/s ± 0% -2.71% (p=0.000 n=9+9) puffs_flate_decode_10k/clang 177MB/s ± 0% 181MB/s ± 0% +2.22% (p=0.000 n=10+9) puffs_flate_decode_100k/clang 198MB/s ± 0% 204MB/s ± 0% +3.04% (p=0.000 n=10+10) puffs_zlib_decode_10k/clang 163MB/s ± 0% 168MB/s ± 0% +3.23% (p=0.000 n=9+10) puffs_zlib_decode_100k/clang 182MB/s ± 0% 189MB/s ± 0% +3.56% (p=0.000 n=9+10) puffs_adler32_10k/gcc 3.26GB/s ± 0% 3.26GB/s ± 0% ~ (p=0.280 n=10+10) puffs_adler32_100k/gcc 3.27GB/s ± 0% 3.27GB/s ± 0% ~ (p=0.075 n=10+10) puffs_flate_decode_1k/gcc 111MB/s ± 1% 130MB/s ± 0% +17.19% (p=0.000 n=10+10) puffs_flate_decode_10k/gcc 142MB/s ± 0% 203MB/s ± 0% +43.11% (p=0.000 n=10+9) puffs_flate_decode_100k/gcc 153MB/s ± 0% 241MB/s ± 0% +57.62% (p=0.000 n=9+8) puffs_zlib_decode_10k/gcc 142MB/s ± 0% 188MB/s ± 0% +32.47% (p=0.000 n=9+9) puffs_zlib_decode_100k/gcc 149MB/s ± 0% 223MB/s ± 0% +50.03% (p=0.000 n=8+9) --- gen/c/std/flate.c | 26 ++++++++----------------- std/flate/decode_huffman_fast.puffs | 30 +++++++++-------------------- 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index b31f444d..7b514022 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -2291,25 +2291,15 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast( v_hdist), v_hlen); if (v_hlen <= v_n_copied) { - v_hlen = 0; goto label_1_break; } - if (v_n_copied > 0) { - v_hlen -= v_n_copied; - v_hdist = ((v_hdist + v_n_copied) & 32767); - if (v_hdist == 0) { - if (v_hlen > 0) { - puffs_base__writer1__copy_from_slice32( - &b_wptr_dst, b_wend_dst, - ((puffs_base__slice_u8){ - .ptr = self->private_impl.f_history, .len = 32768}), - v_hlen); - } - goto label_1_break; - } - } - status = PUFFS_FLATE__SUSPENSION_SHORT_WRITE; - PUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3); + v_hlen -= v_n_copied; + puffs_base__writer1__copy_from_slice32( + &b_wptr_dst, b_wend_dst, + ((puffs_base__slice_u8){.ptr = self->private_impl.f_history, + .len = 32768}), + v_hlen); + goto label_1_break; } label_1_break:; if (v_length == 0) { @@ -2326,7 +2316,7 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast( label_0_break:; while (v_n_bits >= 8) { v_n_bits -= 8; - PUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); + PUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); if (b_rptr_src == b_rstart_src) { status = PUFFS_FLATE__ERROR_INVALID_I_O_OPERATION; goto exit; diff --git a/std/flate/decode_huffman_fast.puffs b/std/flate/decode_huffman_fast.puffs index 129807cb..a8d90f8c 100644 --- a/std/flate/decode_huffman_fast.puffs +++ b/std/flate/decode_huffman_fast.puffs @@ -280,32 +280,20 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { // start copying from. hdist = (this.history_index - hdist) & 0x7FFF - // Copy from hdist to the end of this.history. + // Copy from hdist to the end of this.history, and then + // possibly wrap around the this.history ringbuffer. + // + // The "while true { etc; break }" is a redundant version of + // "etc", but its presence minimizes the diff between + // decode_huffman_fast and decode_huffman_slow. while true { n_copied = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen) if hlen <= n_copied { - hlen = 0 break } - if n_copied > 0 { - hlen -= n_copied - hdist = (hdist ~+ n_copied) & 0x7FFF - if hdist == 0 { - // Wrap around the this.history ringbuffer. - if hlen > 0 { - in.dst.copy_from_slice32(s:this.history[:], length:hlen) - } - break - } - } - // TODO: "closed for writes" instead? - return suspension "short write" - // TODO: this "assert false" should be a compiler error. - // This might mean differentiating "return" and "raise". - // - // TODO: invalidate this-related facts because the - // suspension can change the state of this and its fields. - assert false + hlen -= n_copied + in.dst.copy_from_slice32(s:this.history[:], length:hlen) + break } if length == 0 {