OCRmyPDF: [Bug]: Scan time regression in 16.4.3 with `--redo-ocr`
While playing with #1378, I noticed that the initial scanning time with --redo-ocr regressed in v16.4.3 in this commit. The newest version takes ~15x longer to scan the same file even when #1378 is fixed locally.
I can’t upload my local file, but the regression is noticeable with any file with size > 100MB and > 1000 pages. For reference sample-10k.pdf showed 10x regression. Starting from sample-100.pdf, it can be prepared with the following commands:
pdfunite $(for i in {1..10}; do echo sample-100.pdf; done) sample-1k.pdf
pdfunite $(for i in {1..10}; do echo sample-1k.pdf; done) sample-10k.pdf
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (5 by maintainers)
The typical input PDF to ocrmypdf has a lot of images, but not many parseable content streams, which is the only time BUFSIZ comes into play anyway, so this isn’t a typical use case.
My intention is to wait for the pdfminer.six patch to be released in pdfminer.six.
I advise installing ocrmypdf in a virtual environment and using pip to install a patched pdfminer.six and/or ocrmypdf in the meantime.
I’m not inclined to lower BUFSIZ, because it is the maximum value to read, and the token splitting issue is much more painful (wrong behavior and in bizarre, complex ways) than poor performance. I respect pdfminer.six but in the long term I will probably replace that library with something more robust.
@dhdaines In terms of pikepdf and pdfminer.six – lately I’m experimenting with rust PDF libraries and in particular pdfium-render seems far beyond what anything in Python is capable in terms of text rendering and layout capabilities. I’ve had a goal of reducing the number of external binaries ocrmypdf depends on, since that makes installation difficult for less technical users, and generally speaking it looks like the rust ecosystem now has most of what I need. I could pretty much eliminate all mandatory dependencies (tesseract, ghostscript) and some of the optional ones (pngquant) while still allowing optional access to those. So time permitting that is probably the future.
qpdf doesn’t have code to do what pdfminer.six does, so if I were to add it to pikepdf I’d mostly be copying from pdfminer.six.
I’m grateful for pdfminer.six but it is “mature” and dated in many ways.
I agree with the buffered reader approach – separating buffering concerns from lexing and parsing is probably the right appraoch. pdfminer.six’s lexer/parser is complex. After examining it a while, I ended up deciding to make BUFSIZ large rather than actually take on the burden of trying to fix it, so I appreciate your effort in improving it.
Ah, I understand. Yes, basically the code in pdfminer.six is just very ill-suited to a very large BUFSIZ.
My other, less modest, proposal, is to rewrite the ~parser~ lexer to access the input stream directly using
peekfor lookahead, so that the buffering gets done by someone else (presumablyio.BufferedReader). This is probably the ideal solution.I searched for an existing lexer library for Python that will do this properly but can’t seem to find one…
plydefinitely assumes all of the data has been read into memory.