std/jpeg: check up-front for sufficient AC bits

The micro-benchmarks are mixed for now, but this sets us up to remove an
inner-loop bounds check in a future commit.

name                                             old speed     new speed     delta

wuffs_jpeg_decode_19k_8bpp/clang11               226MB/s ± 0%  224MB/s ± 1%  -1.03%  (p=0.029 n=4+4)
wuffs_jpeg_decode_30k_24bpp_progressive/clang11  210MB/s ± 0%  206MB/s ± 0%  -1.59%  (p=0.029 n=4+4)
wuffs_jpeg_decode_30k_24bpp_sequential/clang11   379MB/s ± 0%  374MB/s ± 0%  -1.25%  (p=0.016 n=4+5)
wuffs_jpeg_decode_77k_24bpp/clang11              326MB/s ± 0%  319MB/s ± 0%  -2.35%  (p=0.029 n=4+4)
wuffs_jpeg_decode_552k_24bpp_420/clang11         338MB/s ± 0%  329MB/s ± 0%  -2.53%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_444/clang11         468MB/s ± 0%  465MB/s ± 0%  -0.58%  (p=0.008 n=5+5)
wuffs_jpeg_decode_4002k_24bpp/clang11            347MB/s ± 0%  332MB/s ± 0%  -4.41%  (p=0.016 n=4+5)

wuffs_jpeg_decode_19k_8bpp/gcc10                 174MB/s ± 0%  176MB/s ± 0%  +1.18%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_progressive/gcc10    247MB/s ± 0%  246MB/s ± 0%  -0.55%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_sequential/gcc10     477MB/s ± 0%  472MB/s ± 0%  -0.90%  (p=0.008 n=5+5)
wuffs_jpeg_decode_77k_24bpp/gcc10                328MB/s ± 0%  334MB/s ± 0%  +1.69%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_420/gcc10           346MB/s ± 0%  351MB/s ± 0%  +1.57%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_444/gcc10           424MB/s ± 0%  425MB/s ± 0%  +0.39%  (p=0.008 n=5+5)
wuffs_jpeg_decode_4002k_24bpp/gcc10              350MB/s ± 0%  349MB/s ± 0%  -0.23%  (p=0.008 n=5+5)
This commit is contained in:
Nigel Tao committed 2023-06-18 17:37:12 +10:00
1 parent 2ca61b521f
commit 143991a301
5 files changed
+46 -8

No files matched your search

+11 -3
View File
@@ -41028,7 +41028,7 @@ wuffs_jpeg__decoder__fill_bitstream(
label__0__break:;
if (((uint64_t)(io2_a_src - iop_a_src)) > 1u) {
if ((wuffs_base__peek_u8be__no_bounds_check(iop_a_src) >= 255u) && ((wuffs_base__peek_u16le__no_bounds_check(iop_a_src) >> 8u) > 0u)) {
v_new_wi = (wuffs_base__u32__min(v_wi, 2016u) + 32u);
v_new_wi = (wuffs_base__u32__min(v_wi, 1784u) + 264u);
if (v_wi < v_new_wi) {
wuffs_base__bulk_memset(&self->private_data.f_bitstream_buffer[v_wi], (v_new_wi - v_wi), 0u);
}
@@ -41676,11 +41676,15 @@ wuffs_jpeg__decoder__decode_mcu__choosy_default(
goto label__dc_component__break;
}
label__dc_component__break:;
if (((uint64_t)(io2_v_r - iop_v_r)) < 264u) {
v_ret = 1u;
goto label__goto_done__break;
}
v_ac_h = self->private_impl.f_mcu_blocks_ac_hselector[self->private_impl.f_mcu_current_block];
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)) < 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));
@@ -41837,11 +41841,15 @@ wuffs_jpeg__decoder__decode_mcu_progressive_ac_high_bits(
((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;
}
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)) < 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));
+2 -2
View File
@@ -1514,11 +1514,11 @@ pri func decoder.fill_bitstream!(src: base.io_reader) {
} endwhile
// If we hit a "\xFF\x??" marker then pad the bitstream. In this method
// invocation, insert up to 32 bytes of implicit zeroes. Repeated calls to
// invocation, insert up to 264 bytes of implicit zeroes. Repeated calls to
// this method can insert even more.
if args.src.length() > 1 {
if (args.src.peek_u8() >= 0xFF) and ((args.src.peek_u16le() >> 8) > 0) {
new_wi = wi.min(no_more_than: 0x800 - 32) + 32
new_wi = wi.min(no_more_than: 0x800 - 264) + 264
if wi < new_wi {
this.bitstream_buffer[wi .. new_wi].bulk_memset!(byte_value: 0)
}
+15 -1
View File
@@ -133,6 +133,18 @@ 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
// 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.
if r.length() < 264 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
}
ac_h = this.mcu_blocks_ac_hselector[this.mcu_current_block]
ac_huff_table_fast = this.huff_tables_fast[ac_h][..] as ptr array[256] base.u16
while.ac_components true,
@@ -140,8 +152,10 @@ pri func decoder.decode_mcu!(workbuf: slice base.u8, mx: base.u32[..= 0x1FFF], m
inv ac_huff_table_fast <> nullptr,
{
// Load at least 31 bits, by keeping 56 .. 64 loaded.
//
// TODO: eliminate this bounds check entirely.
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)
@@ -52,14 +52,28 @@ 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
// 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.
if r.length() < 264 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
}
ac_h = this.mcu_blocks_ac_hselector[0]
ac_huff_table_fast = this.huff_tables_fast[ac_h][..] as ptr array[256] base.u16
while.ac_components true,
inv ac_huff_table_fast <> nullptr,
{
// Load at least 31 bits, by keeping 56 .. 64 loaded.
//
// TODO: eliminate this bounds check entirely.
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)
@@ -68,7 +68,9 @@ pri func decoder.decode_mcu_progressive_ac_low_bit!(workbuf: slice base.u8, mx:
// 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 the while.ac_components loop.
// 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