pdfmake icon indicating copy to clipboard operation
pdfmake copied to clipboard

2x text -> 40x slowdown!

Open hujhax opened this issue 11 years ago • 66 comments

So this is odd -- I've got a project where I'm generating text PDFs, nothing too fancy. I've hit some sort of threshold, though -- I went from making a ~20-page document to making a ~40-page document, and the PDF-generating time spiked from ~3 seconds to ~2 minutes.

I've got a repository up (with obfuscated text files, whee NDAs) here. You can click to generate the "slow" or "fast" documents, with basic instrumenting telling you how long each one takes.

Have I hit some sort of known performance landmine here? Does pdfMake just take a lot longer once you get above a particular input-object size? Either way, is there a way to optimize around this?

hujhax avatar May 04 '15 22:05 hujhax

All that said, it looks like it may be choking on the number of items in the "content" array. I tried charting performance time against the number of lines in the (prettified) doc object, and I got a pretty good linear correlation. Is pdfMake just not a viable solution once you get above a certain number of text objects?

hujhax avatar May 04 '15 22:05 hujhax

Hmm -- on a similar pdfMake run, I tried running it on the entire document, and then tried splitting the document into three chunks and running pdfMake on each:

Total: make-report: 132281.000ms
Part #1: make-report: 1178.000ms
Part #2: make-report: 4923.000ms
Part #3: make-report: 3994.000ms

... so that doesn't quite look linear...

hujhax avatar May 04 '15 23:05 hujhax

For what it's worth, it looks like the slowdown happens after the doc.end(); in _createDoc -- everything prior to that is pretty snappy. On the "slow" pdf, the 'data' events get emitted very, very slowly. Not sure what pdfMake is doing at that point in the process....

hujhax avatar May 04 '15 23:05 hujhax

(I guess something's gotten snarled up in the node.js stream.Readable?)

hujhax avatar May 05 '15 00:05 hujhax

Huh -- I did manage to knock it down quite a bit by making this adjustment around line 25,900 in pdfmake.js:

function flow(stream) {
  var state = stream._readableState;
  debug('flow', state.flowing);
  if (state.flowing) {
    do {
      var chunk = stream.read(Number.MAX_SAFE_INTEGER); // grab MAX_SAFE_INTEGER bytes
    } while (null !== chunk && state.flowing);
  }
}

hujhax avatar May 05 '15 04:05 hujhax

I knocked it down further by killing the call to addPageBreaksIfNecessary, which doesn't seem to do anything in this particular case....

hujhax avatar May 05 '15 04:05 hujhax

If addPageBreaksIfNeccessary is an issue, it should work now - if you don't specify a pageBreakBefore function.

From the description you give, I to me it sounds like this could be a GC issue. Could you double check that?

jthoenes avatar May 05 '15 08:05 jthoenes

Thanks for following up on this! I assume "GC" = "Google Chrome"? I tried this in Firefox and got more sane results -- the 'fast' PDF showed up in 52 seconds, whereas the 'slow' one took 1:47. (Also re-confirmed that I was getting the same slowdown from Chrome with all my extensions turned off.)

hujhax avatar May 05 '15 19:05 hujhax

No, I mean GC = garbage collection.

If your Chrome takes up to much memory, it'll try harder to release it. Using more CPU to find disposable javascript objects.

jthoenes avatar May 05 '15 21:05 jthoenes

Ah, gotcha. Not 100% sure how to check on whether it's garbage collection. I did check in the Chrome task manager, and saw that the slow case is using more memory (120MB versus 65MB) and on my machine pegs the CPU at 33% for a long while. If you can point me to the right sort of heap-profiling to do in this case, I can investigate further...

hujhax avatar May 06 '15 06:05 hujhax

Just do a CPU profile - it will show the time spend in garbage collection: https://developer.chrome.com/devtools/docs/cpu-profiling

jthoenes avatar May 06 '15 07:05 jthoenes

Awesome -- I'll give that a shot tomorrow

hujhax avatar May 06 '15 07:05 hujhax

Ugh. Just my luck, the profiler appears to hang on the machine that's demonstrating the slowdown. It works fine on a machine where both files take about 45s:

image

But that's probably no good. Do we just chalk this one up to "not reproducible?"

hujhax avatar May 06 '15 23:05 hujhax

Circling back to this one, I have no good way to profile this particular slowdown. Looks like "don't call addPageBreaksIfNecessary when you don't need to" has been taken care of. The only other obvious performance glitch this one uncovered was to replace the one-chunk-at-a-time "flow" with returning the whole file at once (as per https://github.com/bpampuch/pdfmake/issues/280#issuecomment-98944706), which cut the processing time by more than half. Maybe we could spin that off to a separate discussion and close this one?

hujhax avatar May 17 '15 01:05 hujhax

That's a good suggestion. Wanna try a pull request?

jthoenes avatar May 18 '15 15:05 jthoenes

Lord knows I'd like to, but the problem here is that I'm making that change to the readable-stream module, forcing its flow(stream) function to return the entire pdf object at once (which, again, gives us a massive performance gain on large files).

It's not obvious to me what change I could make to the pdfMake source to effect that same change to the readable-stream behavior.

hujhax avatar May 23 '15 18:05 hujhax

Do you want to chat at some point in time so we can figure out, if there's a good path to a solution to this issue?

jthoenes avatar May 26 '15 19:05 jthoenes

Sorry for the considerable delay -- yeah, I'm happy to chat about this if that would help. I'll send along an email.

hujhax avatar Jun 12 '15 02:06 hujhax

Hi,

Is there any news about this issue ? I have the same one and don't know what to do. Is there a way to automatically split the pdf generation on smaller chunks and merge them at the end ?

Thanks.

ps: Awesome work, thanks a lot.

no-more avatar Aug 30 '15 06:08 no-more

Hello,

Is there anyway I could help with this issue ?

Thanks.

no-more avatar Sep 02 '15 11:09 no-more