Skip to content

Commit 0d4e587

Browse files
committed
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).
1 parent 2e07715 commit 0d4e587

File tree

10 files changed

+27
-7
lines changed

10 files changed

+27
-7
lines changed

src/core/core_utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
assert,
1919
BaseException,
2020
hexNumbers,
21+
makeArr,
2122
objectSize,
2223
stringToPDFString,
2324
Util,
@@ -669,7 +670,9 @@ function getNewAnnotationsMap(annotationStorage) {
669670
if (!key.startsWith(AnnotationEditorPrefix)) {
670671
continue;
671672
}
672-
newAnnotationsByPage.getOrInsert(value.pageIndex, []).push(value);
673+
newAnnotationsByPage
674+
.getOrInsertComputed(value.pageIndex, makeArr)
675+
.push(value);
673676
}
674677
return newAnnotationsByPage.size > 0 ? newAnnotationsByPage : null;
675678
}

src/core/document.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
info,
2121
InvalidPDFException,
2222
isArrayEqual,
23+
makeArr,
2324
objectSize,
2425
PageActionEventType,
2526
RenderingIntentFlag,
@@ -1892,7 +1893,7 @@ class PDFDocument {
18921893
orphanFields.put(fieldRef, parentRef);
18931894
}
18941895

1895-
promises.getOrInsert(name, []).push(
1896+
promises.getOrInsertComputed(name, makeArr).push(
18961897
AnnotationFactory.create(
18971898
xref,
18981899
fieldRef,

src/core/struct_tree.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import {
1717
AnnotationPrefix,
18+
makeArr,
1819
stringToPDFString,
1920
stringToUTF8String,
2021
warn,
@@ -450,7 +451,7 @@ class StructTreeRoot {
450451
for (const element of elements) {
451452
if (element.structTreeParentId) {
452453
const id = parseInt(element.structTreeParentId.split("_mc")[1], 10);
453-
idToElements.getOrInsert(id, []).push(element);
454+
idToElements.getOrInsertComputed(id, makeArr).push(element);
454455
}
455456
}
456457

src/core/xfa/builder.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import {
2424
$resolvePrototypes,
2525
$root,
2626
} from "./symbol_utils.js";
27+
import { makeArr, warn } from "../../shared/util.js";
2728
import { NamespaceSetUp } from "./setup.js";
2829
import { Template } from "./template.js";
2930
import { UnknownNamespace } from "./unknown.js";
30-
import { warn } from "../../shared/util.js";
3131
import { XFAObject } from "./xfa_object.js";
3232

3333
class Root extends XFAObject {
@@ -166,7 +166,9 @@ class Builder {
166166
_addNamespacePrefix(prefixes) {
167167
for (const { prefix, value } of prefixes) {
168168
const namespace = this._searchNamespace(value);
169-
this._namespacePrefixes.getOrInsert(prefix, []).push(namespace);
169+
this._namespacePrefixes
170+
.getOrInsertComputed(prefix, makeArr)
171+
.push(namespace);
170172
}
171173
}
172174

src/display/annotation_layer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
AnnotationType,
3737
FeatureTest,
3838
LINE_FACTOR,
39+
makeArr,
3940
shadow,
4041
unreachable,
4142
Util,
@@ -3877,7 +3878,9 @@ class AnnotationLayer {
38773878
this.#elements.push(element);
38783879

38793880
if (data.popupRef) {
3880-
popupToElements.getOrInsert(data.popupRef, []).push(element);
3881+
popupToElements
3882+
.getOrInsertComputed(data.popupRef, makeArr)
3883+
.push(element);
38813884
}
38823885
}
38833886

src/pdf.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
getUuid,
3434
ImageKind,
3535
InvalidPDFException,
36+
makeArr,
3637
makeMap,
3738
makeObj,
3839
MathClamp,
@@ -125,6 +126,7 @@ globalThis.pdfjsLib = {
125126
isDataScheme,
126127
isPdfFile,
127128
isValidExplicitDest,
129+
makeArr,
128130
makeMap,
129131
makeObj,
130132
MathClamp,
@@ -186,6 +188,7 @@ export {
186188
isDataScheme,
187189
isPdfFile,
188190
isValidExplicitDest,
191+
makeArr,
189192
makeMap,
190193
makeObj,
191194
MathClamp,

src/shared/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ function _isValidExplicitDest(validRef, validName, dest) {
12361236

12371237
// Helpers for simple `Map.prototype.getOrInsertComputed()` invocations,
12381238
// to avoid duplicate function creation.
1239+
const makeArr = () => [];
12391240
const makeMap = () => new Map();
12401241
const makeObj = () => Object.create(null);
12411242

@@ -1336,6 +1337,7 @@ export {
13361337
isNodeJS,
13371338
LINE_DESCENT_FACTOR,
13381339
LINE_FACTOR,
1340+
makeArr,
13391341
makeMap,
13401342
makeObj,
13411343
MathClamp,

test/unit/pdf_spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
getUuid,
2525
ImageKind,
2626
InvalidPDFException,
27+
makeArr,
2728
makeMap,
2829
makeObj,
2930
MathClamp,
@@ -109,6 +110,7 @@ const expectedAPI = Object.freeze({
109110
isDataScheme,
110111
isPdfFile,
111112
isValidExplicitDest,
113+
makeArr,
112114
makeMap,
113115
makeObj,
114116
MathClamp,

web/pdf_viewer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
AnnotationEditorType,
3434
AnnotationEditorUIManager,
3535
AnnotationMode,
36+
makeArr,
3637
MathClamp,
3738
PermissionFlag,
3839
PixelsPerInch,
@@ -2281,7 +2282,7 @@ class PDFViewer {
22812282
if (percent === 0 || widthPercent < 100) {
22822283
continue;
22832284
}
2284-
pageLayout.getOrInsert(y, []).push(id);
2285+
pageLayout.getOrInsertComputed(y, makeArr).push(id);
22852286
}
22862287
// Find the row of the current page.
22872288
for (const yArray of pageLayout.values()) {

web/pdfjs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const {
4444
isDataScheme,
4545
isPdfFile,
4646
isValidExplicitDest,
47+
makeArr,
4748
makeMap,
4849
makeObj,
4950
MathClamp,
@@ -105,6 +106,7 @@ export {
105106
isDataScheme,
106107
isPdfFile,
107108
isValidExplicitDest,
109+
makeArr,
108110
makeMap,
109111
makeObj,
110112
MathClamp,

0 commit comments

Comments
 (0)