mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
perf(n64): stream ROM byte-order normalization (#15179)
Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
This commit is contained in:
2 files changed
+74
-29
No files matched your search
@@ -205,3 +205,40 @@ describe("Nintendo 64 ROM reader", () => {
|
||||
expect(result.metadata?.crc.status).toBe("match");
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["v64", "n64"] as const)(
|
||||
"streams the %s program in bounded chunks and honors cancellation",
|
||||
async (order) => {
|
||||
const program = Buffer.from("12345678".repeat(32 * 1024 + 1));
|
||||
const image = n64RomImage({ order, program });
|
||||
const reader = new N64RomArtifactReader(await write(image.encoded));
|
||||
try {
|
||||
const entries: ArtifactEntry[] = [];
|
||||
for await (const entry of reader.entries()) entries.push(entry);
|
||||
const programEntry = entries.find(
|
||||
(entry) => entry.path === "program.z64",
|
||||
)!;
|
||||
const hash = createHash("sha256");
|
||||
let count = 0;
|
||||
for await (const chunk of await reader.open(programEntry)) {
|
||||
hash.update(chunk);
|
||||
count++;
|
||||
}
|
||||
expect(count).toBeGreaterThan(1);
|
||||
expect(hash.digest("hex")).toBe(digest(image.normalized));
|
||||
|
||||
const controller = new AbortController();
|
||||
const stream = await reader.open(programEntry, controller.signal);
|
||||
const iterator = stream[Symbol.asyncIterator]();
|
||||
const first = await iterator.next();
|
||||
expect(first.done).toBe(false);
|
||||
expect(first.value.length).toBeLessThan(image.normalized.length);
|
||||
controller.abort("stop after the first chunk");
|
||||
await expect(iterator.next()).rejects.toMatchObject({
|
||||
reason: "cancelled",
|
||||
});
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -35,11 +35,14 @@ import {
|
||||
type PositionalArtifactEntry,
|
||||
} from "./PositionalArtifactReader.js";
|
||||
|
||||
// A 64 KiB read holds whole v64 pairs and n64 words.
|
||||
const NORMALIZED_CHUNK_BYTES = 64 * 1024;
|
||||
|
||||
/**
|
||||
* Nintendo 64 cartridge ROM. Header facts come from the domain parser.
|
||||
* `header.bin`, `ipl3.bin` and `program.z64` are stored ranges when the dump
|
||||
* is already big-endian. v64 and n64 dumps serve those same ranges after one
|
||||
* byte-order swap so every entry is big-endian. Nothing is executed or
|
||||
* is already big-endian. v64 and n64 dumps serve those same ranges after a
|
||||
* chunked byte-order swap so every entry is big-endian. Nothing is executed or
|
||||
* patched. A CRC mismatch stays a limitation.
|
||||
*/
|
||||
export class N64RomArtifactReader extends PositionalArtifactReader<N64RomMetadata> {
|
||||
@@ -149,8 +152,8 @@ export class N64RomArtifactReader extends PositionalArtifactReader<N64RomMetadat
|
||||
|
||||
/**
|
||||
* Big-endian bytes for a v64 or n64 member. z64 members stay on the stored
|
||||
* range. Header, IPL3 and `program.z64` each use one {@link swapN64Bytes}
|
||||
* of the stored slice.
|
||||
* range. Header, IPL3 and `program.z64` each normalize their stored slice
|
||||
* in aligned chunks.
|
||||
*/
|
||||
protected override entrySource(
|
||||
entry: PositionalArtifactEntry,
|
||||
@@ -170,33 +173,38 @@ export class N64RomArtifactReader extends PositionalArtifactReader<N64RomMetadat
|
||||
onStateChange: (state: DecodedMemberReadState) => void,
|
||||
): AsyncIterable<Buffer> {
|
||||
const { signal, cancellationFailure } = member;
|
||||
onStateChange("pending");
|
||||
let normalized: Uint8Array;
|
||||
try {
|
||||
const end = entry.byteOffset + entry.declaredSize;
|
||||
for (let position = entry.byteOffset; position < end;) {
|
||||
const length = Math.min(NORMALIZED_CHUNK_BYTES, end - position);
|
||||
onStateChange("pending");
|
||||
let normalized: Uint8Array;
|
||||
try {
|
||||
if (signal.aborted) throw cancellationFailure(signal.reason);
|
||||
const stored = await readArtifactFileBytes(
|
||||
N64_FORMAT,
|
||||
handle,
|
||||
position,
|
||||
length,
|
||||
initial.size,
|
||||
signal,
|
||||
);
|
||||
normalized = swapN64Bytes(stored, this.#order);
|
||||
onStateChange("idle");
|
||||
} catch (cause: unknown) {
|
||||
onStateChange("failed");
|
||||
if (artifactReadCancelledBy(cause, signal))
|
||||
throw cancellationFailure(signal.reason);
|
||||
if (cause instanceof ParseFailureError)
|
||||
throw artifactParseFailure(cause, signal);
|
||||
throw cause;
|
||||
}
|
||||
if (signal.aborted) throw cancellationFailure(signal.reason);
|
||||
const stored = await readArtifactFileBytes(
|
||||
N64_FORMAT,
|
||||
handle,
|
||||
entry.byteOffset,
|
||||
entry.declaredSize,
|
||||
initial.size,
|
||||
signal,
|
||||
position += length;
|
||||
yield Buffer.from(
|
||||
normalized.buffer,
|
||||
normalized.byteOffset,
|
||||
normalized.length,
|
||||
);
|
||||
normalized = swapN64Bytes(stored, this.#order);
|
||||
onStateChange("idle");
|
||||
} catch (cause) {
|
||||
onStateChange("failed");
|
||||
if (artifactReadCancelledBy(cause, signal))
|
||||
throw cancellationFailure(signal.reason);
|
||||
if (cause instanceof ParseFailureError)
|
||||
throw artifactParseFailure(cause, signal);
|
||||
throw cause;
|
||||
}
|
||||
if (signal.aborted) throw cancellationFailure(signal.reason);
|
||||
yield Buffer.from(
|
||||
normalized.buffer,
|
||||
normalized.byteOffset,
|
||||
normalized.length,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user