mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 01:01:35 +01:00
std/brotli: add decoder fast paths
Add non-coroutine read_symbol_fast and read_bits_fast helpers that are tried before the coroutine versions, and a process_commands_fast loop that keeps a 64-bit read-ahead bit buffer and hands back the whole unconsumed bytes when it exits. process_commands now tracks its position in a command (cmd_state) so that both loops can resume each other. LZ77 copies that neither wrap nor reach the ring buffer's end are done in chunks of at most the distance, which matches a byte-by-byte copy. Output is unchanged. On alice29 (decoder reused) decoding is about 1.8x to 2.1x faster for q1, q5, q9 and q11 inputs.
This commit is contained in:
4 files changed
+1286
-122
No files matched your search
@@ -230,6 +230,13 @@ pub struct decoder? implements base.io_transformer(
|
||||
insert_length : base.u32,
|
||||
copy_length : base.u32,
|
||||
|
||||
// cmd_state is where process_commands (and process_commands_fast)
|
||||
// are within a command: 0 at its start, 1 when reading its literals
|
||||
// (insert_length is how many are left), 2 when reading its distance
|
||||
// and 3 when applying its copy. process_commands_fast sets it to 4
|
||||
// when the metablock is done.
|
||||
cmd_state : base.u32[..= 4],
|
||||
|
||||
// quirk_large_window is the QUIRK_LARGE_WINDOW quirk (the C code's
|
||||
// BROTLI_DECODER_PARAM_LARGE_WINDOW parameter). large_window is
|
||||
// whether the stream is a large-window stream (the C code's
|
||||
@@ -679,6 +686,93 @@ pri func decoder.read_symbol?(src: base.io_reader, hoff: base.u32) {
|
||||
this.n_bits = n_bits
|
||||
}
|
||||
|
||||
// read_bits_fast is like read_bits but is not a coroutine. It reads input
|
||||
// bytes exactly when read_bits would, for as long as args.src has them. It
|
||||
// returns 0 when it has set this.bits_value, or 1 when read_bits must finish
|
||||
// the job (this.bits and this.n_bits hold the bits read so far).
|
||||
pri func decoder.read_bits_fast!(src: base.io_reader, n: base.u32[..= 24]) base.u32 {
|
||||
var n : base.u32[..= 24]
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32
|
||||
|
||||
n = args.n
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
while n_bits < n,
|
||||
post n_bits >= n,
|
||||
{
|
||||
if args.src.length() <= 0 {
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
return 1
|
||||
}
|
||||
assert n_bits < 24 via "a < b: a < c; c <= b"(c: n)
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
}
|
||||
this.bits_value = bits & (((1 as base.u32) << n) - 1)
|
||||
this.bits = bits >> n
|
||||
this.n_bits = n_bits - n
|
||||
return 0
|
||||
}
|
||||
|
||||
// read_symbol_fast is like read_symbol but is not a coroutine. It reads
|
||||
// input bytes exactly when read_symbol would, for as long as args.src has
|
||||
// them. It returns 0 when it has set this.symbol, or 1 when read_symbol must
|
||||
// finish the job (this.bits and this.n_bits hold the bits read so far). In
|
||||
// particular, read_symbol (not read_symbol_fast) returns any error.
|
||||
pri func decoder.read_symbol_fast!(src: base.io_reader, hoff: base.u32) base.u32 {
|
||||
var hoff : base.u32
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32
|
||||
var i : base.u32
|
||||
var e : base.u32
|
||||
var e_bits : base.u32[..= 23]
|
||||
|
||||
hoff = args.hoff
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
while true {
|
||||
i = hoff ~mod+ (bits & 0xFF)
|
||||
if i >= HUFFS_LEN {
|
||||
break
|
||||
}
|
||||
e = this.huffs[i] as base.u32
|
||||
e_bits = e & 15
|
||||
if e_bits <= 8 {
|
||||
if n_bits >= e_bits {
|
||||
this.symbol = e >> 4
|
||||
this.bits = bits >> e_bits
|
||||
this.n_bits = n_bits - e_bits
|
||||
return 0
|
||||
}
|
||||
} else if n_bits > 8 {
|
||||
i = (hoff ~mod+ (e >> 4)) ~mod+ ((bits >> 8) & (((1 as base.u32) << (e_bits - 8)) - 1))
|
||||
if i >= HUFFS_LEN {
|
||||
break
|
||||
}
|
||||
e = this.huffs[i] as base.u32
|
||||
e_bits = (e & 15) + 8
|
||||
if n_bits >= e_bits {
|
||||
this.symbol = e >> 4
|
||||
this.bits = bits >> e_bits
|
||||
this.n_bits = n_bits - e_bits
|
||||
return 0
|
||||
}
|
||||
}
|
||||
if (n_bits >= 24) or (args.src.length() <= 0) {
|
||||
break
|
||||
}
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
}
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
return 1
|
||||
}
|
||||
|
||||
// jump_to_byte_boundary drops the bits up to the next byte boundary and
|
||||
// returns whether they were all zero. Mirrors BrotliJumpToByteBoundary in
|
||||
// bit_reader.h.
|
||||
|
||||
Reference in new issue
Block a user