Commit 6089a8af authored by Jasper Chapman-Black's avatar Jasper Chapman-Black Committed by Commit Bot

SuperSize: Caspian: Disable streaming in WebAssembly web worker

Previously, the JavaScript Web Worker backing the Tiger Viewer would
stream newline-delimited JSON content and render incrementally.

The new WebAssembly web worker made tradeoffs to not support streaming,
but have significantly smaller file sizes and faster load times.

Change-Id: Iba8d73e7e6e1f7e3d97113a8a6942e8443865244
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1933561
Auto-Submit: Jasper Chapman-Black <jaspercb@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#718831}
parent 310fc886
...@@ -64,65 +64,13 @@ class DataFetcher { ...@@ -64,65 +64,13 @@ class DataFetcher {
}); });
} }
/**
* Yields binary chunks as Uint8Arrays. After a complete run, the bytes are
* cached and future calls will yield the cached Uint8Array instead.
*/
async *arrayBufferStream() {
if (this._cache) {
yield this._cache;
return;
}
const response = await this.fetch(this._input);
let result;
// Use streams, if supported, so that we can show in-progress data instead
// of waiting for the entire data file to download. The file can be >100 MB,
// so streams ensure slow connections still see some data.
if (response.body) {
const reader = response.body.getReader();
/** @type {Uint8Array[]} Store received bytes to merge later */
let buffer = [];
/** Total size of received bytes */
let byteSize = 0;
while (true) {
// Read values from the stream
const {done, value} = await reader.read();
if (done) break;
const chunk = new Uint8Array(value, 0, value.length);
yield chunk;
buffer.push(chunk);
byteSize += chunk.length;
}
// We just cache a single typed array to save some memory and make future
// runs consistent with the no streams mode.
result = new Uint8Array(byteSize);
let i = 0;
for (const chunk of buffer) {
result.set(chunk, i);
i += chunk.length;
}
} else {
// In-memory version for browsers without stream support
result = new Uint8Array(await response.arrayBuffer());
yield result;
}
this._cache = result;
}
/** /**
* Outputs a single UInt8Array containing the entire input .size file. * Outputs a single UInt8Array containing the entire input .size file.
*/ */
async loadSizeBuffer() { async loadSizeBuffer() {
// Flush cache. if (!this._cache) {
for await (const bytes of this.arrayBufferStream()) { const response = await this.fetch(this._input);
if (this._controller.signal.aborted) { this._cache = new Uint8Array(await response.arrayBuffer());
throw new DOMException('Request was aborted', 'AbortError');
}
} }
return this._cache; return this._cache;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment