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:
Nigel Tao committed 2022-05-23 15:51:51 +10:00
1 parent fdad12d59f
commit 623290ef87
3 files changed
+90 -9

No files matched your search

+45 -1
View File
@@ -6963,6 +6963,7 @@ struct wuffs_bzip2__decoder__struct {
uint8_t f_mtft[256]; uint8_t f_mtft[256];
uint8_t f_huffman_selectors[32768]; uint8_t f_huffman_selectors[32768];
uint16_t f_huffman_trees[6][1024][2]; uint16_t f_huffman_trees[6][1024][2];
uint16_t f_huffman_tables[6][256];
uint32_t f_bwt[1048576]; uint32_t f_bwt[1048576];
struct { struct {
@@ -24938,6 +24939,11 @@ wuffs_bzip2__decoder__build_huffman_tree(
wuffs_bzip2__decoder* self, wuffs_bzip2__decoder* self,
uint32_t a_which); uint32_t a_which);
static wuffs_base__empty_struct
wuffs_bzip2__decoder__build_huffman_table(
wuffs_bzip2__decoder* self,
uint32_t a_which);
static wuffs_base__empty_struct static wuffs_base__empty_struct
wuffs_bzip2__decoder__invert_bwt( wuffs_bzip2__decoder__invert_bwt(
wuffs_bzip2__decoder* self); wuffs_bzip2__decoder* self);
@@ -25047,6 +25053,7 @@ wuffs_bzip2__decoder__decode_block_fast(
uint32_t v_ticks = 0; uint32_t v_ticks = 0;
uint32_t v_section = 0; uint32_t v_section = 0;
uint32_t v_run_shift = 0; uint32_t v_run_shift = 0;
uint16_t v_table_entry = 0;
uint16_t v_child = 0; uint16_t v_child = 0;
uint32_t v_child_ff = 0; uint32_t v_child_ff = 0;
uint32_t v_i = 0; uint32_t v_i = 0;
@@ -25085,7 +25092,10 @@ wuffs_bzip2__decoder__decode_block_fast(
v_bits |= (wuffs_base__peek_u32be__no_bounds_check(iop_a_src) >> v_n_bits); v_bits |= (wuffs_base__peek_u32be__no_bounds_check(iop_a_src) >> v_n_bits);
iop_a_src += ((31 - v_n_bits) >> 3); iop_a_src += ((31 - v_n_bits) >> 3);
v_n_bits |= 24; v_n_bits |= 24;
v_child = 0; v_table_entry = self->private_data.f_huffman_tables[v_which][(v_bits >> 24)];
v_bits <<= (v_table_entry >> 12);
v_n_bits -= ((uint32_t)((v_table_entry >> 12)));
v_child = (v_table_entry & 2047);
while (v_child < 1024) { while (v_child < 1024) {
v_child = self->private_data.f_huffman_trees[v_which][v_child][(v_bits >> 31)]; v_child = self->private_data.f_huffman_trees[v_which][v_child][(v_bits >> 31)];
v_bits <<= 1; v_bits <<= 1;
@@ -25833,6 +25843,7 @@ wuffs_bzip2__decoder__prepare_block(
status = v_status; status = v_status;
goto exit; goto exit;
} }
wuffs_bzip2__decoder__build_huffman_table(self, v_i);
v_i += 1; v_i += 1;
} }
v_i = 0; v_i = 0;
@@ -26086,6 +26097,39 @@ wuffs_bzip2__decoder__build_huffman_tree(
return wuffs_base__make_status(NULL); return wuffs_base__make_status(NULL);
} }
// -------- func bzip2.decoder.build_huffman_table
static wuffs_base__empty_struct
wuffs_bzip2__decoder__build_huffman_table(
wuffs_bzip2__decoder* self,
uint32_t a_which) {
uint32_t v_i = 0;
uint32_t v_bits = 0;
uint16_t v_n_bits = 0;
uint16_t v_child = 0;
while (v_i < 256) {
v_bits = (v_i << 24);
v_n_bits = 0;
v_child = 0;
while ((v_child < 1024) && (v_n_bits < 8)) {
v_child = self->private_data.f_huffman_trees[a_which][v_child][(v_bits >> 31)];
v_bits <<= 1;
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
v_n_bits += 1;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
}
self->private_data.f_huffman_tables[a_which][v_i] = (v_child | (v_n_bits << 12));
v_i += 1;
}
return wuffs_base__make_empty_struct();
}
// -------- func bzip2.decoder.invert_bwt // -------- func bzip2.decoder.invert_bwt
static wuffs_base__empty_struct static wuffs_base__empty_struct
+12 -8
View File
@@ -21,13 +21,14 @@ pri func decoder.decode_block_fast!(src: base.io_reader) base.status {
var section : base.u32 var section : base.u32
var run_shift : base.u32[..= 23] var run_shift : base.u32[..= 23]
var child : base.u16 var table_entry : base.u16
var child_ff : base.u32[..= 255] var child : base.u16
var i : base.u32 var child_ff : base.u32[..= 255]
var j : base.u32 var i : base.u32
var output : base.u32[..= 255] var j : base.u32
var run : base.u32[..= 16_777216] // 16_777216 = (2 << 23) var output : base.u32[..= 255]
var mtft0 : base.u32[..= 255] var run : base.u32[..= 16_777216] // 16_777216 = (2 << 23)
var mtft0 : base.u32[..= 255]
bits = this.bits bits = this.bits
n_bits = this.n_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) args.src.skip_u32_fast!(actual: (31 - n_bits) >> 3, worst_case: 4)
n_bits |= 24 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 { while child < 1024 {
child = this.huffman_trees[which][child][bits >> 31] child = this.huffman_trees[which][child][bits >> 31]
bits ~mod<<= 1 bits ~mod<<= 1
+33
View File
@@ -75,6 +75,16 @@ pub struct decoder? implements base.io_transformer(
// more rigorous about deriving this upper bound. // more rigorous about deriving this upper bound.
huffman_trees : array[6] array[1024] array[2] base.u16, 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 // 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 // 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. // 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() { if status.is_error() {
return status return status
} }
this.build_huffman_table!(which: i)
i += 1 i += 1
} endwhile } endwhile
@@ -632,6 +643,28 @@ pri func decoder.build_huffman_tree!(which: base.u32[..= 5]) base.status {
return ok 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 // 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 // alphabet", per the BWT technical report, except that the first data pass
// (accumulating this.letter_counts) is integrated into the decode_block_etc // (accumulating this.letter_counts) is integrated into the decode_block_etc