Merge pull request #21439 from Snuffleupagus/more-getOrInsertComputed

Use `Map.prototype.getOrInsertComputed()` more in the code-base
This commit is contained in:
Tim van der Meij 2026-06-13 12:44:51 +02:00 committed by GitHub
commit 5f8f6b1e40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 38 additions and 46 deletions

View File

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

View File

@ -37,7 +37,7 @@ import {
RefSetCache, RefSetCache,
} from "../primitives.js"; } from "../primitives.js";
import { incrementalUpdate, writeValue } from "../writer.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 { NameTree, NumberTree } from "../name_number_tree.js";
import { stringToAsciiOrUTF16BE, stringToPDFString } from "../string_utils.js"; import { stringToAsciiOrUTF16BE, stringToPDFString } from "../string_utils.js";
import { AnnotationFactory } from "../annotation.js"; import { AnnotationFactory } from "../annotation.js";
@ -514,20 +514,15 @@ class PDFEditor {
const bytes = this.#rawStreamBytes(stream); const bytes = this.#rawStreamBytes(stream);
const key = this.#resourceStreamKey(dictStr, bytes); const key = this.#resourceStreamKey(dictStr, bytes);
let bucket = this.#resourceStreamCache.get(key); const bucket = this.#resourceStreamCache.getOrInsertComputed(key, makeArr);
if (bucket) { // Same key only means "maybe equal": confirm with an exact comparison.
// Same key only means "maybe equal": confirm with an exact comparison. for (const entry of bucket) {
for (const entry of bucket) { if (
if ( entry.dictStr === dictStr &&
entry.dictStr === dictStr && isArrayEqual(this.#rawStreamBytes(entry.stream), bytes)
isArrayEqual(this.#rawStreamBytes(entry.stream), bytes) ) {
) { return entry.ref;
return entry.ref;
}
} }
} else {
bucket = [];
this.#resourceStreamCache.set(key, bucket);
} }
const ref = this.newRef; const ref = this.newRef;
this.xref[ref.num] = stream; this.xref[ref.num] = stream;

View File

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

View File

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

View File

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

View File

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

View File

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