Skip to content

Commit f6df9f1

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 dea35ae commit f6df9f1

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
@@ -378,24 +378,46 @@ class AlternateCS extends ColorSpace {
378378
: new Uint8ClampedArray(baseNumComps * count);
379379
const numComps = this.numComps;
380380

381+
const tintCache = new Map();
382+
// Use an integer cache-key when possible, since that's a lot faster than
383+
// string concatenation.
384+
const int32Key =
385+
numComps <= 3 &&
386+
(src instanceof Uint8Array || src instanceof Uint8ClampedArray);
387+
381388
const scaled = new Float32Array(numComps);
382389
const tinted = new Float32Array(baseNumComps);
383-
let i, j;
390+
let i, j, key, val;
384391

385392
for (i = 0; i < count; i++) {
393+
key = int32Key ? 0 : "";
394+
386395
for (j = 0; j < numComps; j++) {
387-
scaled[j] = src[srcOffset++] * scale;
396+
val = src[srcOffset++];
397+
scaled[j] = val * scale;
398+
399+
key += int32Key ? val << (8 * j) : val + "_";
388400
}
389-
tintFn(scaled, 0, tinted, 0);
390-
if (usesZeroToOneRange) {
391-
for (j = 0; j < baseNumComps; j++) {
392-
baseBuf[pos++] = tinted[j] * 255;
393-
}
394-
} else {
395-
base.getRgbItem(tinted, 0, baseBuf, pos);
401+
402+
const cached = tintCache.get(key);
403+
if (cached) {
404+
baseBuf.set(cached, pos);
396405
pos += baseNumComps;
406+
} else {
407+
tintFn(scaled, 0, tinted, 0);
408+
409+
if (usesZeroToOneRange) {
410+
for (j = 0; j < baseNumComps; j++) {
411+
baseBuf[pos++] = tinted[j] * 255;
412+
}
413+
} else {
414+
base.getRgbItem(tinted, 0, baseBuf, pos);
415+
pos += baseNumComps;
416+
}
417+
tintCache.set(key, baseBuf.subarray(pos - baseNumComps, pos).slice());
397418
}
398419
}
420+
tintCache.clear();
399421

400422
if (!isPassthrough) {
401423
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)