mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
perf(deno): batch reads of redirect table records (#13416)
This commit is contained in:
1 parent
6895a9e860
commit
d03f4700e6
3 files changed
+131
-29
No files matched your search
@@ -4,6 +4,7 @@ import { setImmediate } from "node:timers/promises";
|
||||
|
||||
import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js";
|
||||
import { readFileHandleChunks } from "../filesystem/readFileHandleChunks.js";
|
||||
import { readFileHandleRange } from "../filesystem/readFileHandleRange.js";
|
||||
import { artifactExpansionBudgetBytes } from "../domain/artifactExpansionBudget.js";
|
||||
import type {
|
||||
DenoStandaloneEntryMetadata,
|
||||
@@ -215,21 +216,13 @@ export class DenoStandaloneArtifactReader implements ArtifactReader {
|
||||
const section = await parseDenoSection(
|
||||
location.length,
|
||||
async (offset, length) => {
|
||||
const bytes = Buffer.alloc(length);
|
||||
let filled = 0;
|
||||
while (filled < length) {
|
||||
assertArtifactReadActive(signal);
|
||||
const read = await source.handle.read(
|
||||
bytes,
|
||||
filled,
|
||||
length - filled,
|
||||
location.offset + offset + filled,
|
||||
);
|
||||
assertArtifactReadActive(signal);
|
||||
if (read.bytesRead === 0)
|
||||
throw denoFormatFailure("section read was short");
|
||||
filled += read.bytesRead;
|
||||
}
|
||||
const bytes = await readFileHandleRange(
|
||||
source.handle,
|
||||
location.offset + offset,
|
||||
length,
|
||||
() => assertArtifactReadActive(signal),
|
||||
);
|
||||
assertArtifactReadActive(signal);
|
||||
return bytes;
|
||||
},
|
||||
signal,
|
||||
|
||||
@@ -24,6 +24,10 @@ const parseDenoSectionBytes = (bytes: Buffer, signal?: AbortSignal) =>
|
||||
it("parses metadata, tables, remote regions, and embedded configuration", async () => {
|
||||
const original = Buffer.from("export const value = 1;\n");
|
||||
const transpiled = Buffer.from("transpiled\n");
|
||||
const laterSpecifiers = Array.from({ length: 1_024 }, (_, index) => ({
|
||||
id: index + 2,
|
||||
text: `https://example.com/dependency-${index}.ts`,
|
||||
}));
|
||||
const parsed = await parseDenoSectionBytes(
|
||||
buildDenoSection({
|
||||
metadata: {
|
||||
@@ -47,8 +51,12 @@ it("parses metadata, tables, remote regions, and embedded configuration", async
|
||||
specifiers: [
|
||||
{ text: "file:///main.ts", id: 0 },
|
||||
{ text: "https://example.com/mod.ts", id: 1 },
|
||||
...laterSpecifiers,
|
||||
],
|
||||
redirects: [
|
||||
{ from: 0, to: 1 },
|
||||
...laterSpecifiers.map(({ id }) => ({ from: id, to: 1 })),
|
||||
],
|
||||
redirects: [{ from: 0, to: 1 }],
|
||||
remoteModules: [
|
||||
{
|
||||
specifierId: 1,
|
||||
@@ -80,10 +88,13 @@ it("parses metadata, tables, remote regions, and embedded configuration", async
|
||||
caDataByteLength: 3,
|
||||
vfsCaseSensitivity: "insensitive",
|
||||
});
|
||||
expect(JSON.stringify(parsed.metadata)).toContain(secret);
|
||||
expect(parsed.npmSnapshotByteLength).toBe(3);
|
||||
expect(parsed.specifiers).toMatchObject({ count: 2, listing: "listed" });
|
||||
expect(parsed.redirects).toMatchObject({ count: 1, listing: "listed" });
|
||||
expect(parsed.specifiers).toMatchObject({ count: 1_026, listing: "listed" });
|
||||
expect(parsed.redirects).toEqual({
|
||||
count: 1_025,
|
||||
byteLength: 8_204,
|
||||
listing: "listed",
|
||||
});
|
||||
expect(parsed.remoteModules.listing).toBe("listed");
|
||||
expect(parsed.remoteModules.modules[0]).toMatchObject({
|
||||
specifierId: 1,
|
||||
@@ -259,6 +270,78 @@ it("keeps decoded specifiers when another remote id is missing", async () => {
|
||||
expect(parsed.specifierText.has(2)).toBe(false);
|
||||
});
|
||||
|
||||
// Empty snapshots and specifier tables leave only their length words before redirect rows.
|
||||
const REDIRECT_METADATA = Buffer.from('{"entrypoint_key":"main.ts"}');
|
||||
const REDIRECT_ROWS =
|
||||
DENO_FIXTURE_SECTION_MAGIC.length + 8 + REDIRECT_METADATA.length + 8 + 4 + 4;
|
||||
const redirectSection = (sources: readonly number[]) =>
|
||||
buildDenoSection({
|
||||
metadataBytes: REDIRECT_METADATA,
|
||||
specifiers: [],
|
||||
redirects: sources.map((from) => ({ from, to: 0 })),
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
"reports the first malformed redirect despite a short third row (physical EOF %s)",
|
||||
async (physicalEof) => {
|
||||
const parseShort = (second: number) => {
|
||||
const section = redirectSection([1, second, 1]);
|
||||
const acquired = section.subarray(0, REDIRECT_ROWS + 20);
|
||||
return parseDenoSection(
|
||||
physicalEof ? section.length : acquired.length,
|
||||
async (offset, length) => acquired.subarray(offset, offset + length),
|
||||
);
|
||||
};
|
||||
await expect(parseShort(1)).rejects.toMatchObject({
|
||||
reason: "format",
|
||||
message: expect.stringMatching(/duplicate redirect specifier id 1/iu),
|
||||
});
|
||||
await expect(parseShort(2)).rejects.toMatchObject({
|
||||
reason: "format",
|
||||
message: expect.stringMatching(
|
||||
physicalEof ? /section read was short/iu : /overruns the section/iu,
|
||||
),
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps the redirect count allowance and rejects duplicate IDs across batches", async () => {
|
||||
const overCount = redirectSection([]).subarray(0, REDIRECT_ROWS);
|
||||
overCount.writeUInt32LE(100_001, REDIRECT_ROWS - 4);
|
||||
await expect(parseDenoSectionBytes(overCount)).rejects.toMatchObject({
|
||||
reason: "limit",
|
||||
fixedAllowance: { limit: 100_000, unit: "records" },
|
||||
});
|
||||
const sources = Array.from({ length: 1_024 }, (_, index) => index);
|
||||
await expect(
|
||||
parseDenoSectionBytes(redirectSection([...sources, 0])),
|
||||
).rejects.toMatchObject({
|
||||
reason: "format",
|
||||
message: expect.stringMatching(/duplicate redirect specifier id 0/iu),
|
||||
});
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
"retains cancellation during redirect admission (count read %s)",
|
||||
async (duringCount) => {
|
||||
const controller = new AbortController();
|
||||
const reason = Symbol("stop during redirect admission");
|
||||
const section = redirectSection([1, 1]);
|
||||
const bytes = duringCount ? section.subarray(0, REDIRECT_ROWS) : section;
|
||||
const abortAt = duringCount ? REDIRECT_ROWS - 4 : REDIRECT_ROWS;
|
||||
await expect(
|
||||
parseDenoSection(
|
||||
bytes.length,
|
||||
async (offset, length) => {
|
||||
if (offset === abortAt) controller.abort(reason);
|
||||
return bytes.subarray(offset, offset + length);
|
||||
},
|
||||
controller.signal,
|
||||
),
|
||||
).rejects.toMatchObject({ reason: "cancelled", cause: reason });
|
||||
},
|
||||
);
|
||||
|
||||
it("honors cancellation before parsing", async () => {
|
||||
const controller = new AbortController();
|
||||
controller.abort(new Error("stop"));
|
||||
|
||||
@@ -15,6 +15,8 @@ export const DENO_SECTION_MAGIC = Buffer.from("d3n0l4nd", "latin1");
|
||||
const MAX_DENO_METADATA_JSON_BYTES = 8 * 1024 * 1024;
|
||||
export const MAX_DENO_VFS_HEADER_JSON_BYTES = 32 * 1024 * 1024;
|
||||
const MAX_DENO_TABLE_COUNT = 100_000;
|
||||
const REDIRECT_ROW_BYTES = 8;
|
||||
const REDIRECT_BATCH_ROWS = 1_024;
|
||||
export const MAX_DENO_SPECIFIER_TEXT_BYTES = 8 * 1024 * 1024;
|
||||
export const MAX_DENO_REMOTE_BODY_BYTES = 256 * 1024 * 1024;
|
||||
|
||||
@@ -115,6 +117,7 @@ export interface DenoParsedSection {
|
||||
readonly specifierText: ReadonlyMap<number, string | null>;
|
||||
}
|
||||
|
||||
/** Read a section-relative range, returning the acquired prefix at physical EOF. */
|
||||
export type DenoSectionRead = (
|
||||
offset: number,
|
||||
length: number,
|
||||
@@ -236,17 +239,40 @@ export const parseDenoSection = async (
|
||||
assertTableCount(redirectCount, "redirect");
|
||||
const redirectIds = new Set<number>();
|
||||
let redirectListing: DenoListing = "listed";
|
||||
for (let index = 0; index < redirectCount; index += 1) {
|
||||
for (let index = 0; index < redirectCount;) {
|
||||
abortIfNeeded(signal);
|
||||
const from = await u32();
|
||||
const to = await u32();
|
||||
if (redirectIds.has(from))
|
||||
throw denoFormatFailure(
|
||||
`duplicate redirect specifier id ${String(from)}`,
|
||||
);
|
||||
redirectIds.add(from);
|
||||
if (!hasSpecifierText(from) || !hasSpecifierText(to))
|
||||
redirectListing = "unsupported";
|
||||
// A short last batch still admits every complete earlier row, so an earlier duplicate retains precedence.
|
||||
const rows = Math.min(
|
||||
redirectCount - index,
|
||||
REDIRECT_BATCH_ROWS,
|
||||
Math.floor(Number(size - cursor) / REDIRECT_ROW_BYTES),
|
||||
);
|
||||
if (rows === 0) throw denoFormatFailure("length overruns the section");
|
||||
const range = takeRange(BigInt(rows * REDIRECT_ROW_BYTES));
|
||||
const bytes = await read(range.sectionOffset, range.length);
|
||||
abortIfNeeded(signal);
|
||||
if (bytes.length > range.length)
|
||||
throw denoFormatFailure("section read was short");
|
||||
const batch = new BinaryReader(bytes, true, "deno");
|
||||
// Decode the acquired complete rows even if a later row ends at physical EOF.
|
||||
for (
|
||||
let offset = 0;
|
||||
offset <= bytes.length - REDIRECT_ROW_BYTES;
|
||||
offset += REDIRECT_ROW_BYTES
|
||||
) {
|
||||
const from = batch.u32(offset);
|
||||
const to = batch.u32(offset + 4);
|
||||
if (redirectIds.has(from))
|
||||
throw denoFormatFailure(
|
||||
`duplicate redirect specifier id ${String(from)}`,
|
||||
);
|
||||
redirectIds.add(from);
|
||||
if (!hasSpecifierText(from) || !hasSpecifierText(to))
|
||||
redirectListing = "unsupported";
|
||||
}
|
||||
if (bytes.length < range.length)
|
||||
throw denoFormatFailure("section read was short");
|
||||
index += rows;
|
||||
}
|
||||
const redirects: DenoCountedRegion = {
|
||||
count: redirectCount,
|
||||
|
||||
Reference in new issue
Block a user