Skip to content

Commit 6db949f

Browse files
Snuffleupaguspull[bot]
authored andcommitted
Convert the renderTasks, used in PDFPageProxy.render/PDFPageProxy.getOperatorList, to a Set
When removing tasks we're currently forced to *indirectly* iterate through the array, which can be avoided by using a Set instead. Furthermore, we can also (slightly) modernize the code responsible for initializing the `renderTasks`.
1 parent 8dc3715 commit 6db949f

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/display/api.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,10 +1287,7 @@ class PDFPageProxy {
12871287
}
12881288

12891289
const complete = error => {
1290-
const i = intentState.renderTasks.indexOf(internalRenderTask);
1291-
if (i >= 0) {
1292-
intentState.renderTasks.splice(i, 1);
1293-
}
1290+
intentState.renderTasks.delete(internalRenderTask);
12941291

12951292
// Attempt to reduce memory usage during *printing*, by always running
12961293
// cleanup once rendering has finished (regardless of cleanupAfterRender).
@@ -1335,10 +1332,7 @@ class PDFPageProxy {
13351332
pdfBug: this._pdfBug,
13361333
});
13371334

1338-
if (!intentState.renderTasks) {
1339-
intentState.renderTasks = [];
1340-
}
1341-
intentState.renderTasks.push(internalRenderTask);
1335+
(intentState.renderTasks ||= new Set()).add(internalRenderTask);
13421336
const renderTask = internalRenderTask.task;
13431337

13441338
Promise.all([
@@ -1373,10 +1367,7 @@ class PDFPageProxy {
13731367
if (intentState.operatorList.lastChunk) {
13741368
intentState.opListReadCapability.resolve(intentState.operatorList);
13751369

1376-
const i = intentState.renderTasks.indexOf(opListTask);
1377-
if (i >= 0) {
1378-
intentState.renderTasks.splice(i, 1);
1379-
}
1370+
intentState.renderTasks.delete(opListTask);
13801371
}
13811372
}
13821373

@@ -1392,8 +1383,7 @@ class PDFPageProxy {
13921383
opListTask = Object.create(null);
13931384
opListTask.operatorListChanged = operatorListChanged;
13941385
intentState.opListReadCapability = createPromiseCapability();
1395-
intentState.renderTasks = [];
1396-
intentState.renderTasks.push(opListTask);
1386+
(intentState.renderTasks ||= new Set()).add(opListTask);
13971387
intentState.operatorList = {
13981388
fnArray: [],
13991389
argsArray: [],
@@ -1521,7 +1511,7 @@ class PDFPageProxy {
15211511
return false;
15221512
}
15231513
for (const { renderTasks, operatorList } of this._intentStates.values()) {
1524-
if (renderTasks.length !== 0 || !operatorList.lastChunk) {
1514+
if (renderTasks.size > 0 || !operatorList.lastChunk) {
15251515
return false;
15261516
}
15271517
}
@@ -1568,8 +1558,8 @@ class PDFPageProxy {
15681558
intentState.operatorList.lastChunk = operatorListChunk.lastChunk;
15691559

15701560
// Notify all the rendering tasks there are more operators to be consumed.
1571-
for (let i = 0; i < intentState.renderTasks.length; i++) {
1572-
intentState.renderTasks[i].operatorListChanged();
1561+
for (const internalRenderTask of intentState.renderTasks) {
1562+
internalRenderTask.operatorListChanged();
15731563
}
15741564

15751565
if (operatorListChunk.lastChunk) {
@@ -1618,8 +1608,8 @@ class PDFPageProxy {
16181608
// Mark operator list as complete.
16191609
intentState.operatorList.lastChunk = true;
16201610

1621-
for (let i = 0; i < intentState.renderTasks.length; i++) {
1622-
intentState.renderTasks[i].operatorListChanged();
1611+
for (const internalRenderTask of intentState.renderTasks) {
1612+
internalRenderTask.operatorListChanged();
16231613
}
16241614
this._tryCleanup();
16251615
}
@@ -1653,7 +1643,7 @@ class PDFPageProxy {
16531643
if (!force) {
16541644
// Ensure that an Error occurring in *only* one `InternalRenderTask`, e.g.
16551645
// multiple render() calls on the same canvas, won't break all rendering.
1656-
if (intentState.renderTasks.length !== 0) {
1646+
if (intentState.renderTasks.size > 0) {
16571647
return;
16581648
}
16591649
// Don't immediately abort parsing on the worker-thread when rendering is

0 commit comments

Comments
 (0)