Skip to content

Commit

Permalink
Avoid unncessary parsing, in Page.GetStructTree, when no structTree…
Browse files Browse the repository at this point in the history
… is available (PR 13221 follow-up)

It's obviously (a bit) more efficient to return early in `Page.getStructTree`, rather than trying to first "parse" an *empty* structTree-root.

*Somehow I didn't think of this yesterday, but this feels like a much better solution overall; sorry about the churn here!*
  • Loading branch information
Snuffleupagus committed Apr 12, 2021
1 parent 0d2dd6c commit b38c99c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,13 @@ class Page {
const structTreeRoot = await this.pdfManager.ensureCatalog(
"structTreeRoot"
);
return this.pdfManager.ensure(this, "_parseStructTree", [structTreeRoot]);
if (!structTreeRoot) {
return null;
}
const structTree = await this.pdfManager.ensure(this, "_parseStructTree", [
structTreeRoot,
]);
return structTree.serializable;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/core/struct_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,6 @@ class StructTreePage {
}
nodeToSerializable(child, root);
}

if (root.children.length === 0) {
return null;
}
return root;
}
}
Expand Down
12 changes: 3 additions & 9 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,15 +743,9 @@ class WorkerMessageHandler {
});

handler.on("GetStructTree", function wphGetStructTree(data) {
const pageIndex = data.pageIndex;
return pdfManager
.getPage(pageIndex)
.then(function (page) {
return pdfManager.ensure(page, "getStructTree");
})
.then(function (structTree) {
return structTree.serializable;
});
return pdfManager.getPage(data.pageIndex).then(function (page) {
return pdfManager.ensure(page, "getStructTree");
});
});

handler.on("FontFallback", function (data) {
Expand Down

0 comments on commit b38c99c

Please sign in to comment.