smalot / smalot/pdfparser

Possibility to improve parsing perfomance by not using uniqid() and add an early return?

Open
#712 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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:

https://github.com/smalot/pdfparser/blob/a19d5550531796aa1d2d351a00066e422c848123/src/Smalot/PdfParser/Page.php#L346-L350

Is there a reason for not returning in the try case and instead return the same call later

https://github.com/smalot/pdfparser/blob/a19d5550531796aa1d2d351a00066e422c848123/src/Smalot/PdfParser/Page.php#L365

?

Thank you for your time!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.