Use Map.prototype.getOrInsertComputed() more in the code-base

This commit is contained in:
Jonas Jenwald 2026-06-12 23:21:16 +02:00
parent ca34359e1f
commit ffa7ac7a91
7 changed files with 38 additions and 46 deletions

View File

@ -346,13 +346,13 @@ class ChunkedStreamManager {
const chunksToRequest = [];
for (const chunk of chunksNeeded) {
let requestIds = this._requestsByChunk.get(chunk);
if (!requestIds) {
requestIds = [];
this._requestsByChunk.set(chunk, requestIds);
chunksToRequest.push(chunk);
}
const requestIds = this._requestsByChunk.getOrInsertComputed(
chunk,
() => {
chunksToRequest.push(chunk);
return [];
}
);
requestIds.push(requestId);
}

View File

@ -29,7 +29,7 @@ import {
} from "../core_utils.js";
import { Dict, isName, Name, Ref, RefSet, RefSetCache } from "../primitives.js";
import { incrementalUpdate, writeValue } from "../writer.js";
import { isArrayEqual, stringToBytes } from "../../shared/util.js";
import { isArrayEqual, makeArr, stringToBytes } from "../../shared/util.js";
import { NameTree, NumberTree } from "../name_number_tree.js";
import { stringToAsciiOrUTF16BE, stringToPDFString } from "../string_utils.js";
import { AnnotationFactory } from "../annotation.js";
@ -510,20 +510,15 @@ class PDFEditor {
const bytes = this.#rawStreamBytes(stream);
const key = this.#resourceStreamKey(dictStr, bytes);
let bucket = this.#resourceStreamCache.get(key);
if (bucket) {
// Same key only means "maybe equal": confirm with an exact comparison.
for (const entry of bucket) {
if (
entry.dictStr === dictStr &&
isArrayEqual(this.#rawStreamBytes(entry.stream), bytes)
) {
return entry.ref;
}
const bucket = this.#resourceStreamCache.getOrInsertComputed(key, makeArr);
// Same key only means "maybe equal": confirm with an exact comparison.
for (const entry of bucket) {
if (
entry.dictStr === dictStr &&
isArrayEqual(this.#rawStreamBytes(entry.stream), bytes)
) {
return entry.ref;
}
} else {
bucket = [];
this.#resourceStreamCache.set(key, bucket);
}
const ref = this.newRef;
this.xref[ref.num] = stream;

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { assert, shadow, unreachable } from "../shared/util.js";
import { assert, makeArr, shadow, unreachable } from "../shared/util.js";
const CIRCULAR_REF = Symbol("CIRCULAR_REF");
const EOF = Symbol("EOF");
@ -242,11 +242,9 @@ class Dict {
continue;
}
for (const [key, value] of dict.getRawEntries()) {
let property = properties.get(key);
if (property === undefined) {
property = [];
properties.set(key, property);
} else if (!mergeSubDicts || !(value instanceof Dict)) {
const property = properties.getOrInsertComputed(key, makeArr);
if (property.length && !(mergeSubDicts && value instanceof Dict)) {
// Ignore additional entries, if either:
// - This is a "shallow" merge, where only the first element matters.
// - The value is *not* a `Dict`, since other types cannot be merged.

View File

@ -241,9 +241,10 @@ class AnnotationStorage {
continue;
}
const { type } = editorStats;
if (!typeToEditor.has(type)) {
typeToEditor.set(type, Object.getPrototypeOf(value).constructor);
}
typeToEditor.getOrInsertComputed(
type,
() => Object.getPrototypeOf(value).constructor
);
stats ||= Object.create(null);
const map = (stats[type] ||= new Map());
for (const [key, val] of Object.entries(editorStats)) {

View File

@ -23,6 +23,7 @@ import {
FONT_IDENTITY_MATRIX,
ImageKind,
info,
makeArr,
makeMap,
OPS,
shadow,
@ -1507,11 +1508,10 @@ class CanvasGraphics {
}
let knockoutFilter = "none";
if (needsAlphaScaling && this.#knockoutFilterCache instanceof Map) {
knockoutFilter = this.#knockoutFilterCache.get(alpha);
if (!knockoutFilter) {
knockoutFilter = this.filterFactory.addKnockoutFilter(alpha);
this.#knockoutFilterCache.set(alpha, knockoutFilter);
}
knockoutFilter = this.#knockoutFilterCache.getOrInsertComputed(
alpha,
() => this.filterFactory.addKnockoutFilter(alpha)
);
}
if (!needsAlphaScaling || knockoutFilter !== "none") {
@ -3682,11 +3682,10 @@ class CanvasGraphics {
);
const { canvas, context } = this.annotationCanvas;
if (canvasName) {
let canvases = this.annotationCanvasMap.get(id);
if (!canvases) {
canvases = [];
this.annotationCanvasMap.set(id, canvases);
}
const canvases = this.annotationCanvasMap.getOrInsertComputed(
id,
makeArr
);
canvas.setAttribute("data-canvas-name", canvasName);
// Replace any same-named canvas from a previous render so stale
// low-resolution canvases don't pile up across zooms.

View File

@ -23,6 +23,7 @@ import {
AnnotationEditorType,
FeatureTest,
getUuid,
makeArr,
shadow,
SVG_NS,
Util,
@ -550,7 +551,7 @@ class KeyboardManager {
continue;
}
this.callbacks
.getOrInsertComputed(keyName, () => [])
.getOrInsertComputed(keyName, makeArr)
.push({ callback, options, modifiers });
}
}

View File

@ -459,11 +459,9 @@ class Stepper {
const dependents = new Map();
for (const [dependentIdx, { dependencies: ownDependencies }] of metadata) {
for (const dependencyIdx of ownDependencies) {
let ownDependents = dependents.get(dependencyIdx);
if (!ownDependents) {
dependents.set(dependencyIdx, (ownDependents = new Set()));
}
ownDependents.add(dependentIdx);
dependents
.getOrInsertComputed(dependencyIdx, () => new Set())
.add(dependentIdx);
}
}