Reduce allocations when using Map.prototype.getOrInsert() with Arrays

Change all these cases to use `Map.prototype.getOrInsertComputed()` instead, in combination with a helper function for creating the `Array`s (similar to the previous patch).
This commit is contained in:
Jonas Jenwald 2026-02-23 18:49:54 +01:00
parent 2e07715c9d
commit 0d4e587a5f
10 changed files with 27 additions and 7 deletions

View File

@ -18,6 +18,7 @@ import {
assert,
BaseException,
hexNumbers,
makeArr,
objectSize,
stringToPDFString,
Util,
@ -669,7 +670,9 @@ function getNewAnnotationsMap(annotationStorage) {
if (!key.startsWith(AnnotationEditorPrefix)) {
continue;
}
newAnnotationsByPage.getOrInsert(value.pageIndex, []).push(value);
newAnnotationsByPage
.getOrInsertComputed(value.pageIndex, makeArr)
.push(value);
}
return newAnnotationsByPage.size > 0 ? newAnnotationsByPage : null;
}

View File

@ -20,6 +20,7 @@ import {
info,
InvalidPDFException,
isArrayEqual,
makeArr,
objectSize,
PageActionEventType,
RenderingIntentFlag,
@ -1892,7 +1893,7 @@ class PDFDocument {
orphanFields.put(fieldRef, parentRef);
}
promises.getOrInsert(name, []).push(
promises.getOrInsertComputed(name, makeArr).push(
AnnotationFactory.create(
xref,
fieldRef,

View File

@ -15,6 +15,7 @@
import {
AnnotationPrefix,
makeArr,
stringToPDFString,
stringToUTF8String,
warn,
@ -450,7 +451,7 @@ class StructTreeRoot {
for (const element of elements) {
if (element.structTreeParentId) {
const id = parseInt(element.structTreeParentId.split("_mc")[1], 10);
idToElements.getOrInsert(id, []).push(element);
idToElements.getOrInsertComputed(id, makeArr).push(element);
}
}

View File

@ -24,10 +24,10 @@ import {
$resolvePrototypes,
$root,
} from "./symbol_utils.js";
import { makeArr, warn } from "../../shared/util.js";
import { NamespaceSetUp } from "./setup.js";
import { Template } from "./template.js";
import { UnknownNamespace } from "./unknown.js";
import { warn } from "../../shared/util.js";
import { XFAObject } from "./xfa_object.js";
class Root extends XFAObject {
@ -166,7 +166,9 @@ class Builder {
_addNamespacePrefix(prefixes) {
for (const { prefix, value } of prefixes) {
const namespace = this._searchNamespace(value);
this._namespacePrefixes.getOrInsert(prefix, []).push(namespace);
this._namespacePrefixes
.getOrInsertComputed(prefix, makeArr)
.push(namespace);
}
}

View File

@ -36,6 +36,7 @@ import {
AnnotationType,
FeatureTest,
LINE_FACTOR,
makeArr,
shadow,
unreachable,
Util,
@ -3877,7 +3878,9 @@ class AnnotationLayer {
this.#elements.push(element);
if (data.popupRef) {
popupToElements.getOrInsert(data.popupRef, []).push(element);
popupToElements
.getOrInsertComputed(data.popupRef, makeArr)
.push(element);
}
}

View File

@ -33,6 +33,7 @@ import {
getUuid,
ImageKind,
InvalidPDFException,
makeArr,
makeMap,
makeObj,
MathClamp,
@ -125,6 +126,7 @@ globalThis.pdfjsLib = {
isDataScheme,
isPdfFile,
isValidExplicitDest,
makeArr,
makeMap,
makeObj,
MathClamp,
@ -186,6 +188,7 @@ export {
isDataScheme,
isPdfFile,
isValidExplicitDest,
makeArr,
makeMap,
makeObj,
MathClamp,

View File

@ -1236,6 +1236,7 @@ function _isValidExplicitDest(validRef, validName, dest) {
// Helpers for simple `Map.prototype.getOrInsertComputed()` invocations,
// to avoid duplicate function creation.
const makeArr = () => [];
const makeMap = () => new Map();
const makeObj = () => Object.create(null);
@ -1336,6 +1337,7 @@ export {
isNodeJS,
LINE_DESCENT_FACTOR,
LINE_FACTOR,
makeArr,
makeMap,
makeObj,
MathClamp,

View File

@ -24,6 +24,7 @@ import {
getUuid,
ImageKind,
InvalidPDFException,
makeArr,
makeMap,
makeObj,
MathClamp,
@ -109,6 +110,7 @@ const expectedAPI = Object.freeze({
isDataScheme,
isPdfFile,
isValidExplicitDest,
makeArr,
makeMap,
makeObj,
MathClamp,

View File

@ -33,6 +33,7 @@ import {
AnnotationEditorType,
AnnotationEditorUIManager,
AnnotationMode,
makeArr,
MathClamp,
PermissionFlag,
PixelsPerInch,
@ -2281,7 +2282,7 @@ class PDFViewer {
if (percent === 0 || widthPercent < 100) {
continue;
}
pageLayout.getOrInsert(y, []).push(id);
pageLayout.getOrInsertComputed(y, makeArr).push(id);
}
// Find the row of the current page.
for (const yArray of pageLayout.values()) {

View File

@ -44,6 +44,7 @@ const {
isDataScheme,
isPdfFile,
isValidExplicitDest,
makeArr,
makeMap,
makeObj,
MathClamp,
@ -105,6 +106,7 @@ export {
isDataScheme,
isPdfFile,
isValidExplicitDest,
makeArr,
makeMap,
makeObj,
MathClamp,