mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Cache std/bzip2 Huffman tree lookup
name old speed new speed delta wuffs_bzip2_decode_10k/clang11 46.5MB/s ± 0% 60.5MB/s ± 0% +30.16% (p=0.008 n=5+5) wuffs_bzip2_decode_100k/clang11 39.4MB/s ± 1% 46.7MB/s ± 1% +18.43% (p=0.008 n=5+5) wuffs_bzip2_decode_10k/gcc10 43.8MB/s ± 0% 58.7MB/s ± 0% +34.07% (p=0.008 n=5+5) wuffs_bzip2_decode_100k/gcc10 35.9MB/s ± 0% 46.9MB/s ± 0% +30.92% (p=0.008 n=5+5)
This commit is contained in:
1 parent
fdad12d59f
commit
623290ef87
3 files changed
+90
-9
No files matched your search
@@ -21,13 +21,14 @@ pri func decoder.decode_block_fast!(src: base.io_reader) base.status {
|
||||
var section : base.u32
|
||||
var run_shift : base.u32[..= 23]
|
||||
|
||||
var child : base.u16
|
||||
var child_ff : base.u32[..= 255]
|
||||
var i : base.u32
|
||||
var j : base.u32
|
||||
var output : base.u32[..= 255]
|
||||
var run : base.u32[..= 16_777216] // 16_777216 = (2 << 23)
|
||||
var mtft0 : base.u32[..= 255]
|
||||
var table_entry : base.u16
|
||||
var child : base.u16
|
||||
var child_ff : base.u32[..= 255]
|
||||
var i : base.u32
|
||||
var j : base.u32
|
||||
var output : base.u32[..= 255]
|
||||
var run : base.u32[..= 16_777216] // 16_777216 = (2 << 23)
|
||||
var mtft0 : base.u32[..= 255]
|
||||
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
@@ -51,7 +52,10 @@ pri func decoder.decode_block_fast!(src: base.io_reader) base.status {
|
||||
args.src.skip_u32_fast!(actual: (31 - n_bits) >> 3, worst_case: 4)
|
||||
n_bits |= 24
|
||||
|
||||
child = 0
|
||||
table_entry = this.huffman_tables[which][bits >> 24]
|
||||
bits ~mod<<= table_entry >> 12
|
||||
n_bits -= (table_entry >> 12) as base.u32
|
||||
child = table_entry & 0x7FF
|
||||
while child < 1024 {
|
||||
child = this.huffman_trees[which][child][bits >> 31]
|
||||
bits ~mod<<= 1
|
||||
|
||||
@@ -75,6 +75,16 @@ pub struct decoder? implements base.io_transformer(
|
||||
// more rigorous about deriving this upper bound.
|
||||
huffman_trees : array[6] array[1024] array[2] base.u16,
|
||||
|
||||
// huffman_tables[which] caches the result of walking huffman_trees[which]
|
||||
// for each possible 8-bit input. The base.u16 value decomposes as:
|
||||
// - The low 11 bits are the final node, where finality comes either from
|
||||
// hitting a leaf node or from spending all 8 bits of the input. As per
|
||||
// the build_huffman_tree method, a value of 0x3FF = 1023 or less means
|
||||
// a branch node and 0x400 = 1024 or more means a leaf node.
|
||||
// - The middle bit is unused.
|
||||
// - The high 4 bits are the number of input bits spent.
|
||||
huffman_tables : array[6] array[256] base.u16,
|
||||
|
||||
// bwt is the Burrows Wheeler Transform state. Per the README.md file, each
|
||||
// entry is a row with the low 8 bits holding the L column and the high 20
|
||||
// bits holding the U column. The middle 4 bits are unused.
|
||||
@@ -400,6 +410,7 @@ pri func decoder.prepare_block?(src: base.io_reader) {
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
this.build_huffman_table!(which: i)
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
@@ -632,6 +643,28 @@ pri func decoder.build_huffman_tree!(which: base.u32[..= 5]) base.status {
|
||||
return ok
|
||||
}
|
||||
|
||||
pri func decoder.build_huffman_table!(which: base.u32[..= 5]) {
|
||||
var i : base.u32
|
||||
var bits : base.u32
|
||||
var n_bits : base.u16[..= 8]
|
||||
var child : base.u16
|
||||
|
||||
while i < 256 {
|
||||
bits = i << 24
|
||||
n_bits = 0
|
||||
child = 0
|
||||
while (child < 1024) and (n_bits < 8),
|
||||
inv i < 256,
|
||||
{
|
||||
child = this.huffman_trees[args.which][child][bits >> 31]
|
||||
bits ~mod<<= 1
|
||||
n_bits += 1
|
||||
} endwhile
|
||||
this.huffman_tables[args.which][i] = child | (n_bits << 12)
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// invert_bwt performs "two passes over the data, and one pass over the
|
||||
// alphabet", per the BWT technical report, except that the first data pass
|
||||
// (accumulating this.letter_counts) is integrated into the decode_block_etc
|
||||
|
||||
Reference in new issue
Block a user