mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 01:58:06 +02:00
Improve caching of shading patterns. (bug 1721949)
The PDF in bug 1721949 uses many unique pattern objects that references the same shading many times. This caused a new canvas pattern to be created and cached many times driving up memory use. To fix, I've changed the cache in the worker to key off the shading object and instead send the shading and matrix separately. While that worked well to fix the above bug, there could be PDFs that use many shading that could cause memory issues, so I've also added a LRU cache on the main thread for canvas patterns. This should prevent memory use from getting too high.
This commit is contained in:
parent
777d890268
commit
c836e1f0fb
4 changed files with 85 additions and 60 deletions
|
@ -1315,20 +1315,17 @@ class PartialEvaluator {
|
|||
}
|
||||
|
||||
parseShading({
|
||||
keyObj,
|
||||
shading,
|
||||
resources,
|
||||
localColorSpaceCache,
|
||||
localShadingPatternCache,
|
||||
matrix = null,
|
||||
}) {
|
||||
// Shadings and patterns may be referenced by the same name but the resource
|
||||
// dictionary could be different so we can't use the name for the cache key.
|
||||
let id = localShadingPatternCache.get(keyObj);
|
||||
let id = localShadingPatternCache.get(shading);
|
||||
if (!id) {
|
||||
var shadingFill = Pattern.parseShading(
|
||||
shading,
|
||||
matrix,
|
||||
this.xref,
|
||||
resources,
|
||||
this.handler,
|
||||
|
@ -1337,7 +1334,7 @@ class PartialEvaluator {
|
|||
);
|
||||
const patternIR = shadingFill.getIR();
|
||||
id = `pattern_${this.idFactory.createObjId()}`;
|
||||
localShadingPatternCache.set(keyObj, id);
|
||||
localShadingPatternCache.set(shading, id);
|
||||
this.handler.send("obj", [id, this.pageIndex, "Pattern", patternIR]);
|
||||
}
|
||||
return id;
|
||||
|
@ -1402,14 +1399,12 @@ class PartialEvaluator {
|
|||
const shading = dict.get("Shading");
|
||||
const matrix = dict.getArray("Matrix");
|
||||
const objId = this.parseShading({
|
||||
keyObj: pattern,
|
||||
shading,
|
||||
matrix,
|
||||
resources,
|
||||
localColorSpaceCache,
|
||||
localShadingPatternCache,
|
||||
});
|
||||
operatorList.addOp(fn, ["Shading", objId]);
|
||||
operatorList.addOp(fn, ["Shading", objId, matrix]);
|
||||
return undefined;
|
||||
}
|
||||
throw new FormatError(`Unknown PatternType: ${typeNum}`);
|
||||
|
@ -1942,7 +1937,6 @@ class PartialEvaluator {
|
|||
throw new FormatError("No shading object found");
|
||||
}
|
||||
const patternId = self.parseShading({
|
||||
keyObj: shading,
|
||||
shading,
|
||||
resources,
|
||||
localColorSpaceCache,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue