Using `pageBreakBefore` seems slower than necessary
Can the pageBreakBefore option be sped up, perhaps with caching or partial document layout?
I'm using the pageBreakBefore option to try prevent page breaks in certain scenarios in my application. I've noticed that whenever my pageBreakBefore function returns true, it causes the layoutBuiler.tryLayoutDocument function to run again. I have a 35-page document that ends up running tryLayoutDocument 22 times, taking ~200 ms each time, for a total run time of ~5.8s (including the other pdfmake functions). Without pageBreakBefore, the run time is about 1.1s. With larger documents, it gets worse.
I realize that the remainder of a document has to be laid out again after a page break, since everything may have shifted. Not really understanding pdfmake's internals, I wonder if some improvements are possible:
- Could the check for a page break be done during the layout process? I suppose one would have to back-track a node or two in the document, but that seems much less expensive than re-laying out the entire document.
- It looks like
DocMeasure.measureNodeis the expensive function in laying out the document. Would it be possible to cache/memoize results for this function, so that things before the page break wouldn't have to be re-computed? - Other?
A year with this without answer. Not wanting to be pushy, but just some thoughts about this will be awesome.
I'm trying to optimize a bit.
By cutting linearNodeList.some() to a for-loop I got some improvement (from 18 seconds to 15 seconds). Working on arrays with methods instead of for loops is less performant (forEach, some, filter and such) and having a library with so big array loops I would suggest to avoid such methods will decrease performance by a nice %. Having a good performant library must be also an objective, I guess.
EDIT: I cut from 18s to 13s (a nice improvement) by removing the maps, the slices and the filters inside addPageBreaksIfNecessary.
Changed linearNodeList.some(... code to this:
var isBreak = false;
for (var index = 0; index < linearNodeList.length; index++) {
var node = linearNodeList[index];
if (node.pageBreak !== 'before' && !node.pageBreakCalculated) {
node.pageBreakCalculated = true;
var pageNumber = node.nodeInfo.pageNumbers[0];
var followingNodesOnPage = [];
var nodesOnNextPage = [];
var previousNodesOnPage = [];
for (var ii = index + 1, l = linearNodeList.length; ii < l; ii++) {
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber) > -1) {
followingNodesOnPage.push(linearNodeList[ii].nodeInfo);
}
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber + 1) > -1) {
nodesOnNextPage.push(linearNodeList[ii].nodeInfo);
}
}
for (var ii = 0; ii < index; ii++) {
if (linearNodeList[ii].nodeInfo.pageNumbers.indexOf(pageNumber) > -1) {
previousNodesOnPage.push(linearNodeList[ii].nodeInfo);
}
}
if (pageBreakBeforeFct(node.nodeInfo, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage)) {
node.pageBreak = 'before';
isBreak = true;
break;
}
}
}
return isBreak;
I've created a PR with this improvement: https://github.com/bpampuch/pdfmake/pull/1718
Optimalization implemented by https://github.com/bpampuch/pdfmake/pull/1718 (in master branch) and by commit https://github.com/bpampuch/pdfmake/commit/38f92c2d5630299819f8cabeab8d23453ef95fe0 (in 0.1 branch)
Change parameters of pageBreakBefore function for support "lazy-calculate" nodes implemented by commit https://github.com/bpampuch/pdfmake/commit/380b4d19135482d0d9d21d0a9f0530863c9d44d6 in maser (for version 0.2).
Before:
pageBreakBefore: function(currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) {
console.log(followingNodesOnPage);
console.log(nodesOnNextPage);
console.log(previousNodesOnPage);
}
Now (version form master):
pageBreakBefore: function(currentNode, nodesObject) {
console.log(nodesObject.getFollowingNodesOnPage());
console.log(nodesObject.getNodesOnNextPage());
console.log(nodesObject.getPreviousNodesOnPage());
}
This is slowing down my nodejs pdf generation by a significant amount. From 14 seconds goes up to 120 seconds.
@erald14 Attach runnable example for reproduce.
Created PR for this: #2202 edit. And the same for v0.1: #2203
We also noticed a huge performance drop when using pageBreakBefore function in release 0.2.4 with big documents. See our attached pdfdefinition example and our calculatePageBreak function, which is called inside pageBreakBefore callback of pdfmake's createPdf function. Everything is rendered in browser.
Now the pdf is rendered in 43327ms with pageBreakBefore function returning true 82 times. Previously, without using pageBreakBefore, we were able to render the pdf in 4354ms.
Do you have any recommendations how we can improve our performance? Thank you
Any update about the release? I have a 200 pages PDF and it takes 8 minutes to render and I would really need this...
Pretty please? :(
Will there be any progress soon? @liborm85 if I can support you, just hit me up.
We are also experiencing this issue. Any update on when a fix will be available?
Latest optimalization is available in beta version 0.3 as mentioned above. Further optimizations are welcome but create a Pull request.
Final version 0.3.0 released.