std/jpeg: tweak decode_mcu up-front r.length check

name                                             old speed     new speed     delta

wuffs_jpeg_decode_19k_8bpp/clang11               236MB/s ± 0%  237MB/s ± 0%  +0.11%  (p=0.032 n=4+5)
wuffs_jpeg_decode_30k_24bpp_progressive/clang11  210MB/s ± 0%  213MB/s ± 0%  +1.20%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_sequential/clang11   385MB/s ± 0%  384MB/s ± 0%    ~     (p=0.095 n=5+5)
wuffs_jpeg_decode_77k_24bpp/clang11              338MB/s ± 0%  338MB/s ± 0%    ~     (p=0.151 n=5+5)
wuffs_jpeg_decode_552k_24bpp_420/clang11         353MB/s ± 0%  353MB/s ± 0%    ~     (p=0.548 n=5+5)
wuffs_jpeg_decode_552k_24bpp_444/clang11         481MB/s ± 0%  481MB/s ± 0%    ~     (p=0.690 n=5+5)
wuffs_jpeg_decode_4002k_24bpp/clang11            360MB/s ± 0%  359MB/s ± 0%  -0.35%  (p=0.008 n=5+5)

wuffs_jpeg_decode_19k_8bpp/gcc10                 244MB/s ± 0%  245MB/s ± 0%  +0.44%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_progressive/gcc10    251MB/s ± 0%  247MB/s ± 0%  -1.46%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_sequential/gcc10     509MB/s ± 0%  510MB/s ± 0%  +0.13%  (p=0.008 n=5+5)
wuffs_jpeg_decode_77k_24bpp/gcc10                431MB/s ± 0%  432MB/s ± 0%  +0.24%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_420/gcc10           455MB/s ± 0%  456MB/s ± 0%  +0.26%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_444/gcc10           481MB/s ± 0%  484MB/s ± 0%  +0.56%  (p=0.016 n=4+5)
wuffs_jpeg_decode_4002k_24bpp/gcc10              463MB/s ± 0%  465MB/s ± 0%  +0.36%  (p=0.008 n=5+5)
This commit is contained in:
Nigel Tao committed 2023-06-22 11:37:40 +10:00
1 parent 255516bc21
commit a00150f62f
6 files changed
+85 -58

No files matched your search

+17 -5
View File
@@ -42415,6 +42415,10 @@ wuffs_jpeg__decoder__decode_mcu_progressive_ac_low_bit(
((uint64_t)(self->private_impl.f_bitstream_ri)));
while (true) {
while (true) {
if (((uint64_t)(io2_v_r - iop_v_r)) < 264u) {
v_ret = 1u;
goto label__goto_done__break;
}
while (true) {
if (self->private_impl.f_eob_run > 0u) {
goto label__goto_do_eob__break;
@@ -42422,8 +42426,8 @@ wuffs_jpeg__decoder__decode_mcu_progressive_ac_low_bit(
v_ac_h = self->private_impl.f_mcu_blocks_ac_hselector[0u];
v_ac_huff_table_fast = &self->private_impl.f_huff_tables_fast[v_ac_h][0u];
while (true) {
if (((uint64_t)(io2_v_r - iop_v_r)) < 24u) {
v_ret = 1u;
if (((uint64_t)(io2_v_r - iop_v_r)) < 8u) {
v_ret = 2u;
goto label__goto_done__break;
}
v_bits |= (wuffs_base__peek_u64be__no_bounds_check(iop_v_r) >> (v_n_bits & 63u));
@@ -42544,7 +42548,7 @@ wuffs_jpeg__decoder__decode_mcu_progressive_ac_low_bit(
if (self->private_data.f_mcu_blocks[0u][v_unzig] != 0u) {
if (v_n_bits == 0u) {
if (((uint64_t)(io2_v_r - iop_v_r)) < 8u) {
v_ret = 1u;
v_ret = 2u;
goto label__goto_done__break;
}
v_bits |= (wuffs_base__peek_u64be__no_bounds_check(iop_v_r) >> (v_n_bits & 63u));
@@ -42666,9 +42670,13 @@ wuffs_jpeg__decoder__decode_mcu_progressive_dc_high_bits(
((uint64_t)(self->private_impl.f_bitstream_ri)));
while (true) {
while (self->private_impl.f_mcu_current_block < self->private_impl.f_mcu_num_blocks) {
if (((uint64_t)(io2_v_r - iop_v_r)) < 264u) {
v_ret = 1u;
goto label__goto_done__break;
}
while (true) {
if (((uint64_t)(io2_v_r - iop_v_r)) < 8u) {
v_ret = 1u;
v_ret = 2u;
goto label__goto_done__break;
}
v_bits |= (wuffs_base__peek_u64be__no_bounds_check(iop_v_r) >> (v_n_bits & 63u));
@@ -42784,9 +42792,13 @@ wuffs_jpeg__decoder__decode_mcu_progressive_dc_low_bit(
((uint64_t)(self->private_impl.f_bitstream_ri)));
while (true) {
while (self->private_impl.f_mcu_current_block < self->private_impl.f_mcu_num_blocks) {
if (((uint64_t)(io2_v_r - iop_v_r)) < 264u) {
v_ret = 1u;
goto label__goto_done__break;
}
while (true) {
if (((uint64_t)(io2_v_r - iop_v_r)) < 8u) {
v_ret = 1u;
v_ret = 2u;
goto label__goto_done__break;
}
v_bits |= (wuffs_base__peek_u64be__no_bounds_check(iop_v_r) >> (v_n_bits & 63u));
+14 -16
View File
@@ -75,7 +75,7 @@ pri func decoder.decode_mcu!(workbuf: slice base.u8, mx: base.u32[..= 0x1FFF], m
{
this.mcu_blocks[.. 1].bulk_memset!(byte_value: 0)
// Load at least 47 bits, by keeping 56 .. 64 loaded.
// Load at least 56 bits.
if r.length() < 8 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
@@ -137,13 +137,11 @@ pri func decoder.decode_mcu!(workbuf: slice base.u8, mx: base.u32[..= 0x1FFF], m
break.dc_component
} endwhile.dc_component
// Ensure that we have enough bits for the entire
// while.ac_components loop.
//
// Worst case, there are 63 AC components (round that up to 64) and
// Ensure that we have enough bits for this iteration of the
// while.block loop body. Worst case, there are 64 components and
// each one needs (16 + 15) bits (round that up to 4 bytes), so we
// need (64 * 4) = 256 bytes of buffer. Add 8 bytes slack so that
// we can always call peek_u64be.
// need (64 * 4) = 256 bytes available. 8 more bytes of slack means
// that we can always call peek_u64be.
if r.length() < 264 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
@@ -171,7 +169,7 @@ pri func decoder.decode_mcu!(workbuf: slice base.u8, mx: base.u32[..= 0x1FFF], m
{
// Use the high bits of the bits variable to look up the
// Huffman table. Conceptually, we should do this *after*
// "Load at least 47 bits" below (at "LOOK-UP" further below),
// "Load at least 56 bits" below (at "LOOK-UP" further below),
// but it is noticably faster to do it beforehand:
// https://github.com/google/wuffs/commit/d9c7740ea6c4a450401c393787838436895b87bb
//
@@ -182,19 +180,19 @@ pri func decoder.decode_mcu!(workbuf: slice base.u8, mx: base.u32[..= 0x1FFF], m
// for almost all loop iterations, but it can be higher on the
// first iteration, set up by "Ensure the informal" above.
//
// For subsequent iterations, the "Load at least 47 bits" below
// For subsequent iterations, the "Load at least 56 bits" below
// does what it says. The Huffman code consumes up to 16 bits
// and processing the Huffman symbol's ssss nibble consumes up
// to 15 bits. (47 - 16 - 15) = 16 so at the end of the loop
// body, (n_bits >= 16).
// to 15 bits. (56 - 16 - 15) = 25 >= 16 so at the end of the
// loop body, (n_bits >= 16).
//
// This 16 bound has some slack, as (NB >= (64 - 56)) is all we
// need, but a bound of 16 (the longest JPEG Huffman code bit
// length) won't need to change even if, in the future, we
// enlarge the look-up table size.
// This 16 bound has some slack, as (NB >= 8) is all we need
// when ac_huff_table_fast has (1 << 8) entries, but a bound of
// 16 (the longest JPEG Huffman code bit length) won't need to
// change even if, in the future, the look-up table size grows.
ac_ht_fast = ac_huff_table_fast[bits >> 56] as base.u32
// Load at least 47 bits, by keeping 56 .. 64 loaded.
// Load at least 56 bits.
//
// TODO: eliminate this bounds check entirely.
if r.length() < 8 {
@@ -53,13 +53,11 @@ pri func decoder.decode_mcu_progressive_ac_high_bits!(workbuf: slice base.u8, mx
while.goto_done true {{
while.block true {
// Ensure that we have enough bits for the entire
// while.ac_components loop.
//
// Worst case, there are 63 AC components (round that up to 64) and
// Ensure that we have enough bits for this iteration of the
// while.block loop body. Worst case, there are 64 components and
// each one needs (16 + 15) bits (round that up to 4 bytes), so we
// need (64 * 4) = 256 bytes of buffer. Add 8 bytes slack so that
// we can always call peek_u64be.
// need (64 * 4) = 256 bytes available. 8 more bytes of slack means
// that we can always call peek_u64be.
if r.length() < 264 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
@@ -88,7 +86,7 @@ pri func decoder.decode_mcu_progressive_ac_high_bits!(workbuf: slice base.u8, mx
// Use the high bits of the bits variable to look up the
// Huffman table. Conceptually, we should do this *after*
// "Load at least 47 bits" below (at "LOOK-UP" further below),
// "Load at least 56 bits" below (at "LOOK-UP" further below),
// but it is noticably faster to do it beforehand:
// https://github.com/google/wuffs/commit/d9c7740ea6c4a450401c393787838436895b87bb
//
@@ -99,19 +97,19 @@ pri func decoder.decode_mcu_progressive_ac_high_bits!(workbuf: slice base.u8, mx
// for almost all loop iterations, but it can be higher on the
// first iteration, set up by "Ensure the informal" above.
//
// For subsequent iterations, the "Load at least 47 bits" below
// For subsequent iterations, the "Load at least 56 bits" below
// does what it says. The Huffman code consumes up to 16 bits
// and processing the Huffman symbol's ssss nibble consumes up
// to 15 bits. (47 - 16 - 15) = 16 so at the end of the loop
// body, (n_bits >= 16).
// to 15 bits. (56 - 16 - 15) = 25 >= 16 so at the end of the
// loop body, (n_bits >= 16).
//
// This 16 bound has some slack, as (NB >= (64 - 56)) is all we
// need, but a bound of 16 (the longest JPEG Huffman code bit
// length) won't need to change even if, in the future, we
// enlarge the look-up table size.
// This 16 bound has some slack, as (NB >= 8) is all we need
// when ac_huff_table_fast has (1 << 8) entries, but a bound of
// 16 (the longest JPEG Huffman code bit length) won't need to
// change even if, in the future, the look-up table size grows.
ac_ht_fast = ac_huff_table_fast[bits >> 56] as base.u32
// Load at least 47 bits, by keeping 56 .. 64 loaded.
// Load at least 56 bits.
//
// TODO: eliminate this bounds check entirely.
if r.length() < 8 {
@@ -52,6 +52,16 @@ pri func decoder.decode_mcu_progressive_ac_low_bit!(workbuf: slice base.u8, mx:
while.goto_done true {{
while.block true {
// Ensure that we have enough bits for this iteration of the
// while.block loop body. Worst case, there are 64 components and
// each one needs (16 + 15) bits (round that up to 4 bytes), so we
// need (64 * 4) = 256 bytes available. 8 more bytes of slack means
// that we can always call peek_u64be.
if r.length() < 264 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
}
while.goto_do_eob true {{
if this.eob_run > 0 {
break.goto_do_eob
@@ -62,22 +72,9 @@ pri func decoder.decode_mcu_progressive_ac_low_bit!(workbuf: slice base.u8, mx:
while.ac_components true,
inv ac_huff_table_fast <> nullptr,
{
// Load at least 47 bits, by keeping 56 .. 64 loaded (call it 8
// bytes). Just like the other decode_mcu_etc functions, this
// is enough for the Huffman-encoded ac_symbol (up to 16 bits)
// and processing the ac_value (up to 15 bits).
//
// After that, it's simpler for this "AC low bit" flavor if we
// never suspend for the rest of this iteration of the
// while.ac_components loop.
//
// Specifically, we want to have enough bits in the bitstream
// so that we don't suspend inside the while.refine_non_zeroes
// loop. In the worst case, we need another 8 bytes for
// refining non-zero coefficients plus 8 bytes slack so that we
// can always call peek_u64be. 8 + 8 + 8 = 24 bytes in total.
if r.length() < 24 {
ret = 1 // Request another fill_bitstream call.
// Load at least 56 bits.
if r.length() < 8 {
ret = 2 // Internal error.
break.goto_done
}
bits |= r.peek_u64be() >> (n_bits & 63)
@@ -203,9 +200,9 @@ pri func decoder.decode_mcu_progressive_ac_low_bit!(workbuf: slice base.u8, mx:
unzig = UNZIG[1 + this.mcu_zig_index]
if this.mcu_blocks[0][unzig] <> 0 {
if n_bits == 0 {
// Load at least 1 bit, by keeping 56 .. 64 loaded.
// Load at least 56 bits.
if r.length() < 8 {
ret = 1 // Request another fill_bitstream call.
ret = 2 // Internal error.
break.goto_done
}
bits |= r.peek_u64be() >> (n_bits & 63)
@@ -44,12 +44,23 @@ pri func decoder.decode_mcu_progressive_dc_high_bits!(workbuf: slice base.u8, mx
while.block this.mcu_current_block < this.mcu_num_blocks {
assert this.mcu_current_block < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks)
// Ensure that we have enough bits for this iteration of the
// while.block loop body. Worst case, there are 64 components and
// each one needs (16 + 15) bits (round that up to 4 bytes), so we
// need (64 * 4) = 256 bytes available. 8 more bytes of slack means
// that we can always call peek_u64be.
if r.length() < 264 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
}
while.dc_component true,
inv this.mcu_current_block < 10,
{
// Load at least 47 bits, by keeping 56 .. 64 loaded.
// Load at least 56 bits.
if r.length() < 8 {
ret = 1 // Request another fill_bitstream call.
ret = 2 // Internal error.
break.goto_done
}
bits |= r.peek_u64be() >> (n_bits & 63)
@@ -36,12 +36,23 @@ pri func decoder.decode_mcu_progressive_dc_low_bit!(workbuf: slice base.u8, mx:
while.block this.mcu_current_block < this.mcu_num_blocks {
assert this.mcu_current_block < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks)
// Ensure that we have enough bits for this iteration of the
// while.block loop body. Worst case, there are 64 components and
// each one needs (16 + 15) bits (round that up to 4 bytes), so we
// need (64 * 4) = 256 bytes available. 8 more bytes of slack means
// that we can always call peek_u64be.
if r.length() < 264 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
}
while.dc_component true,
inv this.mcu_current_block < 10,
{
// Load at least 47 bits, by keeping 56 .. 64 loaded.
// Load at least 56 bits.
if r.length() < 8 {
ret = 1 // Request another fill_bitstream call.
ret = 2 // Internal error.
break.goto_done
}
bits |= r.peek_u64be() >> (n_bits & 63)