From 92f3958a28d98b534b3aacb6a6f9c22573786770 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 2 Feb 2019 08:46:40 +1100 Subject: [PATCH] Reduce the deflate decoder Huffman table size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta wuffs_deflate_decode_1k/clang5 138MB/s ± 0% 140MB/s ± 1% +1.16% (p=0.008 n=5+5) wuffs_deflate_decode_10k/clang5 228MB/s ± 1% 229MB/s ± 1% ~ (p=0.222 n=5+5) wuffs_deflate_decode_100k_just_one_read/clang5 267MB/s ± 1% 267MB/s ± 1% ~ (p=0.690 n=5+5) wuffs_deflate_decode_100k_many_big_reads/clang5 225MB/s ± 0% 225MB/s ± 0% ~ (p=0.310 n=5+5) wuffs_deflate_decode_1k/gcc7 157MB/s ± 0% 157MB/s ± 0% ~ (p=0.730 n=4+5) wuffs_deflate_decode_10k/gcc7 269MB/s ± 0% 269MB/s ± 1% ~ (p=0.222 n=5+5) wuffs_deflate_decode_100k_just_one_read/gcc7 321MB/s ± 0% 321MB/s ± 1% ~ (p=0.548 n=5+5) wuffs_deflate_decode_100k_many_big_reads/gcc7 254MB/s ± 0% 254MB/s ± 0% ~ (p=0.421 n=5+5) wuffs_gzip_decode_10k/clang5 205MB/s ± 0% 212MB/s ± 0% +3.50% (p=0.016 n=5+4) wuffs_gzip_decode_100k/clang5 238MB/s ± 0% 245MB/s ± 0% +3.31% (p=0.008 n=5+5) wuffs_gzip_decode_10k/gcc7 247MB/s ± 0% 248MB/s ± 0% +0.47% (p=0.016 n=5+5) wuffs_gzip_decode_100k/gcc7 293MB/s ± 1% 293MB/s ± 0% ~ (p=0.690 n=5+5) wuffs_zlib_decode_10k/clang5 210MB/s ± 0% 210MB/s ± 0% ~ (p=0.056 n=5+5) wuffs_zlib_decode_100k/clang5 244MB/s ± 0% 244MB/s ± 0% ~ (p=0.556 n=4+5) wuffs_zlib_decode_10k/gcc7 247MB/s ± 0% 249MB/s ± 1% +0.77% (p=0.008 n=5+5) wuffs_zlib_decode_100k/gcc7 288MB/s ± 0% 288MB/s ± 0% ~ (p=1.000 n=5+5) --- release/c/wuffs-unsupported-snapshot.c | 16 ++++++++-------- std/deflate/decode_deflate.wuffs | 8 ++++---- std/deflate/decode_huffman_fast.wuffs | 4 ++-- std/deflate/decode_huffman_slow.wuffs | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 56d8dcab..b34be51d 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -2533,7 +2533,7 @@ struct wuffs_deflate__decoder__struct { uint32_t f_bits; uint32_t f_n_bits; - uint32_t f_huffs[2][1234]; + uint32_t f_huffs[2][1024]; uint32_t f_n_huffs_bits[2]; uint8_t f_history[32768]; uint32_t f_history_index; @@ -5436,7 +5436,7 @@ static const uint32_t wuffs_deflate__dcode_magic_numbers[32] = { 134217728, 134217728, }; -static const uint32_t wuffs_deflate__huffs_table_size = 1234; +static const uint32_t wuffs_deflate__huffs_table_size = 1024; // ---------------- Private Initializer Prototypes @@ -6432,7 +6432,7 @@ label_1_break:; v_j -= 9; v_initial_high_bits = (((uint32_t)(1)) << v_j); v_top = v_next_top; - if ((v_top + (((uint32_t)(1)) << v_j)) > 1234) { + if ((v_top + (((uint32_t)(1)) << v_j)) > 1024) { return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_next_top = (v_top + (((uint32_t)(1)) << v_j)); @@ -6469,7 +6469,7 @@ label_1_break:; v_delta = (((uint32_t)(1)) << v_cl); while (v_high_bits >= v_delta) { v_high_bits -= v_delta; - if ((v_top + ((v_high_bits | v_reversed_key) & 511)) >= 1234) { + if ((v_top + ((v_high_bits | v_reversed_key) & 511)) >= 1024) { return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } self->private_impl @@ -6593,7 +6593,7 @@ label_0_continue:; } v_redir_top = ((v_table_entry >> 8) & 65535); v_redir_mask = ((((uint32_t)(1)) << ((v_table_entry >> 4) & 15)) - 1); - if ((v_redir_top + (v_bits & v_redir_mask)) >= 1234) { + if ((v_redir_top + (v_bits & v_redir_mask)) >= 1024) { status = wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; goto exit; @@ -6678,7 +6678,7 @@ label_0_continue:; } v_redir_top = ((v_table_entry >> 8) & 65535); v_redir_mask = ((((uint32_t)(1)) << ((v_table_entry >> 4) & 15)) - 1); - if ((v_redir_top + (v_bits & v_redir_mask)) >= 1234) { + if ((v_redir_top + (v_bits & v_redir_mask)) >= 1024) { status = wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; goto exit; @@ -6938,7 +6938,7 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self, v_redir_top = ((v_table_entry >> 8) & 65535); v_redir_mask = ((((uint32_t)(1)) << ((v_table_entry >> 4) & 15)) - 1); while (true) { - if ((v_redir_top + (v_bits & v_redir_mask)) >= 1234) { + if ((v_redir_top + (v_bits & v_redir_mask)) >= 1024) { status = wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; goto exit; @@ -7046,7 +7046,7 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self, v_redir_top = ((v_table_entry >> 8) & 65535); v_redir_mask = ((((uint32_t)(1)) << ((v_table_entry >> 4) & 15)) - 1); while (true) { - if ((v_redir_top + (v_bits & v_redir_mask)) >= 1234) { + if ((v_redir_top + (v_bits & v_redir_mask)) >= 1024) { status = wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; goto exit; diff --git a/std/deflate/decode_deflate.wuffs b/std/deflate/decode_deflate.wuffs index d9ef6712..017dc49f 100644 --- a/std/deflate/decode_deflate.wuffs +++ b/std/deflate/decode_deflate.wuffs @@ -53,9 +53,9 @@ pri const dcode_magic_numbers array[32] base.u32 = [ 0x401000B0, 0x401800B0, 0x402000C0, 0x403000C0, 0x404000D0, 0x406000D0, 0x08000000, 0x08000000, ] -// TODO: replace the magic "big enough" 1234 with something more principled, +// TODO: replace the magic "big enough" 1024 with something more principled, // perhaps discovered via an exhaustive search. -pri const huffs_table_size base.u32 = 1234 +pri const huffs_table_size base.u32 = 1024 pub struct decoder?( // These fields yield src's bits in Least Significant Bits order. @@ -771,7 +771,7 @@ pri func decoder.init_huff!(which base.u32[..1], n_codes0 base.u32[..320], n_cod if (top + ((1 as base.u32) << j)) > huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } - assert (top + ((1 as base.u32) << j)) <= 1234 via "a <= b: a <= c; c <= b"(c:huffs_table_size) + assert (top + ((1 as base.u32) << j)) <= 1024 via "a <= b: a <= c; c <= b"(c:huffs_table_size) next_top = top + ((1 as base.u32) << j) redirect_key = (reverse8[redirect_key >> 1] as base.u32) | ((redirect_key & 1) << 8) @@ -819,7 +819,7 @@ pri func decoder.init_huff!(which base.u32[..1], n_codes0 base.u32[..320], n_cod if (top + ((high_bits | reversed_key) & 511)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } - assert (top + ((high_bits | reversed_key) & 511)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) + assert (top + ((high_bits | reversed_key) & 511)) < 1024 via "a < b: a < c; c <= b"(c:huffs_table_size) this.huffs[args.which][top + ((high_bits | reversed_key) & 511)] = value } diff --git a/std/deflate/decode_huffman_fast.wuffs b/std/deflate/decode_huffman_fast.wuffs index d8e22b15..e0428d8b 100644 --- a/std/deflate/decode_huffman_fast.wuffs +++ b/std/deflate/decode_huffman_fast.wuffs @@ -133,7 +133,7 @@ pri func decoder.decode_huffman_fast!(dst base.io_writer, src base.io_reader) ba if (redir_top + (bits & redir_mask)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } - assert (redir_top + (bits & redir_mask)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) + assert (redir_top + (bits & redir_mask)) < 1024 via "a < b: a < c; c <= b"(c:huffs_table_size) table_entry = this.huffs[0][redir_top + (bits & redir_mask)] table_entry_n_bits = table_entry & 0x0F bits >>= table_entry_n_bits @@ -247,7 +247,7 @@ pri func decoder.decode_huffman_fast!(dst base.io_writer, src base.io_reader) ba if (redir_top + (bits & redir_mask)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } - assert (redir_top + (bits & redir_mask)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) + assert (redir_top + (bits & redir_mask)) < 1024 via "a < b: a < c; c <= b"(c:huffs_table_size) table_entry = this.huffs[1][redir_top + (bits & redir_mask)] table_entry_n_bits = table_entry & 0x0F bits >>= table_entry_n_bits diff --git a/std/deflate/decode_huffman_slow.wuffs b/std/deflate/decode_huffman_slow.wuffs index 07c4ebe6..a18a98e1 100644 --- a/std/deflate/decode_huffman_slow.wuffs +++ b/std/deflate/decode_huffman_slow.wuffs @@ -82,7 +82,7 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) { if (redir_top + (bits & redir_mask)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } - assert (redir_top + (bits & redir_mask)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) + assert (redir_top + (bits & redir_mask)) < 1024 via "a < b: a < c; c <= b"(c:huffs_table_size) table_entry = this.huffs[0][redir_top + (bits & redir_mask)] table_entry_n_bits = table_entry & 0x0F if n_bits >= table_entry_n_bits { @@ -164,7 +164,7 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) { if (redir_top + (bits & redir_mask)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } - assert (redir_top + (bits & redir_mask)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) + assert (redir_top + (bits & redir_mask)) < 1024 via "a < b: a < c; c <= b"(c:huffs_table_size) table_entry = this.huffs[1][redir_top + (bits & redir_mask)] table_entry_n_bits = table_entry & 0x0F if n_bits >= table_entry_n_bits {