Add etc_copy_u32_from_history_8_byte_chunks_fast

On a mid-range x86_64 laptop:

----

$ ls -lh linux-5.11.3.tar.gz | awk '{print $5 " " $9}'
178M linux-5.11.3.tar.gz

$ # Debian's /usr/bin/zcat
$ time /usr/bin/zcat        < linux-5.11.3.tar.gz > /dev/null
real	0m8.423s

$ # Wuffs; before
$ time gen/bin/example-zcat < linux-5.11.3.tar.gz > /dev/null
real	0m4.054s

$ # Wuffs; after
$ time gen/bin/example-zcat < linux-5.11.3.tar.gz > /dev/null
real	0m2.651s

----

Wuffs benchmarks:

wuffs_deflate_decode_1k_full_init/clang9                  95.6MB/s ± 0%  98.1MB/s ± 1%   +2.62%  (p=0.008 n=5+5)
wuffs_deflate_decode_1k_part_init/clang9                   109MB/s ± 0%   112MB/s ± 0%   +3.31%  (p=0.008 n=5+5)
wuffs_deflate_decode_10k_full_init/clang9                  162MB/s ± 0%   201MB/s ± 0%  +24.32%  (p=0.008 n=5+5)
wuffs_deflate_decode_10k_part_init/clang9                  165MB/s ± 0%   206MB/s ± 0%  +25.16%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_just_one_read/clang9             170MB/s ± 0%   253MB/s ± 0%  +48.63%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_many_big_reads/clang9            147MB/s ± 0%   160MB/s ± 0%   +8.77%  (p=0.016 n=4+5)

wuffs_deflate_decode_1k_full_init/gcc10                   92.1MB/s ± 0%  91.7MB/s ± 0%   -0.44%  (p=0.016 n=5+5)
wuffs_deflate_decode_1k_part_init/gcc10                    107MB/s ± 0%   104MB/s ± 0%   -3.35%  (p=0.008 n=5+5)
wuffs_deflate_decode_10k_full_init/gcc10                   149MB/s ± 0%   195MB/s ± 0%  +30.68%  (p=0.008 n=5+5)
wuffs_deflate_decode_10k_part_init/gcc10                   152MB/s ± 1%   199MB/s ± 1%  +31.23%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_just_one_read/gcc10              213MB/s ± 0%   256MB/s ± 0%  +20.42%  (p=0.008 n=5+5)
wuffs_deflate_decode_100k_many_big_reads/gcc10             177MB/s ± 0%   177MB/s ± 1%     ~     (p=0.595 n=5+5)

mimic_deflate_decode_1k                                    128MB/s ± 0%   128MB/s ± 0%   +0.42%  (p=0.016 n=5+4)
mimic_deflate_decode_10k                                   143MB/s ± 0%   143MB/s ± 0%     ~     (p=0.310 n=5+5)
mimic_deflate_decode_100k_just_one_read                    176MB/s ± 0%   176MB/s ± 0%   -0.24%  (p=0.008 n=5+5)
mimic_deflate_decode_100k_many_big_reads                   135MB/s ± 0%   135MB/s ± 0%   +0.12%  (p=0.016 n=5+5)

----

wuffs_gzip_decode_10k/clang9                               166MB/s ± 0%   198MB/s ± 1%  +19.66%  (p=0.008 n=5+5)
wuffs_gzip_decode_100k/clang9                              178MB/s ± 0%   241MB/s ± 0%  +35.41%  (p=0.008 n=5+5)

wuffs_gzip_decode_10k/gcc10                                172MB/s ± 0%   199MB/s ± 0%  +15.67%  (p=0.008 n=5+5)
wuffs_gzip_decode_100k/gcc10                               198MB/s ± 0%   258MB/s ± 0%  +29.95%  (p=0.008 n=5+5)

mimic_gzip_decode_10k                                      121MB/s ± 0%   121MB/s ± 0%     ~     (p=0.421 n=5+5)
mimic_gzip_decode_100k                                     144MB/s ± 0%   144MB/s ± 0%     ~     (p=0.548 n=5+5)

----

wuffs_zlib_decode_10k/clang9                               154MB/s ± 0%   199MB/s ± 0%  +28.89%  (p=0.016 n=4+5)
wuffs_zlib_decode_100k/clang9                              186MB/s ± 1%   234MB/s ± 1%  +25.58%  (p=0.008 n=5+5)

wuffs_zlib_decode_10k/gcc10                                156MB/s ± 0%   202MB/s ± 0%  +30.01%  (p=0.008 n=5+5)
wuffs_zlib_decode_100k/gcc10                               198MB/s ± 0%   262MB/s ± 0%  +32.21%  (p=0.008 n=5+5)

mimic_zlib_decode_10k                                      133MB/s ± 0%   133MB/s ± 0%     ~     (p=1.000 n=5+5)
mimic_zlib_decode_100k                                     160MB/s ± 0%   160MB/s ± 0%     ~     (p=0.095 n=5+5)

----

wuffs_png_decode_image_19k_8bpp/clang9                     115MB/s ± 0%   131MB/s ± 0%  +14.45%  (p=0.008 n=5+5)
wuffs_png_decode_image_40k_24bpp/clang9                    153MB/s ± 0%   153MB/s ± 0%   -0.51%  (p=0.008 n=5+5)
wuffs_png_decode_image_77k_8bpp/clang9                     415MB/s ± 0%   474MB/s ± 0%  +14.37%  (p=0.016 n=4+5)
wuffs_png_decode_image_552k_32bpp_ignore_checksum/clang9   345MB/s ± 1%   371MB/s ± 0%   +7.62%  (p=0.008 n=5+5)
wuffs_png_decode_image_552k_32bpp_verify_checksum/clang9   335MB/s ± 0%   359MB/s ± 0%   +7.06%  (p=0.016 n=4+5)
wuffs_png_decode_image_4002k_24bpp/clang9                  153MB/s ± 0%   156MB/s ± 0%   +2.37%  (p=0.008 n=5+5)

wuffs_png_decode_image_19k_8bpp/gcc10                      123MB/s ± 0%   135MB/s ± 1%   +9.38%  (p=0.008 n=5+5)
wuffs_png_decode_image_40k_24bpp/gcc10                     159MB/s ± 1%   163MB/s ± 0%   +2.60%  (p=0.016 n=5+4)
wuffs_png_decode_image_77k_8bpp/gcc10                      452MB/s ± 0%   479MB/s ± 0%   +6.00%  (p=0.008 n=5+5)
wuffs_png_decode_image_552k_32bpp_ignore_checksum/gcc10    353MB/s ± 0%   387MB/s ± 0%   +9.42%  (p=0.008 n=5+5)
wuffs_png_decode_image_552k_32bpp_verify_checksum/gcc10    341MB/s ± 0%   372MB/s ± 0%   +9.07%  (p=0.008 n=5+5)
wuffs_png_decode_image_4002k_24bpp/gcc10                   161MB/s ± 0%   164MB/s ± 0%   +1.86%  (p=0.016 n=4+5)

mimic_png_decode_image_19k_8bpp                           58.1MB/s ± 0%  58.2MB/s ± 0%   +0.22%  (p=0.008 n=5+5)
mimic_png_decode_image_40k_24bpp                          73.2MB/s ± 0%  73.2MB/s ± 0%     ~     (p=0.341 n=5+5)
mimic_png_decode_image_77k_8bpp                            177MB/s ± 0%   177MB/s ± 0%     ~     (p=0.095 n=5+5)
mimic_png_decode_image_552k_32bpp_ignore_checksum               skipped
mimic_png_decode_image_552k_32bpp_verify_checksum          147MB/s ± 0%   146MB/s ± 0%     ~     (p=0.056 n=5+5)
mimic_png_decode_image_4002k_24bpp                         104MB/s ± 0%   104MB/s ± 0%     ~     (p=0.841 n=4+5)
This commit is contained in:
Nigel Tao committed 2021-03-08 13:19:39 +11:00
1 parent 79c7ba0ae9
commit b58a961acb
8 files changed
+186 -49

No files matched your search

+31 -11
View File
@@ -44,7 +44,7 @@ pri func decoder.decode_huffman_fast64!(dst: base.io_writer, src: base.io_reader
dmask = ((1 as base.u64) << this.n_huffs_bits[1]) - 1
// Check up front, on each iteration, that we have enough buffer space to
// both read (8 bytes) and write (258 bytes) as much as we need to. Doing
// both read (8 bytes) and write (266 bytes) as much as we need to. Doing
// this check once (per iteration), up front, removes the need to check
// multiple times inside the loop body, so it's faster overall.
//
@@ -52,12 +52,18 @@ pri func decoder.decode_huffman_fast64!(dst: base.io_writer, src: base.io_reader
// 258 is the maximum length in a length-distance pair, as specified in the
// RFC section 3.2.5. Compressed blocks (length and distance codes).
//
// We adjust 258 up to (258 + 8) = 266 since it can be faster to overshoot
// a little and make multiple-of-8-byte copies even when the length in the
// length-distance pair isn't an exact multiple-of-8. Strictly speaking,
// 264 (an exact multiple-of-8) is the tight bound but (258 + 8) is easier
// for the Wuffs proof system, as length's type is refined to [..= 258],
//
// For reading, strictly speaking, we only need 6 bytes (48 bits) of
// available input, because the H-L Literal/Length code is up to 15 bits
// plus up to 5 extra bits, the H-D Distance code is up to 15 bits plus up
// to 13 extra bits and 15 + 5 + 15 + 13 == 48. However, it's faster to
// read 64 bits than 48 bits or (48 - n_bits) bits.
while.loop(args.dst.length() >= 258) and (args.src.length() >= 8) {
while.loop(args.dst.length() >= 266) and (args.src.length() >= 8) {
// Ensure that we have at least 56 bits of input.
//
// This is "Variant 4" of
@@ -178,8 +184,12 @@ pri func decoder.decode_huffman_fast64!(dst: base.io_writer, src: base.io_reader
// its presence minimizes the diff between decode_huffman_fastxx and
// decode_huffman_slow.
while true,
pre args.dst.length() >= 258,
pre args.dst.length() >= 266,
{
// We can therefore prove:
assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
assert ((length + 8) as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
// Copy from this.history.
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
// Set (hlen, hdist) to be the length-distance pair to copy
@@ -217,23 +227,33 @@ pri func decoder.decode_huffman_fast64!(dst: base.io_writer, src: base.io_reader
}
if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or
((length as base.u64) > args.dst.length()) {
((length as base.u64) > args.dst.length()) or
(((length + 8) as base.u64) > args.dst.length()) {
return "#internal error: inconsistent distance"
}
} else {
assert (length as base.u64) <= 258
assert args.dst.length() >= 258
// We can therefore prove:
assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 258)
}
// Once again, redundant but explicit assertions.
assert (dist_minus_1 + 1) > 0
assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length()
assert (length as base.u64) <= args.dst.length()
assert ((length + 8) as base.u64) <= args.dst.length()
// Copy from args.dst.
args.dst.limited_copy_u32_from_history_fast!(
up_to: length, distance: (dist_minus_1 + 1))
//
// For short distances, less than 8 bytes, copying atomic 8-byte
// chunks can result in incorrect output, so we fall back to a
// slower 1-byte-at-a-time copy. Deflate's copy-from-history can
// pick up freshly written bytes. A length = 5, distance = 2 copy
// starting with "abc" should give "abcbcbcb" but the 8-byte chunk
// technique would give "abcbc???", the exact output depending on
// what was previously in the writer buffer.
if (dist_minus_1 + 1) >= 8 {
args.dst.limited_copy_u32_from_history_8_byte_chunks_fast!(
up_to: length, distance: (dist_minus_1 + 1))
} else {
args.dst.limited_copy_u32_from_history_fast!(
up_to: length, distance: (dist_minus_1 + 1))
}
break
} endwhile
} endwhile.loop