mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-04-22 21:24:04 +02:00
Reduce function creation when using Map.prototype.getOrInsertComputed()
With the exception of the first invocation the callback function is unused, which means that a lot of pointless functions may be created. To avoid this we introduce helper functions for simple cases, such as creating `Map`s and `Objects`s.
This commit is contained in:
parent
c2f5e19eb0
commit
2e07715c9d
@ -13,9 +13,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { makeObj, warn } from "../../shared/util.js";
|
||||
import { $globalData } from "./symbol_utils.js";
|
||||
import { stripQuotes } from "./utils.js";
|
||||
import { warn } from "../../shared/util.js";
|
||||
|
||||
class FontFinder {
|
||||
constructor(pdfFonts) {
|
||||
@ -48,9 +48,7 @@ class FontFinder {
|
||||
addPdfFont(pdfFont) {
|
||||
const cssFontInfo = pdfFont.cssFontInfo;
|
||||
const name = cssFontInfo.fontFamily;
|
||||
const font = this.fonts.getOrInsertComputed(name, () =>
|
||||
Object.create(null)
|
||||
);
|
||||
const font = this.fonts.getOrInsertComputed(name, makeObj);
|
||||
this.defaultFont ??= font;
|
||||
|
||||
let property = "";
|
||||
|
||||
@ -19,7 +19,7 @@ import {
|
||||
$getChildrenByName,
|
||||
$getParent,
|
||||
} from "./symbol_utils.js";
|
||||
import { warn } from "../../shared/util.js";
|
||||
import { makeMap, warn } from "../../shared/util.js";
|
||||
|
||||
const namePattern = /^[^.[]+/;
|
||||
const indexPattern = /^[^\]]+/;
|
||||
@ -193,7 +193,7 @@ function searchNode(
|
||||
let children, cached;
|
||||
|
||||
if (useCache) {
|
||||
cached = somCache.getOrInsertComputed(node, () => new Map());
|
||||
cached = somCache.getOrInsertComputed(node, makeMap);
|
||||
children = cached.get(cacheName);
|
||||
}
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { shadow, unreachable } from "../shared/util.js";
|
||||
import { makeMap, shadow, unreachable } from "../shared/util.js";
|
||||
import { AnnotationEditor } from "./editor/editor.js";
|
||||
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
|
||||
|
||||
@ -260,7 +260,7 @@ class AnnotationStorage {
|
||||
if (key === "type") {
|
||||
continue;
|
||||
}
|
||||
const counters = map.getOrInsertComputed(key, () => new Map());
|
||||
const counters = map.getOrInsertComputed(key, makeMap);
|
||||
counters.set(val, (counters.get(val) ?? 0) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,6 +25,7 @@ import {
|
||||
getVerbosityLevel,
|
||||
info,
|
||||
isNodeJS,
|
||||
makeObj,
|
||||
MathClamp,
|
||||
RenderingIntentFlag,
|
||||
setVerbosityLevel,
|
||||
@ -1502,8 +1503,9 @@ class PDFPageProxy {
|
||||
optionalContentConfigPromise ||=
|
||||
this._transport.getOptionalContentConfig(renderingIntent);
|
||||
|
||||
const intentState = this._intentStates.getOrInsertComputed(cacheKey, () =>
|
||||
Object.create(null)
|
||||
const intentState = this._intentStates.getOrInsertComputed(
|
||||
cacheKey,
|
||||
makeObj
|
||||
);
|
||||
// Ensure that a pending `streamReader` cancel timeout is always aborted.
|
||||
if (intentState.streamReaderCancelTimeout) {
|
||||
@ -1675,7 +1677,7 @@ class PDFPageProxy {
|
||||
);
|
||||
const intentState = this._intentStates.getOrInsertComputed(
|
||||
intentArgs.cacheKey,
|
||||
() => Object.create(null)
|
||||
makeObj
|
||||
);
|
||||
let opListTask;
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@ import {
|
||||
FONT_IDENTITY_MATRIX,
|
||||
ImageKind,
|
||||
info,
|
||||
makeMap,
|
||||
OPS,
|
||||
shadow,
|
||||
TextRenderingMode,
|
||||
@ -989,10 +990,7 @@ class CanvasGraphics {
|
||||
: [currentTransform.slice(0, 4), fillColor]
|
||||
);
|
||||
|
||||
cache = this._cachedBitmapsMap.getOrInsertComputed(
|
||||
mainKey,
|
||||
() => new Map()
|
||||
);
|
||||
cache = this._cachedBitmapsMap.getOrInsertComputed(mainKey, makeMap);
|
||||
const cachedImage = cache.get(cacheKey);
|
||||
if (cachedImage && !isPatternFill) {
|
||||
const offsetX = Math.round(
|
||||
|
||||
@ -15,6 +15,11 @@
|
||||
|
||||
const INITIAL_DATA = Symbol("INITIAL_DATA");
|
||||
|
||||
const dataObj = () => ({
|
||||
...Promise.withResolvers(),
|
||||
data: INITIAL_DATA,
|
||||
});
|
||||
|
||||
/**
|
||||
* A PDF document and page is built of many objects. E.g. there are objects for
|
||||
* fonts, images, rendering code, etc. These objects may get processed inside of
|
||||
@ -30,10 +35,7 @@ class PDFObjects {
|
||||
* @returns {Object}
|
||||
*/
|
||||
#ensureObj(objId) {
|
||||
return this.#objs.getOrInsertComputed(objId, () => ({
|
||||
...Promise.withResolvers(),
|
||||
data: INITIAL_DATA,
|
||||
}));
|
||||
return this.#objs.getOrInsertComputed(objId, dataObj);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -33,6 +33,8 @@ import {
|
||||
getUuid,
|
||||
ImageKind,
|
||||
InvalidPDFException,
|
||||
makeMap,
|
||||
makeObj,
|
||||
MathClamp,
|
||||
normalizeUnicode,
|
||||
OPS,
|
||||
@ -123,6 +125,8 @@ globalThis.pdfjsLib = {
|
||||
isDataScheme,
|
||||
isPdfFile,
|
||||
isValidExplicitDest,
|
||||
makeMap,
|
||||
makeObj,
|
||||
MathClamp,
|
||||
noContextMenu,
|
||||
normalizeUnicode,
|
||||
@ -182,6 +186,8 @@ export {
|
||||
isDataScheme,
|
||||
isPdfFile,
|
||||
isValidExplicitDest,
|
||||
makeMap,
|
||||
makeObj,
|
||||
MathClamp,
|
||||
noContextMenu,
|
||||
normalizeUnicode,
|
||||
|
||||
@ -1234,6 +1234,11 @@ function _isValidExplicitDest(validRef, validName, dest) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Helpers for simple `Map.prototype.getOrInsertComputed()` invocations,
|
||||
// to avoid duplicate function creation.
|
||||
const makeMap = () => new Map();
|
||||
const makeObj = () => Object.create(null);
|
||||
|
||||
// TODO: Replace all occurrences of this function with `Math.clamp` once
|
||||
// https://github.com/tc39/proposal-math-clamp/ is generally available.
|
||||
function MathClamp(v, min, max) {
|
||||
@ -1331,6 +1336,8 @@ export {
|
||||
isNodeJS,
|
||||
LINE_DESCENT_FACTOR,
|
||||
LINE_FACTOR,
|
||||
makeMap,
|
||||
makeObj,
|
||||
MathClamp,
|
||||
MeshFigureType,
|
||||
normalizeUnicode,
|
||||
|
||||
@ -24,6 +24,8 @@ import {
|
||||
getUuid,
|
||||
ImageKind,
|
||||
InvalidPDFException,
|
||||
makeMap,
|
||||
makeObj,
|
||||
MathClamp,
|
||||
normalizeUnicode,
|
||||
OPS,
|
||||
@ -107,6 +109,8 @@ const expectedAPI = Object.freeze({
|
||||
isDataScheme,
|
||||
isPdfFile,
|
||||
isValidExplicitDest,
|
||||
makeMap,
|
||||
makeObj,
|
||||
MathClamp,
|
||||
noContextMenu,
|
||||
normalizeUnicode,
|
||||
|
||||
@ -44,6 +44,8 @@ const {
|
||||
isDataScheme,
|
||||
isPdfFile,
|
||||
isValidExplicitDest,
|
||||
makeMap,
|
||||
makeObj,
|
||||
MathClamp,
|
||||
noContextMenu,
|
||||
normalizeUnicode,
|
||||
@ -103,6 +105,8 @@ export {
|
||||
isDataScheme,
|
||||
isPdfFile,
|
||||
isValidExplicitDest,
|
||||
makeMap,
|
||||
makeObj,
|
||||
MathClamp,
|
||||
noContextMenu,
|
||||
normalizeUnicode,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user