Merge pull request #21576 from Snuffleupagus/RefSetCache-getOrPutComputed

Add a `getOrPutComputed` method in the `RefSetCache` class
This commit is contained in:
Jonas Jenwald 2026-07-15 17:45:47 +02:00 committed by GitHub
commit e39b23904c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 37 additions and 32 deletions

View File

@ -455,12 +455,7 @@ class PDFEditor {
// Re-entry means a (malformed) cycle back to this stream: allocate its
// reference now to break the loop, like the generic path's eager alloc.
if (resourceStreamPath.has(oldRef)) {
let ref = oldRefMapping.get(oldRef);
if (!ref) {
ref = this.newRef;
oldRefMapping.put(oldRef, ref);
}
return ref;
return oldRefMapping.getOrPutComputed(oldRef, () => this.newRef);
}
const key = oldRef.toString();

View File

@ -13,6 +13,8 @@
* limitations under the License.
*/
import { makeSet } from "../shared/util.js";
const ON_CURVE_POINT = 1 << 0;
const X_SHORT_VECTOR = 1 << 1;
const Y_SHORT_VECTOR = 1 << 2;
@ -756,13 +758,7 @@ function pruneCompositeGlyphCycles(glyfTable, locaEntries, numGlyphs) {
stack.push({ node: next, idx: 0 });
continue;
}
let removeSet = backEdges.get(top.node);
if (!removeSet) {
removeSet = new Set();
backEdges.set(top.node, removeSet);
}
removeSet.add(compIdx);
backEdges.getOrInsertComputed(top.node, makeSet).add(compIdx);
}
}

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { assert, unreachable, warn } from "../shared/util.js";
import { assert, makeSet, unreachable, warn } from "../shared/util.js";
import { RefSet, RefSetCache } from "./primitives.js";
class BaseLocalCache {
@ -228,11 +228,7 @@ class GlobalImageCache {
}
shouldCache(ref, pageIndex) {
let pageIndexSet = this._refCache.get(ref);
if (!pageIndexSet) {
pageIndexSet = new Set();
this._refCache.put(ref, pageIndexSet);
}
const pageIndexSet = this._refCache.getOrPutComputed(ref, makeSet);
pageIndexSet.add(pageIndex);
if (pageIndexSet.size < GlobalImageCache.NUM_PAGES_THRESHOLD) {

View File

@ -398,6 +398,16 @@ class RefSetCache {
this._map.set(ref.toString(), this.get(aliasRef));
}
getOrPutComputed(ref, callback) {
const map = this._map,
refStr = ref.toString();
if (!map.has(refStr)) {
map.set(refStr, callback(ref));
}
return map.get(refStr);
}
[Symbol.iterator]() {
return this._map.values();
}

View File

@ -93,12 +93,7 @@ class StructTreeRoot {
return;
}
this.structParentIds ||= new RefSetCache();
let ids = this.structParentIds.get(pageRef);
if (!ids) {
ids = [];
this.structParentIds.put(pageRef, ids);
}
ids.push([id, type]);
this.structParentIds.getOrPutComputed(pageRef, makeArr).push([id, type]);
}
addAnnotationIdToPage(pageRef, id) {
@ -537,11 +532,9 @@ class StructTreeRoot {
return;
}
let cachedParentDict = cache.get(parentRef);
if (!cachedParentDict) {
cachedParentDict = parentDict.clone();
cache.put(parentRef, cachedParentDict);
}
const cachedParentDict = cache.getOrPutComputed(parentRef, () =>
parentDict.clone()
);
const parentKidsRaw = cachedParentDict.getRaw("K");
let cachedParentKids =
parentKidsRaw instanceof Ref ? cache.get(parentKidsRaw) : null;

View File

@ -36,6 +36,7 @@ import {
makeArr,
makeMap,
makeObj,
makeSet,
normalizeUnicode,
OPS,
PasswordException,
@ -131,6 +132,7 @@ globalThis.pdfjsLib = {
makeArr,
makeMap,
makeObj,
makeSet,
MathClamp,
noContextMenu,
normalizeUnicode,
@ -195,6 +197,7 @@ export {
makeArr,
makeMap,
makeObj,
makeSet,
MathClamp,
noContextMenu,
normalizeUnicode,

View File

@ -1135,6 +1135,7 @@ function _isValidExplicitDest(validRef, validName, dest) {
const makeArr = () => [];
const makeMap = () => new Map();
const makeObj = () => Object.create(null);
const makeSet = () => new Set();
// See https://developer.mozilla.org/en-US/docs/Web/API/Blob/bytes#browser_compatibility
if (
@ -1197,6 +1198,7 @@ export {
makeArr,
makeMap,
makeObj,
makeSet,
MeshFigureType,
normalizeUnicode,
objectSize,

View File

@ -27,6 +27,7 @@ import {
makeArr,
makeMap,
makeObj,
makeSet,
normalizeUnicode,
OPS,
PasswordException,
@ -115,6 +116,7 @@ const expectedAPI = Object.freeze({
makeArr,
makeMap,
makeObj,
makeSet,
MathClamp,
noContextMenu,
normalizeUnicode,

View File

@ -568,6 +568,12 @@ describe("primitives", function () {
cache.put(ref2, obj2);
expect([...cache.keys()]).toEqual([ref1, ref2]);
});
it("should handle getOrPutComputed correctly", function () {
expect(cache.getOrPutComputed(ref1, () => obj1)).toEqual(obj1);
// Trying to set it again should be ignored.
expect(cache.getOrPutComputed(ref1, () => obj2)).toEqual(obj1);
});
});
describe("isName", function () {

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
const { OPS } = globalThis.pdfjsLib || (await import("pdfjs-lib"));
const { makeSet, OPS } = globalThis.pdfjsLib || (await import("pdfjs-lib"));
const opMap = Object.create(null);
for (const key in OPS) {
@ -460,7 +460,7 @@ class Stepper {
for (const [dependentIdx, { dependencies: ownDependencies }] of metadata) {
for (const dependencyIdx of ownDependencies) {
dependents
.getOrInsertComputed(dependencyIdx, () => new Set())
.getOrInsertComputed(dependencyIdx, makeSet)
.add(dependentIdx);
}
}

View File

@ -47,6 +47,7 @@ const {
makeArr,
makeMap,
makeObj,
makeSet,
MathClamp,
noContextMenu,
normalizeUnicode,
@ -111,6 +112,7 @@ export {
makeArr,
makeMap,
makeObj,
makeSet,
MathClamp,
noContextMenu,
normalizeUnicode,