2x text -> 40x slowdown!
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?
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?
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...
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....
(I guess something's gotten snarled up in the node.js stream.Readable?)
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);
}
}
I knocked it down further by killing the call to addPageBreaksIfNecessary, which doesn't seem to do anything in this particular case....
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?
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.)
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.
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...
Just do a CPU profile - it will show the time spend in garbage collection: https://developer.chrome.com/devtools/docs/cpu-profiling
Awesome -- I'll give that a shot tomorrow
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:

But that's probably no good. Do we just chalk this one up to "not reproducible?"
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?
That's a good suggestion. Wanna try a pull request?
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.
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?
Sorry for the considerable delay -- yeah, I'm happy to chat about this if that would help. I'll send along an email.
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.
Hello,
Is there anyway I could help with this issue ?
Thanks.