mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-02 20:37:21 +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.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { makeObj, warn } from "../../shared/util.js";
|
||||||
import { $globalData } from "./symbol_utils.js";
|
import { $globalData } from "./symbol_utils.js";
|
||||||
import { stripQuotes } from "./utils.js";
|
import { stripQuotes } from "./utils.js";
|
||||||
import { warn } from "../../shared/util.js";
|
|
||||||
|
|
||||||
class FontFinder {
|
class FontFinder {
|
||||||
constructor(pdfFonts) {
|
constructor(pdfFonts) {
|
||||||
@ -48,9 +48,7 @@ class FontFinder {
|
|||||||
addPdfFont(pdfFont) {
|
addPdfFont(pdfFont) {
|
||||||
const cssFontInfo = pdfFont.cssFontInfo;
|
const cssFontInfo = pdfFont.cssFontInfo;
|
||||||
const name = cssFontInfo.fontFamily;
|
const name = cssFontInfo.fontFamily;
|
||||||
const font = this.fonts.getOrInsertComputed(name, () =>
|
const font = this.fonts.getOrInsertComputed(name, makeObj);
|
||||||
Object.create(null)
|
|
||||||
);
|
|
||||||
this.defaultFont ??= font;
|
this.defaultFont ??= font;
|
||||||
|
|
||||||
let property = "";
|
let property = "";
|
||||||
|
|||||||
@ -19,7 +19,7 @@ import {
|
|||||||
$getChildrenByName,
|
$getChildrenByName,
|
||||||
$getParent,
|
$getParent,
|
||||||
} from "./symbol_utils.js";
|
} from "./symbol_utils.js";
|
||||||
import { warn } from "../../shared/util.js";
|
import { makeMap, warn } from "../../shared/util.js";
|
||||||
|
|
||||||
const namePattern = /^[^.[]+/;
|
const namePattern = /^[^.[]+/;
|
||||||
const indexPattern = /^[^\]]+/;
|
const indexPattern = /^[^\]]+/;
|
||||||
@ -193,7 +193,7 @@ function searchNode(
|
|||||||
let children, cached;
|
let children, cached;
|
||||||
|
|
||||||
if (useCache) {
|
if (useCache) {
|
||||||
cached = somCache.getOrInsertComputed(node, () => new Map());
|
cached = somCache.getOrInsertComputed(node, makeMap);
|
||||||
children = cached.get(cacheName);
|
children = cached.get(cacheName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
* limitations under the License.
|
* 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 { AnnotationEditor } from "./editor/editor.js";
|
||||||
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
|
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
|
||||||
|
|
||||||
@ -260,7 +260,7 @@ class AnnotationStorage {
|
|||||||
if (key === "type") {
|
if (key === "type") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const counters = map.getOrInsertComputed(key, () => new Map());
|
const counters = map.getOrInsertComputed(key, makeMap);
|
||||||
counters.set(val, (counters.get(val) ?? 0) + 1);
|
counters.set(val, (counters.get(val) ?? 0) + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,7 @@ import {
|
|||||||
getVerbosityLevel,
|
getVerbosityLevel,
|
||||||
info,
|
info,
|
||||||
isNodeJS,
|
isNodeJS,
|
||||||
|
makeObj,
|
||||||
MathClamp,
|
MathClamp,
|
||||||
RenderingIntentFlag,
|
RenderingIntentFlag,
|
||||||
setVerbosityLevel,
|
setVerbosityLevel,
|
||||||
@ -1502,8 +1503,9 @@ class PDFPageProxy {
|
|||||||
optionalContentConfigPromise ||=
|
optionalContentConfigPromise ||=
|
||||||
this._transport.getOptionalContentConfig(renderingIntent);
|
this._transport.getOptionalContentConfig(renderingIntent);
|
||||||
|
|
||||||
const intentState = this._intentStates.getOrInsertComputed(cacheKey, () =>
|
const intentState = this._intentStates.getOrInsertComputed(
|
||||||
Object.create(null)
|
cacheKey,
|
||||||
|
makeObj
|
||||||
);
|
);
|
||||||
// Ensure that a pending `streamReader` cancel timeout is always aborted.
|
// Ensure that a pending `streamReader` cancel timeout is always aborted.
|
||||||
if (intentState.streamReaderCancelTimeout) {
|
if (intentState.streamReaderCancelTimeout) {
|
||||||
@ -1675,7 +1677,7 @@ class PDFPageProxy {
|
|||||||
);
|
);
|
||||||
const intentState = this._intentStates.getOrInsertComputed(
|
const intentState = this._intentStates.getOrInsertComputed(
|
||||||
intentArgs.cacheKey,
|
intentArgs.cacheKey,
|
||||||
() => Object.create(null)
|
makeObj
|
||||||
);
|
);
|
||||||
let opListTask;
|
let opListTask;
|
||||||
|
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import {
|
|||||||
FONT_IDENTITY_MATRIX,
|
FONT_IDENTITY_MATRIX,
|
||||||
ImageKind,
|
ImageKind,
|
||||||
info,
|
info,
|
||||||
|
makeMap,
|
||||||
OPS,
|
OPS,
|
||||||
shadow,
|
shadow,
|
||||||
TextRenderingMode,
|
TextRenderingMode,
|
||||||
@ -989,10 +990,7 @@ class CanvasGraphics {
|
|||||||
: [currentTransform.slice(0, 4), fillColor]
|
: [currentTransform.slice(0, 4), fillColor]
|
||||||
);
|
);
|
||||||
|
|
||||||
cache = this._cachedBitmapsMap.getOrInsertComputed(
|
cache = this._cachedBitmapsMap.getOrInsertComputed(mainKey, makeMap);
|
||||||
mainKey,
|
|
||||||
() => new Map()
|
|
||||||
);
|
|
||||||
const cachedImage = cache.get(cacheKey);
|
const cachedImage = cache.get(cacheKey);
|
||||||
if (cachedImage && !isPatternFill) {
|
if (cachedImage && !isPatternFill) {
|
||||||
const offsetX = Math.round(
|
const offsetX = Math.round(
|
||||||
|
|||||||
@ -15,6 +15,11 @@
|
|||||||
|
|
||||||
const INITIAL_DATA = Symbol("INITIAL_DATA");
|
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
|
* 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
|
* fonts, images, rendering code, etc. These objects may get processed inside of
|
||||||
@ -30,10 +35,7 @@ class PDFObjects {
|
|||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
#ensureObj(objId) {
|
#ensureObj(objId) {
|
||||||
return this.#objs.getOrInsertComputed(objId, () => ({
|
return this.#objs.getOrInsertComputed(objId, dataObj);
|
||||||
...Promise.withResolvers(),
|
|
||||||
data: INITIAL_DATA,
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -33,6 +33,8 @@ import {
|
|||||||
getUuid,
|
getUuid,
|
||||||
ImageKind,
|
ImageKind,
|
||||||
InvalidPDFException,
|
InvalidPDFException,
|
||||||
|
makeMap,
|
||||||
|
makeObj,
|
||||||
MathClamp,
|
MathClamp,
|
||||||
normalizeUnicode,
|
normalizeUnicode,
|
||||||
OPS,
|
OPS,
|
||||||
@ -123,6 +125,8 @@ globalThis.pdfjsLib = {
|
|||||||
isDataScheme,
|
isDataScheme,
|
||||||
isPdfFile,
|
isPdfFile,
|
||||||
isValidExplicitDest,
|
isValidExplicitDest,
|
||||||
|
makeMap,
|
||||||
|
makeObj,
|
||||||
MathClamp,
|
MathClamp,
|
||||||
noContextMenu,
|
noContextMenu,
|
||||||
normalizeUnicode,
|
normalizeUnicode,
|
||||||
@ -182,6 +186,8 @@ export {
|
|||||||
isDataScheme,
|
isDataScheme,
|
||||||
isPdfFile,
|
isPdfFile,
|
||||||
isValidExplicitDest,
|
isValidExplicitDest,
|
||||||
|
makeMap,
|
||||||
|
makeObj,
|
||||||
MathClamp,
|
MathClamp,
|
||||||
noContextMenu,
|
noContextMenu,
|
||||||
normalizeUnicode,
|
normalizeUnicode,
|
||||||
|
|||||||
@ -1234,6 +1234,11 @@ function _isValidExplicitDest(validRef, validName, dest) {
|
|||||||
return true;
|
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
|
// TODO: Replace all occurrences of this function with `Math.clamp` once
|
||||||
// https://github.com/tc39/proposal-math-clamp/ is generally available.
|
// https://github.com/tc39/proposal-math-clamp/ is generally available.
|
||||||
function MathClamp(v, min, max) {
|
function MathClamp(v, min, max) {
|
||||||
@ -1331,6 +1336,8 @@ export {
|
|||||||
isNodeJS,
|
isNodeJS,
|
||||||
LINE_DESCENT_FACTOR,
|
LINE_DESCENT_FACTOR,
|
||||||
LINE_FACTOR,
|
LINE_FACTOR,
|
||||||
|
makeMap,
|
||||||
|
makeObj,
|
||||||
MathClamp,
|
MathClamp,
|
||||||
MeshFigureType,
|
MeshFigureType,
|
||||||
normalizeUnicode,
|
normalizeUnicode,
|
||||||
|
|||||||
@ -24,6 +24,8 @@ import {
|
|||||||
getUuid,
|
getUuid,
|
||||||
ImageKind,
|
ImageKind,
|
||||||
InvalidPDFException,
|
InvalidPDFException,
|
||||||
|
makeMap,
|
||||||
|
makeObj,
|
||||||
MathClamp,
|
MathClamp,
|
||||||
normalizeUnicode,
|
normalizeUnicode,
|
||||||
OPS,
|
OPS,
|
||||||
@ -107,6 +109,8 @@ const expectedAPI = Object.freeze({
|
|||||||
isDataScheme,
|
isDataScheme,
|
||||||
isPdfFile,
|
isPdfFile,
|
||||||
isValidExplicitDest,
|
isValidExplicitDest,
|
||||||
|
makeMap,
|
||||||
|
makeObj,
|
||||||
MathClamp,
|
MathClamp,
|
||||||
noContextMenu,
|
noContextMenu,
|
||||||
normalizeUnicode,
|
normalizeUnicode,
|
||||||
|
|||||||
@ -44,6 +44,8 @@ const {
|
|||||||
isDataScheme,
|
isDataScheme,
|
||||||
isPdfFile,
|
isPdfFile,
|
||||||
isValidExplicitDest,
|
isValidExplicitDest,
|
||||||
|
makeMap,
|
||||||
|
makeObj,
|
||||||
MathClamp,
|
MathClamp,
|
||||||
noContextMenu,
|
noContextMenu,
|
||||||
normalizeUnicode,
|
normalizeUnicode,
|
||||||
@ -103,6 +105,8 @@ export {
|
|||||||
isDataScheme,
|
isDataScheme,
|
||||||
isPdfFile,
|
isPdfFile,
|
||||||
isValidExplicitDest,
|
isValidExplicitDest,
|
||||||
|
makeMap,
|
||||||
|
makeObj,
|
||||||
MathClamp,
|
MathClamp,
|
||||||
noContextMenu,
|
noContextMenu,
|
||||||
normalizeUnicode,
|
normalizeUnicode,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user