mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 16:32:11 +01:00
We will need this later, to prove that the other read_u8 calls in decode_huffman_fast will not suspend. It's not obvious why the clang benchmarks don't change but the gcc ones (including the adler numbers, which are superficially unrelated) look worse. If the change from 6 to 12 bytes is a genuine performance regression, then a future commit can tighten the bound back to 6, although it would probably make the decode_huffman_fast assertions and proof requirements considerably more complicated. name old speed new speed delta puffs_adler32_10k/clang 2.40GB/s ± 1% 2.41GB/s ± 0% ~ (p=0.690 n=5+5) puffs_adler32_100k/clang 2.39GB/s ± 2% 2.40GB/s ± 1% ~ (p=1.000 n=5+5) puffs_flate_decode_1k/clang 128MB/s ± 0% 128MB/s ± 0% ~ (p=1.000 n=5+5) puffs_flate_decode_10k/clang 167MB/s ± 2% 168MB/s ± 0% ~ (p=0.548 n=5+5) puffs_flate_decode_100k/clang 193MB/s ± 2% 194MB/s ± 0% ~ (p=0.310 n=5+5) puffs_zlib_decode_10k/clang 157MB/s ± 0% 155MB/s ± 4% ~ (p=0.421 n=5+5) puffs_zlib_decode_100k/clang 180MB/s ± 1% 177MB/s ± 3% ~ (p=0.222 n=5+5) puffs_adler32_10k/gcc 3.26GB/s ± 0% 3.20GB/s ± 1% -1.81% (p=0.008 n=5+5) puffs_adler32_100k/gcc 3.26GB/s ± 0% 3.21GB/s ± 0% -1.63% (p=0.008 n=5+5) puffs_flate_decode_1k/gcc 129MB/s ± 0% 126MB/s ± 1% -2.30% (p=0.008 n=5+5) puffs_flate_decode_10k/gcc 164MB/s ± 0% 161MB/s ± 0% -1.86% (p=0.008 n=5+5) puffs_flate_decode_100k/gcc 185MB/s ± 0% 180MB/s ± 3% -2.81% (p=0.016 n=4+5) puffs_zlib_decode_10k/gcc 157MB/s ± 0% 151MB/s ± 2% -3.49% (p=0.008 n=5+5) puffs_zlib_decode_100k/gcc 175MB/s ± 0% 170MB/s ± 2% -2.97% (p=0.016 n=4+5)
287 lines
9.5 KiB
Plaintext
287 lines
9.5 KiB
Plaintext
// Copyright 2017 The Puffs Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// TODO: describe how the xxx_fast version differs from the xxx_slow one, the
|
|
// assumptions that xxx_fast makes, and how that makes it fast.
|
|
pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() {
|
|
// When editing this function, consider making the equivalent change to the
|
|
// decode_huffman_slow function. Keep the diff between the two
|
|
// decode_huffman_*.puffs files as small as possible, while retaining both
|
|
// correctness and performance.
|
|
|
|
var bits u32 = this.bits
|
|
var n_bits u32 = this.n_bits
|
|
|
|
// TODO: check that n_bits < 8?
|
|
|
|
var table_entry u32
|
|
var table_entry_n_bits u32[..15]
|
|
var lmask u32[..511] = ((1 as u32) << this.n_huffs_bits[0]) - 1
|
|
var dmask u32[..511] = ((1 as u32) << this.n_huffs_bits[1]) - 1
|
|
|
|
// Strictly speaking, we only need 6 bytes (48 bits) of available input,
|
|
// because the H-L length/literal 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, in.src.available() assertions are
|
|
// made in terms of bytes, not bits.
|
|
//
|
|
// In theory, we could write assertions that mixed bytes and bits, such as:
|
|
//
|
|
// assert ((in.src.available() >= 6) and (n_bits >= 5)) or
|
|
// ((in.src.available() >= 4) and (n_bits >= 21))
|
|
//
|
|
// but that looks clumsy and makes the proofs more complicated. Instead, we
|
|
// are conservative and work in terms of bytes only. In bytes, each code
|
|
// requires either one (direct) or two (redirect) lookups, up to 2 bytes
|
|
// for each. There are also up to 13 extra bits, or 2 bytes. Thus, each
|
|
// code requires up to 2 + 2 + 2 == 6 bytes, and there are two codes (one
|
|
// from H-L and one from H-D). Therefore, we check for at least 12 bytes.
|
|
while:loop in.src.available() >= 12 {
|
|
// Ensure that we have at least 15 bits of input.
|
|
if n_bits < 15 {
|
|
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
|
n_bits += 8
|
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
|
n_bits += 8
|
|
assert n_bits >= 15
|
|
} else {
|
|
assert in.src.available() >= 10
|
|
}
|
|
|
|
// These assertions are redundant, but are listed explicitly for
|
|
// clarity. They must hold regardless of which branch was taken for the
|
|
// if statement above.
|
|
assert in.src.available() >= 10
|
|
assert n_bits >= 15
|
|
|
|
// Decode an lcode symbol from H-L.
|
|
table_entry = this.huffs[0][bits & lmask]
|
|
table_entry_n_bits = table_entry & 0x0F
|
|
bits >>= table_entry_n_bits
|
|
n_bits -= table_entry_n_bits
|
|
|
|
if (table_entry >> 31) != 0 {
|
|
// Literal.
|
|
in.dst.write_u8?(x:(table_entry >> 8) & 0xFF)
|
|
continue:loop
|
|
} else if (table_entry >> 30) != 0 {
|
|
// No-op; code continues past the if-else chain.
|
|
} else if (table_entry >> 29) != 0 {
|
|
// End of block.
|
|
this.end_of_block = true
|
|
break:loop
|
|
} else if (table_entry >> 28) != 0 {
|
|
// Redirect.
|
|
var redir_top u32[..0xFFFF] = (table_entry >> 8) & 0xFFFF
|
|
var redir_mask u32[..0x7FFF] = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
|
|
while true {
|
|
if (redir_top + (bits & redir_mask)) >= 1234 {
|
|
return error "internal error: inconsistent Huffman decoder state"
|
|
}
|
|
table_entry = this.huffs[0][redir_top + (bits & redir_mask)]
|
|
table_entry_n_bits = table_entry & 0x0F
|
|
if n_bits >= table_entry_n_bits {
|
|
bits >>= table_entry_n_bits
|
|
n_bits -= table_entry_n_bits
|
|
break
|
|
}
|
|
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
|
n_bits += 8
|
|
}
|
|
|
|
if (table_entry >> 31) != 0 {
|
|
// Literal.
|
|
in.dst.write_u8?(x:(table_entry >> 8) & 0xFF)
|
|
continue:loop
|
|
} else if (table_entry >> 30) != 0 {
|
|
// No-op; code continues past the if-else chain.
|
|
} else if (table_entry >> 29) != 0 {
|
|
// End of block.
|
|
this.end_of_block = true
|
|
break:loop
|
|
} else if (table_entry >> 28) != 0 {
|
|
return error "internal error: inconsistent Huffman decoder state"
|
|
} else if (table_entry >> 27) != 0 {
|
|
return error "bad Huffman code"
|
|
} else {
|
|
return error "internal error: inconsistent Huffman decoder state"
|
|
}
|
|
|
|
} else if (table_entry >> 27) != 0 {
|
|
return error "bad Huffman code"
|
|
} else {
|
|
return error "internal error: inconsistent Huffman decoder state"
|
|
}
|
|
|
|
// length = base number + extra bits.
|
|
var length u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
|
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
|
while n_bits < table_entry_n_bits,
|
|
post n_bits >= table_entry_n_bits,
|
|
{
|
|
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
|
n_bits += 8
|
|
}
|
|
length = (length + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF
|
|
bits >>= table_entry_n_bits
|
|
n_bits -= table_entry_n_bits
|
|
|
|
// Decode a dcode symbol from H-D.
|
|
while true {
|
|
table_entry = this.huffs[1][bits & dmask]
|
|
table_entry_n_bits = table_entry & 15
|
|
if n_bits >= table_entry_n_bits {
|
|
bits >>= table_entry_n_bits
|
|
n_bits -= table_entry_n_bits
|
|
break
|
|
}
|
|
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
|
n_bits += 8
|
|
}
|
|
// Check for a redirect.
|
|
if (table_entry >> 28) == 1 {
|
|
redir_top = (table_entry >> 8) & 0xFFFF
|
|
redir_mask = ((1 as u32) << ((table_entry >> 4) & 0x0F)) - 1
|
|
while true {
|
|
if (redir_top + (bits & redir_mask)) >= 1234 {
|
|
return error "internal error: inconsistent Huffman decoder state"
|
|
}
|
|
table_entry = this.huffs[1][redir_top + (bits & redir_mask)]
|
|
table_entry_n_bits = table_entry & 0x0F
|
|
if n_bits >= table_entry_n_bits {
|
|
bits >>= table_entry_n_bits
|
|
n_bits -= table_entry_n_bits
|
|
break
|
|
}
|
|
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
|
n_bits += 8
|
|
}
|
|
if (table_entry >> 31) != 0 {
|
|
return error "internal error: inconsistent Huffman decoder state"
|
|
}
|
|
}
|
|
|
|
// For H-D, all symbols should be base number + extra bits.
|
|
if (table_entry >> 24) != 0x40 {
|
|
if (table_entry >> 24) == 0x08 {
|
|
return error "bad Huffman code"
|
|
}
|
|
return error "internal error: inconsistent Huffman decoder state"
|
|
}
|
|
|
|
// distance = base number + extra bits.
|
|
var distance u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF
|
|
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
|
while n_bits < table_entry_n_bits,
|
|
post n_bits >= table_entry_n_bits,
|
|
{
|
|
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
|
n_bits += 8
|
|
}
|
|
distance = (distance + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF
|
|
bits >>= table_entry_n_bits
|
|
n_bits -= table_entry_n_bits
|
|
|
|
var n_copied u32
|
|
while true {
|
|
// Copy from this.history.
|
|
if (distance as u64) > in.dst.since_mark().length() {
|
|
// Set (hlen, hdist) to be the length-distance pair to copy
|
|
// from this.history, and (length, distance) to be the
|
|
// remaining length-distance pair to copy from in.dst.
|
|
var hlen u32[..0x7FFF]
|
|
var hdist u32[..0x7FFF] = ((distance as u64) - in.dst.since_mark().length()) as u32
|
|
if length > hdist {
|
|
length -= hdist
|
|
hlen = hdist
|
|
} else {
|
|
hlen = length
|
|
length = 0
|
|
}
|
|
if this.history_index < hdist {
|
|
return error "bad distance"
|
|
}
|
|
// Re-purpose the hdist variable as the this.history index to
|
|
// start copying from.
|
|
hdist = (this.history_index - hdist) & 0x7FFF
|
|
|
|
// Copy from hdist to the end of this.history.
|
|
while true {
|
|
n_copied = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen)
|
|
if hlen <= n_copied {
|
|
hlen = 0
|
|
break
|
|
}
|
|
if n_copied > 0 {
|
|
hlen -= n_copied
|
|
hdist = (hdist ~+ n_copied) & 0x7FFF
|
|
if hdist == 0 {
|
|
// Wrap around the this.history ringbuffer.
|
|
break
|
|
}
|
|
}
|
|
// TODO: "closed for writes" instead?
|
|
return suspension "short write"
|
|
// TODO: this "assert false" should be a compiler error.
|
|
// This might mean differentiating "return" and "raise".
|
|
//
|
|
// TODO: invalidate this-related facts because the
|
|
// suspension can change the state of this and its fields.
|
|
assert false
|
|
}
|
|
// Copy from the start of this.history, if we wrapped around.
|
|
if hlen > 0 {
|
|
while true {
|
|
n_copied = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen)
|
|
if hlen <= n_copied {
|
|
hlen = 0
|
|
break
|
|
}
|
|
hlen -= n_copied
|
|
hdist = (hdist + (n_copied & 0x7FFF)) & 0x7FFF
|
|
// TODO: "closed for writes" instead?
|
|
return suspension "short write"
|
|
}
|
|
}
|
|
|
|
if length == 0 {
|
|
// No need to copy from in.dst.
|
|
continue:loop
|
|
}
|
|
}
|
|
|
|
// Copy from in.dst.
|
|
n_copied = in.dst.copy_from_history32(distance:distance, length:length)
|
|
if length <= n_copied {
|
|
length = 0
|
|
break
|
|
}
|
|
length -= n_copied
|
|
// TODO: "closed for writes" instead?
|
|
return suspension "short write"
|
|
}
|
|
}
|
|
|
|
// TODO: unread some in.src bytes if n_bits >= 8?
|
|
|
|
this.bits = bits
|
|
this.n_bits = n_bits
|
|
}
|