Skip to content

QOI: streaming row reader and streaming encoder session - #14

Merged
sdcb merged 6 commits into
mainfrom
streaming/qoi
Nov 23, 2026
Merged

sdcb merged 6 commits into
mainfrom
streaming/qoi

Conversation

@sdcb

@sdcb sdcb commented Nov 23, 2026

Copy link
Copy Markdown
Owner

QOI gets true forward-only streaming read and streaming write on top of the streaming foundation (84b58f2). Abstractions, the facade and AGENTS.md are untouched.

Decoder

  • QoiImageDecoder implements IStreamingImageDecoder; Registration.CreateRowReader = CreateRowReader (concrete method group).
  • Header: 14 bytes; a wrong signature fails as soon as 4 bytes are in. A row wider than an array can hold in the requested format throws InvalidDataException (avoids the base's checked row-size overflow on hostile widths).
  • A row is the unit. QoiDecoder.TryDecodeRow (now also the span decoder's per-row step, so the rows are the same bytes for every PixelFormat) runs on a copy of the reader state over the buffered window, and the copy is kept only when the whole row decoded. A failed attempt waits for twice the bytes (capped at 5 bytes a pixel, the most a row can take), so a byte-at-a-time stream costs a few decodes of each row. Before a row it asks for more input while fewer than twice the previous row's bytes are buffered.
  • Tightly packed RGBA / BGRA rows decode in batches of as many rows as the buffered bytes are sure to hold (5 bytes a pixel): such a batch cannot run out, so no work is discarded.
  • QoiCodec.Reader: Read (throws) and TryRead (returns false) share one loop instantiated over a struct marker (typeof test, Mono-AOT-safe). The throwing instantiation is the old loop: void return, cold throw blocks. A shared bool-returning loop cost the span decode 4-8% (measured), which is why it is shaped like this.
  • The end marker is not checked, as in the span decoder. Non-full-image options (scale, region, orientation) take UseBufferedFallback (IsStreaming == false).

Encoder

  • QoiImageEncoder.Create returns a StreamingEncoderSession. AddFrame writes the header, each WriteRows band is encoded and written as it arrives with the encoder state (index, previous pixel, open run) carried across bands, and Commit writes the end marker. QoiEncoder is split into WriteHeader / WriteRow / WriteEnd, shared with the whole-image path, so the bytes are identical for any banding.

Tests

  • QoiStreamingTests: rows equal TryDecodeInto for 9 widths (1..1100) × alpha/opaque × 10 pixel formats under every feed; ImageMagick-written and hand-assembled streams; runs across rows and a run past the last pixel; fallback options; every cut of a file fails exactly where the span decoder fails (one-byte, random, async-only feeds); malformed headers; read-ahead bound; streaming-encode bytes equal QoiEncoder.Encode for 5 sizes × 5 formats × sRGB/linear × 6 bandings/strides; EncoderEmitsBeforeCommit; 8192×4096 encode and decode each allocate < 1 MiB; an async-only ChunkedStream piped through ReadRowsAsync into the streaming encoder gives back the source file byte for byte.
  • QoiStreamingRobustnessTests (new soak variable): mutated files fed in random pieces, sync and async-only, give exactly TryDecodeInto's rows or throw InvalidDataException exactly when it returns false.
  • I broke the code on purpose to check the tests catch it: no state restore after a failed row, encoder state reset per band, and a wrong batch BGRA swap. Each one fails the tests.

Soaks (all pass):

QOI_STREAM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~QoiStreamingRobustness"
QOI_FUZZ_ITERATIONS=300000 PNM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~QoiPnmRobustness"

For AGENTS.md (I did not edit it): please add the QOI_STREAM_FUZZ_ITERATIONS soak next to the QOI/Netpbm one.

Validation: full suite 7054 tests, 0 failures (559 oracle skips, the same as main). The QOI classes pass under DOTNET_EnableAVX2=0 and DOTNET_EnableHWIntrinsic=0. SimdCodecs.slnx builds with 0 warnings. The wasm smoke test does not exercise QOI, so I did not run it.

Benchmark

QoiPnm.Benchmarks gains a QOI streaming contestant (row reader over a MemoryStream, 16 rows a call / session in 16-row bands). This PR also fixes the baseline's decode binding: 2.0's TryDecodeInto takes 6 parameters, so the old 4-parameter lookup found nothing and decode had no baseline column. baseline 533b03e + matrix all --cases qoi --rounds 31, run from a detached worktree outside the repo. An A/A run (baseline = HEAD) measured 1.00×.

Machine: AMD Ryzen 7 5800X, Windows 10.0.26200 x64, .NET 10.0.15, AVX2 (no AVX-512), 1920×1080 synthetic sources. Other jobs were running on the machine, so run-to-run spread was several percent.

Decode, ms current streaming baseline streaming ÷ current current ÷ baseline
photo-qoi-rgba 10.7 11.4 10.4 1.07 1.03
photo-qoi-rgb 10.8 11.4 10.6 1.06 1.02
screen-qoi-rgba 0.952 1.036 0.940 1.09 1.01
screen-qoi-rgb 0.945 1.042 0.932 1.10 1.01
Encode, ms current current → stream streaming baseline
photo-qoi-rgba 12.7 12.4 12.0 12.5
photo-qoi-rgb 13.9 13.4 13.2 13.5
screen-qoi-rgba 1.695 1.682 1.679 2.039
screen-qoi-rgb 2.687 2.657 2.672 3.047
  • Span decode vs main: geometric mean 1.02× in this run. The earlier runs on the final code measured 0.97-1.01×, inside the noise.
  • Streaming encode vs whole-image encode: 0.97×, which meets the target.
  • Streaming decode vs span decode: 1.08× (photo 1.06-1.07×, screen 1.09-1.10×), over the 1.05× target. Ablation shows the cost comes from calling the reader a band at a time, not from the stream: the span decoder called once per row (PerRow) is as slow as the streaming reader, and copying from the stream costs under 2%. Batching tight RGBA / BGRA rows took streaming decode from 1.15× to 1.08×.
  • Managed allocation per streaming decode is 0.6 KB; per streaming encode it is 0.3 KB.

🤖 Generated with Claude Code

sdcb and others added 6 commits November 23, 2026 19:19
QoiImageDecoder implements IStreamingImageDecoder: CreateRowReader decodes a
forward-only stream a row at a time (sync and async). A row decodes on a copy
of the reader state and is kept only when all its chunks are buffered; a
failed attempt waits for twice the bytes (capped at 5 bytes a pixel), so a
byte-at-a-time stream costs a few decodes per row. Non-full-image options take
the buffered fallback. Rows come from the same per-row step as TryDecodeInto
(QoiDecoder.TryDecodeRow), so they are the same bytes for every PixelFormat.

QoiImageEncoder.Create returns a StreamingEncoderSession: header at AddFrame,
each band encoded and written as it arrives with the encoder state (index,
previous pixel, open run) carried across bands, end marker at Commit. Bytes
equal QoiEncoder.Encode for any banding (shared WriteHeader/WriteRow/WriteEnd).

Tests: QoiStreamingTests (rows vs TryDecodeInto over every feed and format,
ImageMagick-written and hand-assembled streams, every cut, banded encode bytes,
emits before commit, 8192x4096 decode/encode under 1 MiB, async-only pipe into
the encoder) and QoiStreamingRobustnessTests (QOI_STREAM_FUZZ_ITERATIONS).
The QoiPnm benchmark gains a QOI `streaming` contestant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 2.0 TryDecodeInto takes options and a token; the four-parameter lookup found
nothing, so decode had no baseline column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TryRead and Read share one loop instantiated over a struct marker; returning
false from the end checks in the shared loop cost the span decode about 8%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d two rows

The throwing instantiation is the pre-streaming loop; TryRead marks running out
with position -1. The row reader asks for more input while fewer than twice the
previous row's bytes are buffered, so it seldom decodes a partial row twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sdcb
sdcb merged commit 8d37832 into main Nov 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant