perf(pdf): skip the per-character word walk for text boxes with no overlapping link
KR
5h ago+8+8+0123456789-6-6-0123456789
Summary
When a page has any link annotations, process_page_layout_from_pdfminer() and _pdfminer_page_elements() called get_words_from_obj() for every LTTextBox on the page, then used its result only for the annotations that overlap that box. get_words_from_obj() walks every character of the box. It now runs only when check_annotations_within_element() returned at least one annotation. get_words_from_obj() has no side effects, so output is unchanged.
Performance
Walltime, pytest-codspeed run locally (Apple Silicon), process_page_layout_from_pdfminer over all 16 pages of example-docs/pdf/layout-parser-paper.pdf (109 link annotations), repeated runs:
| before | after | |
|---|---|---|
| all pages | ~11.0 ms | ~6.1 ms |
cProfile had get_words_from_obj at ~41% of this function before the change.
Validation
test_unstructured/partition/pdf_image/test_pdfminer_processing.py,test_pdf.pyandtest_merge_elements.pypass.