From 25a1a9d28fd83b1d41a273af0f0de0ce0841565e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 18 Oct 2023 15:30:31 +0200 Subject: [PATCH] Reduce unnecessary type conversion in `writeStream` Currently we're unnecessarily converting data between strings and typed-arrays, when dealing with compressible data, in the `writeStream` function. Note how we're *first* getting a string-representation of the stream, which involves converting the underlying typed-array into a string, only to immediately convert this back into a typed-array. This seems completely unnecessary, and is easy enough to avoid, and we'll now only do a *single* type-conversion in this function. --- src/core/writer.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/writer.js b/src/core/writer.js index 26db83cd4..70560ea5b 100644 --- a/src/core/writer.js +++ b/src/core/writer.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { bytesToString, info, stringToBytes, warn } from "../shared/util.js"; +import { bytesToString, info, warn } from "../shared/util.js"; import { Dict, isName, Name, Ref } from "./primitives.js"; import { escapePDFName, @@ -48,7 +48,7 @@ async function writeDict(dict, buffer, transform) { } async function writeStream(stream, buffer, transform) { - let string = stream.getString(); + let bytes = stream.getBytes(); const { dict } = stream; const [filter, params] = await Promise.all([ @@ -67,18 +67,17 @@ async function writeStream(stream, buffer, transform) { if ( typeof CompressionStream !== "undefined" && - (string.length >= MIN_LENGTH_FOR_COMPRESSING || isFilterZeroFlateDecode) + (bytes.length >= MIN_LENGTH_FOR_COMPRESSING || isFilterZeroFlateDecode) ) { try { - const byteArray = stringToBytes(string); const cs = new CompressionStream("deflate"); const writer = cs.writable.getWriter(); - writer.write(byteArray); + writer.write(bytes); writer.close(); // Response::text doesn't return the correct data. const buf = await new Response(cs.readable).arrayBuffer(); - string = bytesToString(new Uint8Array(buf)); + bytes = new Uint8Array(buf); let newFilter, newParams; if (!filter) { @@ -104,6 +103,7 @@ async function writeStream(stream, buffer, transform) { } } + let string = bytesToString(bytes); if (transform) { string = transform.encryptString(string); }