From 265d3a5a1a63ca65633b3dc19c44a093ff288b66 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 30 Jul 2017 11:13:40 +1000 Subject: [PATCH] Add a hand-written C impl of decode_huffman. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This gives a rough upper bound on how fast Puffs' generated C code can be, with a sufficiently smart Puffs compiler. A comparison of the 'old', safe, generated C code and the 'new', unsafe, hand-written C code: name old speed new speed delta puffs_flate_decode_1k/clang 127MB/s ± 2% 181MB/s ± 2% +42.27% (p=0.000 n=83+5) puffs_flate_decode_10k/clang 151MB/s ± 1% 235MB/s ± 2% +56.04% (p=0.000 n=82+5) puffs_flate_decode_100k/clang 165MB/s ± 1% 275MB/s ± 3% +66.69% (p=0.000 n=89+5) puffs_flate_decode_1k/gcc 155MB/s ± 2% 181MB/s ± 1% +16.96% (p=0.000 n=91+5) puffs_flate_decode_10k/gcc 146MB/s ± 2% 241MB/s ± 2% +64.78% (p=0.000 n=86+5) puffs_flate_decode_100k/gcc 166MB/s ± 1% 277MB/s ± 3% +66.78% (p=0.000 n=88+5) While we're discussing benchmark numbers, below is an overall snapshot. std/flate puffs_flate_decode_1k/clang 127MB/s ± 2% puffs_flate_decode_10k/clang 151MB/s ± 1% puffs_flate_decode_100k/clang 165MB/s ± 1% puffs_flate_decode_1k/gcc 155MB/s ± 2% puffs_flate_decode_10k/gcc 146MB/s ± 2% puffs_flate_decode_100k/gcc 166MB/s ± 1% mimic_adler32_10k 2.98GB/s ± 1% mimic_adler32_100k 2.93GB/s ± 1% mimic_crc32_10k 1.38GB/s ± 1% mimic_crc32_100k 1.37GB/s ± 1% mimic_flate_decode_1k 220MB/s ± 2% mimic_flate_decode_10k 262MB/s ± 1% mimic_flate_decode_100k 281MB/s ± 1% mimic_gzip_decode_10k 221MB/s ± 1% mimic_gzip_decode_100k 259MB/s ± 1% mimic_zlib_decode_10k 242MB/s ± 2% mimic_zlib_decode_100k 289MB/s ± 1% std/gif puffs_gif_lzw_decode_20k/clang 125MB/s ± 1% puffs_gif_lzw_decode_100k/clang 173MB/s ± 1% puffs_gif_decode_1k/clang 376MB/s ± 3% puffs_gif_decode_10k/clang 128MB/s ± 2% puffs_gif_decode_100k/clang 120MB/s ± 2% puffs_gif_decode_1000k/clang 121MB/s ± 3% puffs_gif_lzw_decode_20k/gcc 153MB/s ± 1% puffs_gif_lzw_decode_100k/gcc 208MB/s ± 1% puffs_gif_decode_1k/gcc 429MB/s ± 1% puffs_gif_decode_10k/gcc 161MB/s ± 1% puffs_gif_decode_100k/gcc 140MB/s ± 1% puffs_gif_decode_1000k/gcc 143MB/s ± 1% mimic_gif_decode_1k 158MB/s ± 1% mimic_gif_decode_10k 93.9MB/s ± 1% mimic_gif_decode_100k 100MB/s ± 1% mimic_gif_decode_1000k 102MB/s ± 1% --- gen/c/std/flate.c | 4 +- script/puffs-flate-decoder-decode-huffman.c | 234 ++++++++++++++++++++ 2 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 script/puffs-flate-decoder-decode-huffman.c diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 0fac4664..87166fc0 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -252,6 +252,8 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, // C HEADER ENDS HERE. +#include "../../../script/puffs-flate-decoder-decode-huffman.c" + #ifndef PUFFS_BASE_IMPL_H #define PUFFS_BASE_IMPL_H @@ -580,7 +582,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, } } } - status = puffs_flate_decoder_decode_huffman(self, a_dst, a_src); + status = c_puffs_flate_decoder_decode_huffman(self, a_dst, a_src); if (a_src.buf) { b_rptr_src = a_src.buf->ptr + a_src.buf->ri; size_t len = a_src.buf->wi - a_src.buf->ri; diff --git a/script/puffs-flate-decoder-decode-huffman.c b/script/puffs-flate-decoder-decode-huffman.c new file mode 100644 index 00000000..3e2d1089 --- /dev/null +++ b/script/puffs-flate-decoder-decode-huffman.c @@ -0,0 +1,234 @@ +// Use of this source code is governed by a BSD-style license that can be found +// in the LICENSE file. + +// This file contains a hand-written C implementation of gen/c/std/flate.c's +// generated puffs_flate_decoder_decode_huffman function. +// +// It is not intended to be used in production settings, on untrusted data. Its +// purpose is to give a rough upper bound on how fast Puffs' generated C code +// can be, with a sufficiently smart Puffs compiler. +// +// To repeat, substituting in this C implementation is NOT SAFE, and may result +// in buffer overflows. This code exists only to aid optimization of the (safe) +// std/flate/*.puffs code and the Puffs compiler's code generation. +// +// ---------------- +// +// Having said that, to generate the benchmark numbers with this hand-written C +// implementation, edit gen/c/std/flate.c and find the line that says +// +// C HEADER ENDS HERE. +// +// After that, add this line: +// +// #include "../../../script/puffs-flate-decoder-decode-huffman.c" +// +// Then find the call to puffs_flate_decoder_decode_huffman. It should be +// inside the puffs_flate_decoder_decode function body, and the line of code +// should look something like +// +// status = puffs_flate_decoder_decode_huffman(self, a_dst, a_src); +// +// Change the "puffs" to "c_puffs", i.e. add a "c_" prefix. The net result +// should look something like: +// +// ---------------- +// +// $ git diff +// diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c +// index 0fac466..87166fc 100644 +// --- a/gen/c/std/flate.c +// +++ b/gen/c/std/flate.c +// @@ -252,6 +252,8 @@ puffs_flate_status +// puffs_flate_decoder_decode(puffs_flate_decoder* self, +// +// // C HEADER ENDS HERE. +// +// +#include "../../../script/puffs-flate-decoder-decode-huffman.c" +// + +// #ifndef PUFFS_BASE_IMPL_H +// #define PUFFS_BASE_IMPL_H +// +// @@ -580,7 +582,7 @@ puffs_flate_status +// puffs_flate_decoder_decode(puffs_flate_decoder* self, +// } +// } +// } +// - status = puffs_flate_decoder_decode_huffman(self, a_dst, a_src); +// + status = c_puffs_flate_decoder_decode_huffman(self, a_dst, a_src); +// if (a_src.buf) { +// b_rptr_src = a_src.buf->ptr + a_src.buf->ri; +// size_t len = a_src.buf->wi - a_src.buf->ri; +// +// ---------------- +// +// That concludes the two edits to gen/c/std/flate.c. Run the C test and +// benchmark programs directly, instead of running "puffs test" or "puffs +// bench", since the "puffs" tool will re-generate the C code and override your +// gen/c/std/flate.c edit. +// +// cd test/c/std +// gcc -O3 flate.c && ./a.out && ./a.out -bench; rm -f ./a.out + +#include +#include // For manual printf debugging. + +puffs_flate_status c_puffs_flate_decoder_decode_huffman( + puffs_flate_decoder* self, + puffs_base_writer1 a_dst, + puffs_base_reader1 a_src) { + if (!a_dst.buf || !a_src.buf) { + return PUFFS_FLATE_ERROR_BAD_ARGUMENT; + } + puffs_flate_status status = PUFFS_FLATE_STATUS_OK; + + // Load contextual state. + uint8_t* pdst = a_dst.buf->ptr + a_dst.buf->wi; + uint8_t* psrc = a_src.buf->ptr + a_src.buf->ri; + uint32_t bits = self->private_impl.f_bits; + uint32_t n_bits = self->private_impl.f_n_bits; + + // Initialize other local variables. + uint8_t* pdst0 = pdst; + uint32_t lmask = + ((((uint32_t)(1)) << self->private_impl.f_n_huffs_bits[0]) - 1); + uint32_t dmask = + ((((uint32_t)(1)) << self->private_impl.f_n_huffs_bits[1]) - 1); + + // NOTE: a reminder that this loop is NOT SAFE, as it does not check that + // pdst and psrc remain within a_dst's and a_src's bounds. + +outer_loop: + while (true) { + // Ensure that we have at least 15 bits of input. + if (n_bits < 15) { + bits |= (((uint32_t)(*psrc++)) << n_bits); + n_bits += 8; + bits |= (((uint32_t)(*psrc++)) << n_bits); + n_bits += 8; + } + + // Decode an lcode symbol from H-L. + uint32_t table_entry = self->private_impl.f_huffs[0][bits & lmask]; + while (true) { + uint32_t n = table_entry & 0x0F; + bits >>= n; + n_bits -= n; + if ((table_entry >> 30) == 1) { + *pdst++ = (uint8_t)(table_entry >> 8); + goto outer_loop; + } + if ((table_entry >> 29) == 1) { + // Back-reference. + break; + } + if ((table_entry >> 28) == 1) { + // End of block. + goto end; + } + if ((table_entry >> 24) != 0x80) { + status = + PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; + goto end; + } + uint32_t top = (table_entry >> 8) & 0xFFFF; + uint32_t mask = ((((uint32_t)(1)) << ((table_entry >> 4) & 0x0F)) - 1); + table_entry = self->private_impl.f_huffs[0][top + (bits & mask)]; + } + + // length = base number + extra bits. + uint32_t length = (table_entry >> 8) & 0xFFFF; + { + uint32_t n = (table_entry >> 4) & 0x0F; + if (n) { + if (n_bits < n) { + bits |= (((uint32_t)(*psrc++)) << n_bits); + n_bits += 8; + } + length += bits & ((((uint32_t)(1)) << n) - 1); + bits >>= n; + n_bits -= n; + } + } + + // Ensure that we have at least 15 bits of input. + if (n_bits < 15) { + bits |= (((uint32_t)(*psrc++)) << n_bits); + n_bits += 8; + bits |= (((uint32_t)(*psrc++)) << n_bits); + n_bits += 8; + } + + // Decode a dcode symbol from H-D. + table_entry = self->private_impl.f_huffs[1][bits & dmask]; + while (true) { + uint32_t n = table_entry & 0x0F; + bits >>= n; + n_bits -= n; + if ((table_entry >> 29) == 1) { + // Back-reference. + break; + } + if ((table_entry >> 24) != 0x80) { + status = + PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; + goto end; + } + uint32_t top = (table_entry >> 8) & 0xFFFF; + uint32_t mask = ((((uint32_t)(1)) << ((table_entry >> 4) & 0x0F)) - 1); + table_entry = self->private_impl.f_huffs[1][top + (bits & mask)]; + } + + // distance = base number + extra bits. + uint32_t distance = (table_entry >> 8) & 0xFFFF; + { + uint32_t n = (table_entry >> 4) & 0x0F; + if (n) { + if (n_bits < 15) { + bits |= (((uint32_t)(*psrc++)) << n_bits); + n_bits += 8; + bits |= (((uint32_t)(*psrc++)) << n_bits); + n_bits += 8; + } + distance += bits & ((((uint32_t)(1)) << n) - 1); + bits >>= n; + n_bits -= n; + } + } + + // TODO: look at a sliding window, not just output written so far to dst. + if ((ptrdiff_t)(distance) > (pdst - pdst0)) { + status = PUFFS_FLATE_ERROR_BAD_ARGUMENT; + goto end; + } + + uint8_t* pback = pdst - distance; + for (; length >= 8; length -= 8) { + *pdst++ = *pback++; + *pdst++ = *pback++; + *pdst++ = *pback++; + *pdst++ = *pback++; + *pdst++ = *pback++; + *pdst++ = *pback++; + *pdst++ = *pback++; + *pdst++ = *pback++; + } + for (; length; length--) { + *pdst++ = *pback++; + } + } + +end: + // Return unused input bytes. + for (; n_bits >= 8; n_bits -= 8) { + psrc--; + } + + // Save contextual state. + a_dst.buf->wi = pdst - a_dst.buf->ptr; + a_src.buf->ri = psrc - a_src.buf->ptr; + self->private_impl.f_bits = bits; + self->private_impl.f_n_bits = n_bits; + + return status; +}