test(tests): R4.3a browser re-measure of large files, leak series, WebKit repetition (stream sink, BYOB)
Streaming into FileBytes removes the file's JavaScript copy in Chromium and Firefox (50 MiB: 65 and 67 MiB summed growth against 56 and 79 for the TypeScript); WebKit keeps about 180 MiB for any file read inside a worker (272 against 76), a main-thread BYOB read feeding the worker's sink measured at 106. Corpus 666/678 on the stream path. Leak series (3 engines × 3 documents × 3 repetitions): Chromium ≤ 0.5 MiB over the last 5, Firefox within ±3 (idle only), WebKit copy +13 then level (12-import series −2.3). 20 dense imports in WebKit: no worker error event.
This commit is contained in:
1 parent
8924d50edf
commit
49591c53cd
14 files changed
+3002
-387
No files matched your search
@@ -1,11 +1,23 @@
|
||||
// Import worker of the R4 measurement page (R4.2e), the shape plan §B.1.5
|
||||
// gives the application at R4.3: a module worker created for one import,
|
||||
// the glue imported statically, the module received from the main thread
|
||||
// (structured clone) or fetched from its URL, the File read here, one
|
||||
// SheetHandle, `import_pdf` or the strict decode then `draft.restore`, one
|
||||
// result message. The main thread terminates it in every case.
|
||||
import init, { SheetHandle } from '/pkg/faunosteo_wasm.js'
|
||||
// Import worker of the R4 measurement page (R4.2e, R4.3a), the shape plan
|
||||
// §B.1.5 gives the application at R4.3: a module worker created for one
|
||||
// import, the glue imported statically, the module received from the main
|
||||
// thread (structured clone) or fetched from its URL, the File read here, one
|
||||
// SheetHandle, one result message. The main thread terminates it in every
|
||||
// case. Two read paths, chosen per task (`readPath`):
|
||||
// stream (default, R4.3a) `file.stream()` pushed chunk by chunk into
|
||||
// a `FileBytes` sink in linear memory, then `import_pdf_bytes`
|
||||
// or `restore_copy_bytes` (the core decodes the copy): the
|
||||
// file lives once, in linear memory. A BYOB reader reads into
|
||||
// one reused 1 MiB buffer (no garbage chunk per read); a
|
||||
// stream that refuses BYOB is read with its default reader,
|
||||
// each chunk dropped once pushed; without `stream` the file
|
||||
// is read whole and pushed once. `readVia` records which;
|
||||
// arrayBuffer (R4.2e, kept to measure both) one ArrayBuffer of the file,
|
||||
// `import_pdf` (the glue copies it into linear memory) or the
|
||||
// strict TextDecoder then `draft.restore`.
|
||||
import init, { FileBytes, SheetHandle } from '/pkg/faunosteo_wasm.js'
|
||||
|
||||
const BYOB_BUFFER_BYTES = 1024 * 1024
|
||||
const cspViolations = []
|
||||
let memory = null
|
||||
|
||||
@@ -50,7 +62,15 @@ async function start({ module, url }) {
|
||||
/** A refusal decided here, in the envelope's shape. */
|
||||
const refusal = (kind, detail) => JSON.stringify({ ok: false, kind, detail })
|
||||
|
||||
/** `import_pdf`, or the strict UTF-8 decode then `draft.restore`. */
|
||||
/** The size pre-check, before any read: the kinds the reader would give. */
|
||||
function precheck(route, file, limits) {
|
||||
const limit = route === 'copy' ? limits.copyBytes : limits.pdfBytes
|
||||
if (file.size <= limit) return null
|
||||
const kind = route === 'copy' ? 'invalid' : 'invalid_pdf'
|
||||
return refusal(kind, `file size ${file.size} > ${limit}, not read`)
|
||||
}
|
||||
|
||||
/** R4.2e: `import_pdf`, or the strict UTF-8 decode then `draft.restore`. */
|
||||
function runImport(handle, route, bytes) {
|
||||
if (route === 'pdf') return handle.import_pdf(new Uint8Array(bytes))
|
||||
let text
|
||||
@@ -63,6 +83,144 @@ function runImport(handle, route, bytes) {
|
||||
return handle.call('draft.restore', input)
|
||||
}
|
||||
|
||||
/** R4.3a: the sink consumed by the import (the wrapper is invalidated). */
|
||||
function runSink(handle, route, sink) {
|
||||
if (route === 'pdf') return handle.import_pdf_bytes(sink)
|
||||
return handle.restore_copy_bytes(sink)
|
||||
}
|
||||
|
||||
/** The R4.2e read: one ArrayBuffer, alive until the import returns. */
|
||||
async function readWhole(file) {
|
||||
let buffer
|
||||
try {
|
||||
buffer = await file.arrayBuffer()
|
||||
} catch (error) {
|
||||
return { kind: 'read', error }
|
||||
}
|
||||
const size = buffer.byteLength
|
||||
return {
|
||||
facts: { readVia: 'arrayBuffer', chunks: 1, maxChunkBytes: size },
|
||||
run: (handle, route) => runImport(handle, route, buffer),
|
||||
release: () => undefined,
|
||||
}
|
||||
}
|
||||
|
||||
/** A read's result, or `{ error }` when the browser cannot read the file. */
|
||||
const settleRead = (pending) => pending.catch((error) => ({ error }))
|
||||
const ignore = () => undefined
|
||||
|
||||
/** BYOB when the stream is a byte stream, else its default reader. */
|
||||
function byobReader(stream) {
|
||||
try {
|
||||
return stream.getReader({ mode: 'byob' })
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The stream's chunks: BYOB reads into one buffer, recycled after each push
|
||||
* (its bytes were copied into the sink); else the stream's own chunks.
|
||||
*/
|
||||
function chunkSource(stream) {
|
||||
const byob = byobReader(stream)
|
||||
if (!byob) {
|
||||
const reader = stream.getReader()
|
||||
return {
|
||||
via: 'stream-default',
|
||||
next: () => settleRead(reader.read()),
|
||||
recycle: ignore,
|
||||
cancel: () => reader.cancel().catch(ignore),
|
||||
}
|
||||
}
|
||||
let buffer = new ArrayBuffer(BYOB_BUFFER_BYTES)
|
||||
return {
|
||||
via: 'stream-byob',
|
||||
next: () => settleRead(byob.read(new Uint8Array(buffer))),
|
||||
recycle: (view) => (buffer = view.buffer),
|
||||
cancel: () => byob.cancel().catch(ignore),
|
||||
}
|
||||
}
|
||||
|
||||
/** Pushes one chunk; `{ kind, error }` when refused or when it throws. */
|
||||
function pushChunk(sink, chunk, facts) {
|
||||
facts.chunks += 1
|
||||
facts.maxChunkBytes = Math.max(facts.maxChunkBytes, chunk.byteLength)
|
||||
try {
|
||||
if (sink.push(chunk)) return null
|
||||
} catch (error) {
|
||||
return { kind: kindOf(error), error }
|
||||
}
|
||||
return { kind: 'read', error: new Error('more bytes than file.size') }
|
||||
}
|
||||
|
||||
/**
|
||||
* `file.stream()` into the sink, chunk by chunk (no chunk list, no
|
||||
* concatenation), so the whole file exists only in the sink.
|
||||
*/
|
||||
async function streamInto(file, sink, facts) {
|
||||
let source
|
||||
try {
|
||||
source = chunkSource(file.stream())
|
||||
} catch (error) {
|
||||
return { kind: 'read', error }
|
||||
}
|
||||
facts.readVia = source.via
|
||||
for (;;) {
|
||||
let next = await source.next()
|
||||
if (next.error) return { kind: 'read', error: next.error }
|
||||
if (next.done) return null
|
||||
const refused = pushChunk(sink, next.value, facts)
|
||||
source.recycle(next.value)
|
||||
next = null // the chunk is unreachable before the next read suspends
|
||||
if (refused) {
|
||||
source.cancel()
|
||||
return refused
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** No `file.stream()`: the file read whole, pushed once, then dropped. */
|
||||
async function bufferInto(file, sink, facts) {
|
||||
facts.readVia = 'arrayBuffer-fallback'
|
||||
let buffer
|
||||
try {
|
||||
buffer = await file.arrayBuffer()
|
||||
} catch (error) {
|
||||
return { kind: 'read', error }
|
||||
}
|
||||
return pushChunk(sink, new Uint8Array(buffer), facts)
|
||||
}
|
||||
|
||||
/** The file gathered in a `FileBytes` sink sized from `file.size`. */
|
||||
async function readStreamed(file) {
|
||||
let sink
|
||||
try {
|
||||
sink = new FileBytes(file.size)
|
||||
} catch (error) {
|
||||
return { kind: kindOf(error), error }
|
||||
}
|
||||
const facts = { readVia: 'stream', chunks: 0, maxChunkBytes: 0 }
|
||||
const fill = typeof file.stream === 'function' ? streamInto : bufferInto
|
||||
const failure = (await fill(file, sink, facts)) ?? shortRead(file, sink)
|
||||
if (failure) {
|
||||
sink.free()
|
||||
return failure
|
||||
}
|
||||
return {
|
||||
facts,
|
||||
run: (handle, route) => runSink(handle, route, sink),
|
||||
release: () => sink.free(),
|
||||
}
|
||||
}
|
||||
|
||||
/** A read that ended before `file.size`: a read failure, never a PDF. */
|
||||
function shortRead(file, sink) {
|
||||
if (sink.len() === file.size) return null
|
||||
const text = `read ${sink.len()} of ${file.size} bytes`
|
||||
return { kind: 'read', error: new Error(text) }
|
||||
}
|
||||
|
||||
/** One step whose throwable becomes a `failed` message of its own kind. */
|
||||
async function step(kind, work) {
|
||||
try {
|
||||
@@ -73,23 +231,22 @@ async function step(kind, work) {
|
||||
}
|
||||
}
|
||||
|
||||
async function task({ route, file, sheet, limits }) {
|
||||
async function task({ route, file, sheet, limits, readPath = 'stream' }) {
|
||||
const startedAt = performance.now()
|
||||
const limit = route === 'copy' ? limits.copyBytes : limits.pdfBytes
|
||||
if (file.size > limit) {
|
||||
const out = refusal('file_too_large', `${file.size} > ${limit}`)
|
||||
return post({ type: 'result', out, diag: { precheck: true } })
|
||||
const refused = precheck(route, file, limits)
|
||||
if (refused) {
|
||||
const diag = { precheck: true, readPath }
|
||||
return post({ type: 'result', out: refused, diag })
|
||||
}
|
||||
const bytes = await step('read', () => file.arrayBuffer())
|
||||
if (!bytes.ok) return
|
||||
const read = readPath === 'arrayBuffer' ? readWhole : readStreamed
|
||||
const bytes = await read(file)
|
||||
if (bytes.error) return fail('task', bytes.kind, bytes.error)
|
||||
const readAt = performance.now()
|
||||
const handle = await step('sheet', () => new SheetHandle(sheet))
|
||||
if (!handle.ok) return
|
||||
if (!handle.ok) return bytes.release()
|
||||
const sheetAt = performance.now()
|
||||
const linearBeforeRun = linearBytes()
|
||||
const out = await step(null, () =>
|
||||
runImport(handle.value, route, bytes.value),
|
||||
)
|
||||
const out = await step(null, () => bytes.run(handle.value, route))
|
||||
if (!out.ok) return
|
||||
const doneAt = performance.now()
|
||||
const diag = {
|
||||
@@ -99,6 +256,8 @@ async function task({ route, file, sheet, limits }) {
|
||||
runMs: doneAt - sheetAt,
|
||||
linearBeforeRun,
|
||||
linearBytes: linearBytes(),
|
||||
readPath,
|
||||
...bytes.facts,
|
||||
}
|
||||
post({ type: 'result', out: out.value, diag })
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user