mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-02 20:37:21 +02:00
Prevent unnecessary data copy in ChunkedStream.prototype.onReceiveData
This method is only invoked via `ChunkedStreamManager.prototype.sendRequest`, which currently returns data in `Uint8Array` format (since it potentially combines multiple `ArrayBuffer`s). Hence we end up doing a short-lived, but still completely unnecessary, data copy[1] in `ChunkedStream.prototype.onReceiveData` when handling range requests. In practice this is unlikely to be a big problem by default, given that streaming is used and the (low) value of the `rangeChunkSize` API-option. (However, in custom PDF.js deployments it might affect things more.) Given that no data copy is better than a short lived one, let's fix this small oversight and add non-production `assert`s to keep it working as intended. This way we also improve consistency, since all other streaming and range request methods (see e.g. `BasePDFStream` and related code) only return `ArrayBuffer` data. --- [1] Remember that `new Uint8Array(arrayBuffer)` only creates a view of the underlying `arrayBuffer`, whereas `new Uint8Array(typedArray)` actually creates a copy of the `typedArray`.
This commit is contained in:
parent
01deb085f8
commit
b3cd042ded
@ -70,6 +70,12 @@ class ChunkedStream extends Stream {
|
|||||||
throw new Error(`Bad end offset: ${end}`);
|
throw new Error(`Bad end offset: ${end}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
||||||
|
assert(
|
||||||
|
chunk instanceof ArrayBuffer,
|
||||||
|
"onReceiveData - expected an ArrayBuffer."
|
||||||
|
);
|
||||||
|
}
|
||||||
this.bytes.set(new Uint8Array(chunk), begin);
|
this.bytes.set(new Uint8Array(chunk), begin);
|
||||||
const beginChunk = Math.floor(begin / chunkSize);
|
const beginChunk = Math.floor(begin / chunkSize);
|
||||||
const endChunk = Math.floor((end - 1) / chunkSize) + 1;
|
const endChunk = Math.floor((end - 1) / chunkSize) + 1;
|
||||||
@ -85,6 +91,12 @@ class ChunkedStream extends Stream {
|
|||||||
let position = this.progressiveDataLength;
|
let position = this.progressiveDataLength;
|
||||||
const beginChunk = Math.floor(position / this.chunkSize);
|
const beginChunk = Math.floor(position / this.chunkSize);
|
||||||
|
|
||||||
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
||||||
|
assert(
|
||||||
|
data instanceof ArrayBuffer,
|
||||||
|
"onReceiveProgressiveData - expected an ArrayBuffer."
|
||||||
|
);
|
||||||
|
}
|
||||||
this.bytes.set(new Uint8Array(data), position);
|
this.bytes.set(new Uint8Array(data), position);
|
||||||
position += data.byteLength;
|
position += data.byteLength;
|
||||||
this.progressiveDataLength = position;
|
this.progressiveDataLength = position;
|
||||||
@ -310,7 +322,7 @@ class ChunkedStreamManager {
|
|||||||
if (this.aborted) {
|
if (this.aborted) {
|
||||||
return; // Ignoring any data after abort.
|
return; // Ignoring any data after abort.
|
||||||
}
|
}
|
||||||
this.onReceiveData({ chunk: data, begin });
|
this.onReceiveData({ chunk: data.buffer, begin });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user