From 2e6b364dcccdf9804ac9ff256a2a1148ff063b02 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 3 Aug 2017 17:45:47 +1000 Subject: [PATCH] Do 8- instead of 1-byte back-copies. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta puffs_flate_decode_1k/clang 191MB/s ± 1% 171MB/s ±12% ~ (p=0.095 n=5+5) puffs_flate_decode_10k/clang 277MB/s ± 1% 325MB/s ± 7% +17.47% (p=0.008 n=5+5) puffs_flate_decode_100k/clang 315MB/s ± 1% 416MB/s ± 1% +31.88% (p=0.008 n=5+5) puffs_flate_decode_1k/gcc 194MB/s ± 2% 197MB/s ± 0% +1.98% (p=0.016 n=5+4) puffs_flate_decode_10k/gcc 279MB/s ± 1% 361MB/s ± 1% +29.19% (p=0.016 n=4+5) puffs_flate_decode_100k/gcc 315MB/s ± 0% 437MB/s ± 0% +38.77% (p=0.008 n=5+5) Note that these are benchmark numbers for an 'ideal', hand-written C implementation, not for Puffs' generated C code. --- script/puffs-flate-decoder-decode-huffman.c | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/script/puffs-flate-decoder-decode-huffman.c b/script/puffs-flate-decoder-decode-huffman.c index 03469871..5b1e7357 100644 --- a/script/puffs-flate-decoder-decode-huffman.c +++ b/script/puffs-flate-decoder-decode-huffman.c @@ -235,6 +235,30 @@ outer_loop: } uint8_t* pback = pdst - distance; + +#ifdef PUFFS_FLATE_HAVE_64_BIT_UNALIGNED_LITTLE_ENDIAN_LOADS + // Back-copy fast path, copying 8 instead of 1 bytes at a time. + // + // This always copies 8*N bytes (where N is the smallest integer such that + // 8*N >= length, i.e. we round length up to a multiple of 8), instead of + // only length bytes, but that's OK, as subsequent iterations will fix up + // the overrun. + if (distance >= 8) { + while (1) { + *((uint64_t*)(pdst)) = *((uint64_t*)(pback)); + if (length <= 8) { + pdst += length; + break; + } + pdst += 8; + pback += 8; + length -= 8; + } + continue; + } +#endif + + // Back-copy slow path. for (; length >= 3; length -= 3) { *pdst++ = *pback++; *pdst++ = *pback++;