pdfmake icon indicating copy to clipboard operation
pdfmake copied to clipboard

Using `pageBreakBefore` seems slower than necessary

Open dahjelle opened this issue 8 years ago • 13 comments

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.measureNode is 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?

dahjelle avatar Jun 22 '18 16:06 dahjelle

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;

Llorx avatar May 06 '19 12:05 Llorx

I've created a PR with this improvement: https://github.com/bpampuch/pdfmake/pull/1718

Llorx avatar May 06 '19 13:05 Llorx

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)

liborm85 avatar Feb 09 '20 12:02 liborm85

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());
  }

liborm85 avatar Jul 30 '20 14:07 liborm85

This is slowing down my nodejs pdf generation by a significant amount. From 14 seconds goes up to 120 seconds.

erald14 avatar Oct 06 '20 21:10 erald14

@erald14 Attach runnable example for reproduce.

liborm85 avatar Oct 10 '20 15:10 liborm85

Created PR for this: #2202 edit. And the same for v0.1: #2203

psaawkel avatar Mar 04 '21 11:03 psaawkel

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

bigDocumentPageBreakBefore.zip

Seemeile avatar Mar 28 '22 13:03 Seemeile

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? :(

alexvrv avatar Apr 27 '22 06:04 alexvrv

Will there be any progress soon? @liborm85 if I can support you, just hit me up.

AlexanderNo avatar Aug 05 '22 07:08 AlexanderNo

We are also experiencing this issue. Any update on when a fix will be available?

msesun avatar Dec 15 '22 06:12 msesun

Latest optimalization is available in beta version 0.3 as mentioned above. Further optimizations are welcome but create a Pull request.

liborm85 avatar Dec 17 '22 13:12 liborm85

Final version 0.3.0 released.

liborm85 avatar Jan 02 '26 09:01 liborm85