Extend std/deflate ringbuffer to simplify copies

name                                             old speed     new speed     delta

wuffs_deflate_decode_1k_full_init/clang5         155MB/s ± 1%  154MB/s ± 0%   -0.98%  (p=0.000 n=10+9)
wuffs_deflate_decode_1k_part_init/clang5         190MB/s ± 0%  193MB/s ± 1%   +1.49%  (p=0.000 n=10+9)
wuffs_deflate_decode_10k_full_init/clang5        240MB/s ± 0%  249MB/s ± 0%   +3.68%  (p=0.000 n=9+10)
wuffs_deflate_decode_10k_part_init/clang5        247MB/s ± 0%  257MB/s ± 1%   +4.03%  (p=0.000 n=10+10)
wuffs_deflate_decode_100k_just_one_read/clang5   292MB/s ± 1%  291MB/s ± 0%   -0.34%  (p=0.043 n=10+9)
wuffs_deflate_decode_100k_many_big_reads/clang5  246MB/s ± 0%  250MB/s ± 0%   +1.77%  (p=0.000 n=9+10)

wuffs_deflate_decode_1k_full_init/gcc9           166MB/s ± 1%  165MB/s ± 1%   -0.48%  (p=0.009 n=10+10)
wuffs_deflate_decode_1k_part_init/gcc9           208MB/s ± 1%  212MB/s ± 0%   +1.85%  (p=0.000 n=10+9)
wuffs_deflate_decode_10k_full_init/gcc9          248MB/s ± 1%  281MB/s ± 0%  +13.05%  (p=0.000 n=9+10)
wuffs_deflate_decode_10k_part_init/gcc9          256MB/s ± 0%  290MB/s ± 0%  +13.34%  (p=0.000 n=10+10)
wuffs_deflate_decode_100k_just_one_read/gcc9     336MB/s ± 0%  333MB/s ± 0%   -0.93%  (p=0.000 n=9+8)
wuffs_deflate_decode_100k_many_big_reads/gcc9    262MB/s ± 0%  280MB/s ± 1%   +6.99%  (p=0.000 n=9+10)
This commit is contained in:
Nigel Tao committed 2020-03-05 12:35:03 +11:00
1 parent 771d4f9d98
commit e65c861207
5 files changed
+80 -69

No files matched your search

+21 -11
View File
@@ -117,18 +117,24 @@ pub struct decoder? implements base.io_transformer(
// Exactly one of the eight bits [24 ..= 31] should be set.
huffs : array[2] array[huffs_table_size] base.u32,
// history holds up to the last 32KiB of decoded output, if the decoding
// was incomplete (e.g. due to a short read or write). RFC 1951 (DEFLATE)
// gives the maximum distance in a length-distance back-reference as 32768,
// or 0x8000.
// history[.. 0x8000] holds up to the last 32KiB of decoded output, if the
// decoding was incomplete (e.g. due to a short read or write). RFC 1951
// (DEFLATE) gives the maximum distance in a length-distance back-reference
// as 32768, or 0x8000. Similarly, the RFC gives the maximum length as 258.
//
// It is a ringbuffer, so that the most distant byte in the decoding isn't
// necessarily history[0]. The ringbuffer is full (i.e. it holds 32KiB of
// history) if and only if history_index >= 0x8000.
// history[.. 0x8000] is a ringbuffer, so that the most distant byte in
// the decoding isn't necessarily history[0]. The ringbuffer is full (i.e.
// it holds 32KiB of history) if and only if history_index >= 0x8000.
//
// history[history_index & 0x7FFF] is where the next byte of decoded output
// will be written.
history : array[0x8000] base.u8, // 32 KiB.
//
// When suspended in decoder.transform_io, or after an add_history call,
// history[0x8000 .. 0x8000 + (ML - 1)] duplicates history[.. (ML - 1)],
// where ML is the maximum length (258 as stated above). This simplifies
// copying up to ML bytes from the ringbuffer, as there is no need to split
// the copy around the 0x8000 index.
history : array[0x8000 + (258 - 1)] base.u8, // 32 KiB + (ML - 1) bytes.
// code_lengths is used to pass out-of-band data to init_huff.
//
@@ -147,12 +153,12 @@ pub func decoder.add_history!(hist: slice base.u8) {
// If s is longer than the ringbuffer, we can ignore the previous value
// of history_index, as we will overwrite the whole ringbuffer.
s = s.suffix(up_to: 0x8000)
this.history[..].copy_from_slice!(s: s)
this.history[.. 0x8000].copy_from_slice!(s: s)
this.history_index = 0x8000
} else {
// Otherwise, append s to the history ringbuffer starting at the
// previous history_index (modulo 0x8000).
n_copied = this.history[this.history_index & 0x7FFF ..].copy_from_slice!(s: s)
n_copied = this.history[this.history_index & 0x7FFF .. 0x8000].copy_from_slice!(s: s)
if n_copied < s.length() {
// a_slice.copy_from(s:b_slice) returns the minimum of the two
// slice lengths. If that value is less than b_slice.length(), then
@@ -162,7 +168,7 @@ pub func decoder.add_history!(hist: slice base.u8) {
// wrap around and copy the remainder of s over the start of the
// history ringbuffer.
s = s[n_copied ..]
n_copied = this.history[..].copy_from_slice!(s: s)
n_copied = this.history[.. 0x8000].copy_from_slice!(s: s)
// Set history_index (modulo 0x8000) to the length of this
// remainder. The &0x7FFF is redundant, but proves to the compiler
// that the conversion to u32 will not overflow. The +0x8000 is to
@@ -178,6 +184,10 @@ pub func decoder.add_history!(hist: slice base.u8) {
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as base.u32) + already_full
}
}
// Have the tail of this.history duplicate the head. Look for "ML" in the
// comments for the history field for more discussion.
this.history[0x8000 ..].copy_from_slice!(s: this.history[..])
}
pub func decoder.workbuf_len() base.range_ii_u64 {