Skip to content

Commit af02b8e

Browse files
committed
Add a temporary function-cache in AlternateCS.prototype.getRgbBuffer
This supplements, rather than replaces, the existing caching in `PDFFunction.constructPostScript` since that one still makes sense given that functions are cached on the page-level. Using an additional cache helps improve performance because: - There are many fewer function calls, and overall less parsing this way. - For the common case of `Uint8Array`-data we're able to use integer cache-keys, which is a lot faster than string concatenation. This significantly improves performance of the `pr5134` test-case with `isEvalSupported = false` set, and testing locally in the viewer: - With the `master` branch and `isEvalSupported = true`, page 2 renders in approx. 340 milliseconds. - With the `master` branch and `isEvalSupported = false`, page 2 renders in approx. 1200 milliseconds. - With this patch and `isEvalSupported = false`, page 2 renders in approx. 380 milliseconds. While this is obviously still slower, the difference is now small enough that it shouldn't be too much of an issue in practice (compare with PR 18070) and the `pr5134` test-case is an especially "bad" one w.r.t. its PostScript function use.
1 parent e3ea926 commit af02b8e

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

src/core/colorspace.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -636,24 +636,46 @@ class AlternateCS extends ColorSpace {
636636
: new Uint8ClampedArray(baseNumComps * count);
637637
const numComps = this.numComps;
638638

639+
const tintCache = new Map();
640+
// Use an integer cache-key when possible, since that's a lot faster than
641+
// string concatenation.
642+
const int32Key =
643+
numComps <= 3 &&
644+
(src instanceof Uint8Array || src instanceof Uint8ClampedArray);
645+
639646
const scaled = new Float32Array(numComps);
640647
const tinted = new Float32Array(baseNumComps);
641-
let i, j;
648+
let i, j, key, val;
642649

643650
for (i = 0; i < count; i++) {
651+
key = int32Key ? 0 : "";
652+
644653
for (j = 0; j < numComps; j++) {
645-
scaled[j] = src[srcOffset++] * scale;
654+
val = src[srcOffset++];
655+
scaled[j] = val * scale;
656+
657+
key += int32Key ? val << (8 * j) : val + "_";
646658
}
647-
tintFn(scaled, 0, tinted, 0);
648-
if (usesZeroToOneRange) {
649-
for (j = 0; j < baseNumComps; j++) {
650-
baseBuf[pos++] = tinted[j] * 255;
651-
}
652-
} else {
653-
base.getRgbItem(tinted, 0, baseBuf, pos);
659+
660+
const cached = tintCache.get(key);
661+
if (cached) {
662+
baseBuf.set(cached, pos);
654663
pos += baseNumComps;
664+
} else {
665+
tintFn(scaled, 0, tinted, 0);
666+
667+
if (usesZeroToOneRange) {
668+
for (j = 0; j < baseNumComps; j++) {
669+
baseBuf[pos++] = tinted[j] * 255;
670+
}
671+
} else {
672+
base.getRgbItem(tinted, 0, baseBuf, pos);
673+
pos += baseNumComps;
674+
}
675+
tintCache.set(key, baseBuf.subarray(pos - baseNumComps, pos).slice());
655676
}
656677
}
678+
tintCache.clear();
657679

658680
if (!isPassthrough) {
659681
base.getRgbBuffer(baseBuf, 0, count, dest, destOffset, 8, alpha01);

src/core/function.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,11 @@ class PDFFunction {
444444
// seen in our tests.
445445
const MAX_CACHE_SIZE = 2048 * 4;
446446
let cache_available = MAX_CACHE_SIZE;
447-
const tmpBuf = new Float32Array(numInputs);
447+
const input = new Float32Array(numInputs);
448448

449449
return function constructPostScriptFn(src, srcOffset, dest, destOffset) {
450450
let i, value;
451451
let key = "";
452-
const input = tmpBuf;
453452
for (i = 0; i < numInputs; i++) {
454453
value = src[srcOffset + i];
455454
input[i] = value;

test/pdfs/pr5134.pdf.link

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://web.archive.org/web/20140211020222/http://www.coachusa.com/CoachUsaAssets/files/97/route45.pdf

test/test_manifest.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,15 @@
22542254
"lastPage": 1,
22552255
"type": "eq"
22562256
},
2257+
{
2258+
"id": "pr5134",
2259+
"file": "pdfs/pr5134.pdf",
2260+
"md5": "6a701a163472e071a2519348b55cbac1",
2261+
"rounds": 1,
2262+
"link": true,
2263+
"firstPage": 2,
2264+
"type": "eq"
2265+
},
22572266
{
22582267
"id": "issue5599",
22592268
"file": "pdfs/issue5599.pdf",

0 commit comments

Comments
 (0)