Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-factor the LocalTilingPatternCache to cache by Ref rather than Name (PR 12458 follow-up, issue 13780) #13904

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,6 @@ class PartialEvaluator {
patternDict,
operatorList,
task,
cacheKey,
localTilingPatternCache
) {
// Create an IR of the pattern code.
Expand Down Expand Up @@ -825,8 +824,8 @@ class PartialEvaluator {
operatorList.addDependencies(tilingOpList.dependencies);
operatorList.addOp(fn, tilingPatternIR);

if (cacheKey) {
localTilingPatternCache.set(cacheKey, patternDict.objId, {
if (patternDict.objId) {
localTilingPatternCache.set(/* name = */ null, patternDict.objId, {
operatorListIR,
dict: patternDict,
});
Expand Down Expand Up @@ -1356,9 +1355,11 @@ class PartialEvaluator {
const patternName = args.pop();
// SCN/scn applies patterns along with normal colors
if (patternName instanceof Name) {
const name = patternName.name;
const rawPattern = patterns.getRaw(patternName.name);

const localTilingPattern = localTilingPatternCache.getByName(name);
const localTilingPattern =
rawPattern instanceof Ref &&
localTilingPatternCache.getByRef(rawPattern);
if (localTilingPattern) {
try {
const color = cs.base ? cs.base.getRgb(args, 0) : null;
Expand All @@ -1373,11 +1374,8 @@ class PartialEvaluator {
// Handle any errors during normal TilingPattern parsing.
}
}
// TODO: Attempt to lookup cached TilingPatterns by reference as well,
// if and only if there are PDF documents where doing so would
// significantly improve performance.

const pattern = patterns.get(name);
const pattern = this.xref.fetchIfRef(rawPattern);
if (pattern) {
const dict = isStream(pattern) ? pattern.dict : pattern;
const typeNum = dict.get("PatternType");
Expand All @@ -1392,7 +1390,6 @@ class PartialEvaluator {
dict,
operatorList,
task,
/* cacheKey = */ name,
localTilingPatternCache
);
} else if (typeNum === PatternType.SHADING) {
Expand Down
36 changes: 15 additions & 21 deletions src/core/image_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ class BaseLocalCache {
if (this.constructor === BaseLocalCache) {
unreachable("Cannot initialize BaseLocalCache.");
}
if (!options || !options.onlyRefs) {
this._onlyRefs = (options && options.onlyRefs) === true;

if (!this._onlyRefs) {
this._nameRefMap = new Map();
this._imageMap = new Map();
}
this._imageCache = new RefSetCache();
}

getByName(name) {
if (this._onlyRefs) {
unreachable("Should not call `getByName` method.");
}
const ref = this._nameRefMap.get(name);
if (ref) {
return this.getByRef(ref);
Expand Down Expand Up @@ -97,10 +102,6 @@ class LocalFunctionCache extends BaseLocalCache {
super({ onlyRefs: true });
}

getByName(name) {
unreachable("Should not call `getByName` method.");
}

set(name = null, ref, data) {
if (!ref) {
throw new Error('LocalFunctionCache.set - expected "ref" argument.');
Expand Down Expand Up @@ -134,25 +135,18 @@ class LocalGStateCache extends BaseLocalCache {
}

class LocalTilingPatternCache extends BaseLocalCache {
set(name, ref = null, data) {
if (typeof name !== "string") {
throw new Error(
'LocalTilingPatternCache.set - expected "name" argument.'
);
}
if (ref) {
if (this._imageCache.has(ref)) {
return;
}
this._nameRefMap.set(name, ref);
this._imageCache.put(ref, data);
return;
constructor(options) {
super({ onlyRefs: true });
}

set(name = null, ref, data) {
if (!ref) {
throw new Error('LocalTilingPatternCache.set - expected "ref" argument.');
}
// name
if (this._imageMap.has(name)) {
if (this._imageCache.has(ref)) {
return;
}
this._imageMap.set(name, data);
this._imageCache.put(ref, data);
}
}

Expand Down