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

View File

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

View File

@ -15,6 +15,7 @@
import { import {
AnnotationPrefix, AnnotationPrefix,
makeArr,
stringToPDFString, stringToPDFString,
stringToUTF8String, stringToUTF8String,
warn, warn,
@ -450,7 +451,7 @@ class StructTreeRoot {
for (const element of elements) { for (const element of elements) {
if (element.structTreeParentId) { if (element.structTreeParentId) {
const id = parseInt(element.structTreeParentId.split("_mc")[1], 10); 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, $resolvePrototypes,
$root, $root,
} from "./symbol_utils.js"; } from "./symbol_utils.js";
import { makeArr, warn } from "../../shared/util.js";
import { NamespaceSetUp } from "./setup.js"; import { NamespaceSetUp } from "./setup.js";
import { Template } from "./template.js"; import { Template } from "./template.js";
import { UnknownNamespace } from "./unknown.js"; import { UnknownNamespace } from "./unknown.js";
import { warn } from "../../shared/util.js";
import { XFAObject } from "./xfa_object.js"; import { XFAObject } from "./xfa_object.js";
class Root extends XFAObject { class Root extends XFAObject {
@ -166,7 +166,9 @@ class Builder {
_addNamespacePrefix(prefixes) { _addNamespacePrefix(prefixes) {
for (const { prefix, value } of prefixes) { for (const { prefix, value } of prefixes) {
const namespace = this._searchNamespace(value); 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, AnnotationType,
FeatureTest, FeatureTest,
LINE_FACTOR, LINE_FACTOR,
makeArr,
shadow, shadow,
unreachable, unreachable,
Util, Util,
@ -3877,7 +3878,9 @@ class AnnotationLayer {
this.#elements.push(element); this.#elements.push(element);
if (data.popupRef) { if (data.popupRef) {
popupToElements.getOrInsert(data.popupRef, []).push(element); popupToElements
.getOrInsertComputed(data.popupRef, makeArr)
.push(element);
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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