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:
Claude committed 2026-10-27 12:10:41 +00:00
1 parent 4dff581dd0
commit 85fd6770d5
4 files changed
+1286 -122

No files matched your search

+24 -9
View File
@@ -55,8 +55,9 @@ Without the large-window quirk and without attached dictionaries (see the
window size plus some slack. The slack
mirrors the C code's `kRingBufferWriteAheadSlack`, which its fast paths use to
write (and discard) a few bytes past the ring buffer's end. This
implementation does not yet have such fast paths, but it still reserves the
slack, so that adding them later will not change the `workbuf_len` API. A
implementation's fast paths (see below) do not write past the ring buffer's
end, but it still reserves the slack, so that adding such fast paths later
will not change the `workbuf_len` API. A
workbuf shorter than the stream's window size plus 542 bytes is rejected with
`base."#bad workbuf length"`.
@@ -138,12 +139,25 @@ invariants for memory safety but Wuffs requires explicit bounds checks.
- The bit reader reads input bytes only when it needs them (like the C code's
"safe" `BrotliSafeReadBits` and `SafeReadSymbol` functions), so that
`transform_io` can suspend at any point. There is no "fast path" (like the C
code's `ProcessCommands`, as opposed to `SafeProcessCommands`). Both paths
produce the same output and errors.
- LZ77 copies and dictionary words are written to the ring buffer one byte at
a time, with ring buffer events (the C code's `WriteRingBuffer` and
`WrapRingBuffer` calls) at the same positions as the C code.
`transform_io` can suspend at any point. Between reads, fewer than 8 bits
are buffered. The fast path, `process_commands_fast` (like the C code's
`ProcessCommands`, as opposed to `SafeProcessCommands`), is not a coroutine.
It reads ahead (up to 8 bytes at a time) into a 64-bit bit buffer while the
input has enough bytes, and when it returns to `process_commands` (at a
block switch, a ring buffer event, a dictionary reference or when the input
runs low), it gives back the whole bytes left in that buffer, which restores
the "fewer than 8 bits" state exactly. `process_commands` tracks where it is
within a command (`cmd_state`), so that either can pick up where the other
left off. Within `process_commands`, `read_symbol_fast` and `read_bits_fast`
(which are not coroutines either) read the same bytes at the same times as
the coroutines `read_symbol` and `read_bits`, which finish the job when the
input runs out. All paths produce the same output and errors.
- Dictionary words, and LZ77 copies that wrap around the physical ring buffer
or reach the C code's (virtual) ring buffer size, are written to the ring
buffer one byte at a time, with ring buffer events (the C code's
`WriteRingBuffer` and `WrapRingBuffer` calls) at the same positions as the
C code. Other LZ77 copies are written in chunks of at most the copy distance
(so that each chunk's source bytes are final), which is equivalent.
- On error, the Wuffs decoder may have already written more output to `dst`
than libbrotlidec would have (libbrotlidec does not flush on error), but
what both have written agrees.
@@ -153,7 +167,8 @@ invariants for memory safety but Wuffs requires explicit bounds checks.
- Metadata callbacks (metadata blocks are currently skipped). See the
"Metadata Blocks" section below.
- Fast paths.
- More fast paths, for example for dictionary words, for copies that wrap
around the ring buffer and for block switches.
- Compound dictionary matches at encoder qualities 10 and 11 (see the
"Encoder" section below).