Possibility to improve parsing perfomance by not using uniqid() and add an early return?
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.7k
- Forks
- 579
- Avg merge
- 2m
- Merged PRs (30d)
- 1
Description
I parse PDFs where strings are encoded in the bracket notation like
[(Xxxx)1.7(a)1.2(tt )-10.1(\374)1.1(be)1.4(r )-8.5(Xxxx)-7.8(xx)-9.5(xxx)2.2(xxx)-1.1(x)-9(xxx)2( )]TJ
I have an idea to improve parsing performance which reduced parsing time from 1.65 seconds to 0.54 seconds in my example:
First, I observed a significant performance increase when calling Page::getTextArray() on documents with strings like this with a simpler placeholder in PDFObject::formatContent() like
$id = "S_$i"; where $i is a simple counting integer instead of $id = uniqid('STRING_', true);
Reason for this are the latter numerous calls to str_replace() in order to replace back the placeholders with the original content. The complex placeholder generated by uniqid() seems to slow down the calls.
Is there any reason for using this complex placeholder or can it be replaced by a much simpler one?
Second, I recognized 2 (more or less) subsequent calls to PDFObject->getTextArray($this) in Page::getTextArray() around line where the first one might immediatlly return:
Is there a reason for not returning in the try case and instead return the same call later
?
Thank you for your time!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in PDFObject::formatContent() to inspect how uniqid() placeholders are created and restored, then review src/Smalot/PdfParser/Page.php around lines 346-350 and 365 for the repeated getTextArray() calls. Reproduce the reported parsing example and compare behavior and timing before and after each proposed change. Done means preserved PDF text extraction with measurable performance improvement.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- performance
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100