Skip to content

Commit 4ecbd0c

Browse files
Merge pull request #20726 from Snuffleupagus/getOrInsertComputed-fewer-functions
Reduce allocations and function creation when using `getOrInsert` and `getOrInsertComputed`
2 parents b43c8ea + 185fee6 commit 4ecbd0c

File tree

17 files changed

+73
-47
lines changed

17 files changed

+73
-47
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,
@@ -1894,7 +1895,7 @@ class PDFDocument {
18941895
orphanFields.put(fieldRef, parentRef);
18951896
}
18961897

1897-
promises.getOrInsert(name, []).push(
1898+
promises.getOrInsertComputed(name, makeArr).push(
18981899
AnnotationFactory.create(
18991900
xref,
19001901
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 & 7 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,12 +166,9 @@ class Builder {
166166
_addNamespacePrefix(prefixes) {
167167
for (const { prefix, value } of prefixes) {
168168
const namespace = this._searchNamespace(value);
169-
let prefixStack = this._namespacePrefixes.get(prefix);
170-
if (!prefixStack) {
171-
prefixStack = [];
172-
this._namespacePrefixes.set(prefix, prefixStack);
173-
}
174-
prefixStack.push(namespace);
169+
this._namespacePrefixes
170+
.getOrInsertComputed(prefix, makeArr)
171+
.push(namespace);
175172
}
176173
}
177174

src/core/xfa/fonts.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* limitations under the License.
1414
*/
1515

16+
import { makeObj, warn } from "../../shared/util.js";
1617
import { $globalData } from "./symbol_utils.js";
1718
import { stripQuotes } from "./utils.js";
18-
import { warn } from "../../shared/util.js";
1919

2020
class FontFinder {
2121
constructor(pdfFonts) {
@@ -48,14 +48,9 @@ class FontFinder {
4848
addPdfFont(pdfFont) {
4949
const cssFontInfo = pdfFont.cssFontInfo;
5050
const name = cssFontInfo.fontFamily;
51-
let font = this.fonts.get(name);
52-
if (!font) {
53-
font = Object.create(null);
54-
this.fonts.set(name, font);
55-
if (!this.defaultFont) {
56-
this.defaultFont = font;
57-
}
58-
}
51+
const font = this.fonts.getOrInsertComputed(name, makeObj);
52+
this.defaultFont ??= font;
53+
5954
let property = "";
6055
const fontWeight = parseFloat(cssFontInfo.fontWeight);
6156
if (parseFloat(cssFontInfo.italicAngle) !== 0) {

src/core/xfa/som.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
$getChildrenByName,
2020
$getParent,
2121
} from "./symbol_utils.js";
22-
import { warn } from "../../shared/util.js";
22+
import { makeMap, warn } from "../../shared/util.js";
2323

2424
const namePattern = /^[^.[]+/;
2525
const indexPattern = /^[^\]]+/;
@@ -193,11 +193,7 @@ function searchNode(
193193
let children, cached;
194194

195195
if (useCache) {
196-
cached = somCache.get(node);
197-
if (!cached) {
198-
cached = new Map();
199-
somCache.set(node, cached);
200-
}
196+
cached = somCache.getOrInsertComputed(node, makeMap);
201197
children = cached.get(cacheName);
202198
}
203199

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/display/annotation_storage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { shadow, unreachable } from "../shared/util.js";
16+
import { makeMap, shadow, unreachable } from "../shared/util.js";
1717
import { AnnotationEditor } from "./editor/editor.js";
1818
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
1919

@@ -260,7 +260,7 @@ class AnnotationStorage {
260260
if (key === "type") {
261261
continue;
262262
}
263-
const counters = map.getOrInsertComputed(key, () => new Map());
263+
const counters = map.getOrInsertComputed(key, makeMap);
264264
counters.set(val, (counters.get(val) ?? 0) + 1);
265265
}
266266
}

src/display/api.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
getVerbosityLevel,
2626
info,
2727
isNodeJS,
28+
makeObj,
2829
MathClamp,
2930
RenderingIntentFlag,
3031
setVerbosityLevel,
@@ -1502,8 +1503,9 @@ class PDFPageProxy {
15021503
optionalContentConfigPromise ||=
15031504
this._transport.getOptionalContentConfig(renderingIntent);
15041505

1505-
const intentState = this._intentStates.getOrInsertComputed(cacheKey, () =>
1506-
Object.create(null)
1506+
const intentState = this._intentStates.getOrInsertComputed(
1507+
cacheKey,
1508+
makeObj
15071509
);
15081510
// Ensure that a pending `streamReader` cancel timeout is always aborted.
15091511
if (intentState.streamReaderCancelTimeout) {
@@ -1675,7 +1677,7 @@ class PDFPageProxy {
16751677
);
16761678
const intentState = this._intentStates.getOrInsertComputed(
16771679
intentArgs.cacheKey,
1678-
() => Object.create(null)
1680+
makeObj
16791681
);
16801682
let opListTask;
16811683

src/display/canvas.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
FONT_IDENTITY_MATRIX,
2323
ImageKind,
2424
info,
25+
makeMap,
2526
OPS,
2627
shadow,
2728
TextRenderingMode,
@@ -989,10 +990,7 @@ class CanvasGraphics {
989990
: [currentTransform.slice(0, 4), fillColor]
990991
);
991992

992-
cache = this._cachedBitmapsMap.getOrInsertComputed(
993-
mainKey,
994-
() => new Map()
995-
);
993+
cache = this._cachedBitmapsMap.getOrInsertComputed(mainKey, makeMap);
996994
const cachedImage = cache.get(cacheKey);
997995
if (cachedImage && !isPatternFill) {
998996
const offsetX = Math.round(

0 commit comments

Comments
 (0)