A streamed IPS patch built its target as the source arrived: a second
buffer the size of the ROM, taken at open, every byte of the source
copied into it, then the records laid over it, and the source freed.
The target of an IPS patch is the source with the records on it, and
the source is the content buffer the load already owns.
patch_stream_finish_into() finishes a stream whose source the caller
owns (malloc'd, given to patch_stream_set_source()): an IPS patch puts
its records on that buffer in place, growing it with realloc() only
when the target is longer, and any other format hands back its target
in the buffer's place. On failure the buffer is still the unpatched
source, as before. Fed in place, IPS copies nothing while the source
arrives; a chunk fed from elsewhere copies what came before through to
the target and carries on as before. The target is no longer taken at
open but by the first copy-through, so the in-place path never takes
one. The content load finishes its patch with finish_into().
A 16 MiB ROM with a 2000-record IPS patch, first touch: 6.7 ms ->
0.05 ms to apply, and 16 MiB less heap during the load.
The patch_stream test finishes every in-place and fallback comparison
with finish_into() on a buffer of its own, and checks a 4 MiB IPS
source is patched where it lies without being moved or copied. Leaving
the grown tail unzeroed, skipping the records in place or in the
catch-up, copying nothing in the catch-up, or copying through while fed
in place each fail it.
The content load streams a soft patch alongside the read: each span the
transfer fills is fed to the applier as it arrives. BPS and xdelta may
copy from anywhere in the source, so both kept their own growing copy of
every byte fed (BPS in src_buf, xdelta in own_src), and UPS kept the
part it had not consumed yet in its carry. The transfer buffer already
holds the whole source in one allocation that never moves, and stays
alive, detached as the content, until the patch is finished - so that
copy is a second ROM in memory, written once and read back.
patch_stream_set_source() (and vcdiff_stream_set_source() under it)
tells the stream where the source it is about to be fed lies. Chunks
fed from there are read in place: no copy, no realloc as it grows, no
memmove of the UPS carry. A chunk fed from anywhere else falls back to
the old copy, starting with what came before it, so a caller that does
not keep the source whole still gets the right answer. IPS keeps no
source and is unchanged. content_file_pump_source() sets the transfer
buffer as the source.
A 32 MiB BPS hack patch: 32 MiB less heap during the load, and
143 -> 137 ms to apply.
The patch_stream test feeds each comparison as a copy, in place, or in
place then from another buffer (freed at once, so ASan catches a stream
still reading it), and checks with glibc's heap counters that a 4 MiB
BPS source and the xdelta source are no longer copied. Dropping the
bytes the fallback copies, restarting the UPS carry at 0, reading the
wrong byte in place, or ignoring the setter each fail it.
Running the streaming appliers against a real ROM through a real build
turned up a claim of mine that was wrong. The commit that introduced
streaming asserted that a deliberately corrupt patch would leave the
content unpatched, and offered a truncated .ips as the check. It does
not, and the check was the one format where the claim does not hold.
BPS and UPS carry checksums, and xdelta is structured enough that
damage almost always violates it, so for those three a damaged patch is
refused: finish() fails, the caller keeps the unpatched buffer, and the
content runs unpatched. That was confirmed end to end - a bit-flipped
and a truncated .xdelta each had the streamed attempt refuse, the
whole-buffer pass refuse after it, and the content load with the
source ROM's own checksum.
IPS has no checksum, no length and no framing beyond a terminator, so a
truncated one is a shorter valid one as far as the format is concerned.
Both appliers apply its intact leading records and produce a partially
patched target. That is not a regression and not a decision taken in
the streaming appliers: the whole-buffer applier has always done it,
and the two were confirmed to produce byte-identical output for exactly
this input. It is a property of the format.
Write both down - in the header, where a caller decides what to do
about a failed patch, and in the test, which deliberately does not
assert refusal because the contract that holds for every format is the
weaker one: whatever the whole-buffer applier does with a patch, the
streaming one does too.
No functional change.
The other three patch formats advance as the content arrives; xdelta
was the exception, excluded from the streaming path because the vendored
decoder's interface was not shaped for it. That reason is gone: the
VCDIFF decoder in libretro-common is ours, so it can be given the shape
the load path needs.
A VCDIFF window names the source segment it requires, which makes the
suspend boundary the window rather than the instruction: a window
decodes as soon as its segment has been fed and waits otherwise, with
the cursor left on its header so the retry starts clean. Source bytes
are retained in full, because a window may name any part of the source
however far the stream has got - as with BPS, the gain is overlapping
the decode with the read, not a smaller footprint.
The streaming form is now the implementation and the whole-buffer entry
point is a wrapper over it, so there is one decoder rather than two
that must agree. That wrapper points at the caller's buffer instead of
copying it, since it decodes against multi-megabyte ROMs and the copy
would cost more than the decode; the incremental form owns its copy
because it has no choice.
finish() refuses a stream fed less than the source length declared at
open. A decoder cannot tell a truncated feed from a genuinely shorter
source, and would otherwise return a target confidently built from a
partial one - the same failure the other appliers guard against.
Verified: the whole-buffer path is unchanged, still 3013 differential
cases against the reference implementation with zero mismatches. The
streamed path was checked against its own whole-buffer output over 240
decodes spanning chunk sizes from one byte to a megabyte, across
lightly edited, wholly unrelated, and resized targets, with zero
differences; a deliberately short feed is refused. Clean under address
and undefined-behaviour sanitizers, and both patch_stream.c and the
decoder compile under -std=c89 -pedantic -Wall -Wextra with and
without HAVE_XDELTA.
The notification differed depending on which path applied the patch.
apply_patch_content reports the patch's file name - "Applying patch:
Mario.ips" - but the streamed path was passing the format instead, so
the same patch announced itself as "Applying patch: IPS" purely because
the content happened to take the streaming route. Resolve the patch's
path out of patch_content_stream_open rather than its format name, and
report the base name exactly as the whole-buffer path does; the format
is still what the log line names, which is where it is actually useful.
The pump also kept feeding a stream that had already failed. A
malformed patch or a failed allocation puts the applier into a state it
cannot leave, and finish will refuse it, but the load still has the
rest of the content to deliver - so every remaining span was being
pushed through an applier that could only keep failing, retrying the
allocation that already did not work for each one. Expose the state and
stop feeding when it is set. The load itself still completes, finish
still refuses, and the ordinary whole-buffer pass still patches the
finished buffer, so behaviour is unchanged; only the wasted work goes.
Verified: the query reports a fresh stream and a fully fed one as
healthy, a completed finish still succeeds, and NULL counts as dead so
callers need no separate check. The differential suite is unaffected
(300000 comparisons, zero mismatches) and the short-feed guard still
rejects partial feeds and accepts complete ones. Compiles under
-std=c89 -pedantic -Wall -Wextra, and task_content.c with and without
each of HAVE_PATCH and HAVE_COMPRESSION.
Soft patching currently runs as a separate pass: content_file_load_into
_memory reads (and, for compressed content, inflates) the whole ROM
into a buffer, and only then does patch_content walk it end to end.
The data_transfer spine already makes the load itself sliceable across
task ticks - the comment on the entry-source path says as much - but a
whole-buffer applier cannot participate, because it needs every byte
before it can start.
Add incremental counterparts to the three appliers in task_patch.c.
They consume the source as consecutive chunks, so the patch advances on
the same tick that produced the bytes, and load, decompression and
patching interleave instead of running as serial passes over the same
data. Output is byte-identical to the whole-buffer form and independent
of where the chunk boundaries fall.
Residency differs by format, because they differ in how they address
the source. IPS reads it strictly sequentially and retains nothing; UPS
consumes it in lockstep with the target and retains only a small
rolling carry; BPS has to retain it in full, because SourceCopy seeks
arbitrarily - there the gain is the overlap with the load, not memory.
IPS and UPS run as straight state machines; BPS attempts each command
and, when it would read source past the fed frontier, rolls the command
back whole and retries on the next feed, which is what makes its output
chunk-boundary-independent.
Nothing calls this yet, so no build system is wired and no existing
binary changes; the first consumer will add it.
The harness in tasks/test holds the whole-buffer logic as an oracle and
compares the two across random valid patches at chunk sizes from 1 byte
to 1 MB, with a deliberately edge-heavy size distribution. Streaming
appliers have failure modes the whole-buffer form cannot have, because
the whole-buffer form always has everything: an empty source (feed()
never called), a source ending short of its declared length, and chunk
boundaries landing inside a patch command. The first of those caught a
real bug during development - an empty source left the patch entirely
uninterpreted - which is why the case is now generated explicitly.
Verified: 300000 comparisons (280000 IPS/UPS, 20000 BPS) with zero
mismatches, clean under ASan and UBSan; the module compiles under
-std=c89 -pedantic -Wall -Wextra with no diagnostics. Separately
exercised against the real data_transfer source-mode spine - a producer
callback standing in for an inflating archive entry, iterate() driven
with a per-tick byte budget - for 3200 loads with zero mismatches,
confirming the intended integration shape works before any of it is
wired up.