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,12 +1,17 @@
|
||||
// Main thread of the R4 measurement page (R4.2e). Compiles the module once
|
||||
// Main thread of the R4 measurement page (R4.2e, R4.3a). Compiles the module once
|
||||
// (`WebAssembly.compileStreaming`), routes a picked file as HomeRoute does
|
||||
// (first KiB: PDF magic, JSON object or `.json` name), then runs each import
|
||||
// in a module worker created for it (plan §B.1.5): the compiled Module
|
||||
// posted by structured clone (or, when an engine refuses the clone, the
|
||||
// worker fetches the URL, and the refusal is recorded), the File and the
|
||||
// sheet bundle text posted on `ready`, `terminate()` in every case.
|
||||
// sheet bundle text posted on `ready`, `terminate()` in every case. The
|
||||
// worker's read path is `stream` (R4.3a, default) or `arrayBuffer` (R4.2e),
|
||||
// per call or for the page through `?read=arrayBuffer`. Every `error` and
|
||||
// `messageerror` event of a worker is recorded with its text, the first one
|
||||
// settles the run.
|
||||
//
|
||||
// window.r4Import(fileOrBytes, bundleJson, mode = 'auto' | 'pdf' | 'copy')
|
||||
// window.r4Import(fileOrBytes, bundleJson, mode = 'auto' | 'pdf' | 'copy',
|
||||
// readPath = 'stream' | 'arrayBuffer')
|
||||
// window.r4Proof('ceiling' | 'cap_off', bundleJson, file?)
|
||||
// window.r4LastResult, window.r4Page (CSP violations, long tasks, compile)
|
||||
const WASM_URL = '/pkg/faunosteo_wasm_bg.wasm'
|
||||
@@ -17,6 +22,11 @@ const LIMITS = { pdfBytes: 50 * MiB, copyBytes: 16 * MiB }
|
||||
const TIMEOUTS = { startMs: 10_000, taskMs: 120_000 }
|
||||
const SNIFF_BYTES = 1024
|
||||
const PDF_MAGIC = '%PDF-'
|
||||
const READ_PATHS = ['stream', 'arrayBuffer']
|
||||
const PAGE_READ_PATH =
|
||||
new URLSearchParams(location.search).get('read') === 'arrayBuffer'
|
||||
? 'arrayBuffer'
|
||||
: 'stream'
|
||||
|
||||
const page = {
|
||||
csp: [],
|
||||
@@ -130,6 +140,7 @@ class WorkerRun {
|
||||
this.transfer = null
|
||||
this.cloneError = null
|
||||
this.workerCsp = []
|
||||
this.events = []
|
||||
this.done = new Promise((resolve) => (this.resolve = resolve))
|
||||
}
|
||||
|
||||
@@ -140,10 +151,8 @@ class WorkerRun {
|
||||
return this.settle(this.failure('start', 'worker_unavailable', error))
|
||||
}
|
||||
this.worker.onmessage = (event) => this.onMessage(event.data)
|
||||
this.worker.onerror = (event) =>
|
||||
this.settle(this.failure(this.phase(), 'worker_error', errorText(event)))
|
||||
this.worker.onmessageerror = () =>
|
||||
this.settle(this.failure(this.phase(), 'messageerror', null))
|
||||
this.worker.onerror = (event) => this.onEvent('worker_error', event)
|
||||
this.worker.onmessageerror = (event) => this.onEvent('messageerror', event)
|
||||
this.arm(TIMEOUTS.startMs, 'start')
|
||||
this.postInit()
|
||||
return this.done
|
||||
@@ -153,6 +162,14 @@ class WorkerRun {
|
||||
return this.readyAt === null ? 'start' : 'task'
|
||||
}
|
||||
|
||||
/** Every error / messageerror event kept with its text; the first settles. */
|
||||
onEvent(kind, event) {
|
||||
const text = kind === 'worker_error' ? errorText(event) : event.type
|
||||
const at = Math.round(now() - this.createdAt)
|
||||
this.events.push({ kind, phase: this.phase(), text, atMs: at })
|
||||
this.settle(this.failure(this.phase(), kind, text))
|
||||
}
|
||||
|
||||
failure(phase, kind, error) {
|
||||
const text = typeof error === 'string' ? error : error && describe(error)
|
||||
return { outcome: 'failed', phase, kind, error: text ?? null }
|
||||
@@ -239,6 +256,7 @@ class WorkerRun {
|
||||
cloneError: this.cloneError,
|
||||
readyLinearBytes: this.readyLinearBytes ?? null,
|
||||
workerCsp: this.workerCsp,
|
||||
workerEvents: this.events,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,19 +264,27 @@ class WorkerRun {
|
||||
const asFile = (input) =>
|
||||
input instanceof Blob ? input : new File([input], 'input.pdf')
|
||||
|
||||
async function r4Import(input, bundleJson, mode = 'auto') {
|
||||
async function r4Import(input, bundleJson, mode = 'auto', readPath) {
|
||||
const calledAt = now()
|
||||
const file = asFile(input)
|
||||
const route = mode === 'auto' ? await routeOf(file) : mode
|
||||
const { module } = await window.r4Ready.compiled
|
||||
const task = { type: 'task', route, file, sheet: bundleJson, limits: LIMITS }
|
||||
const path = READ_PATHS.includes(readPath) ? readPath : PAGE_READ_PATH
|
||||
const task = {
|
||||
type: 'task',
|
||||
route,
|
||||
file,
|
||||
sheet: bundleJson,
|
||||
limits: LIMITS,
|
||||
readPath: path,
|
||||
}
|
||||
const run = new WorkerRun({
|
||||
url: IMPORT_WORKER,
|
||||
protocol: 'import',
|
||||
module,
|
||||
task,
|
||||
})
|
||||
const result = { route, ...(await run.start()) }
|
||||
const result = { route, readPath: path, ...(await run.start()) }
|
||||
result.pageMs = now() - calledAt
|
||||
window.r4LastResult = result
|
||||
document.getElementById('status').textContent = JSON.stringify(result)
|
||||
|
||||
@@ -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