Repository navigation
A PDF font's ToUnicode map is read in one linear pass, not rescanned from each block opening - #339
Merged
Merged
Conversation
…from each block opening _to_unicode found each beginbfchar/beginbfrange block with a lazy regex, which on a map of openings with no close rescans to the end from every opening: an 88 KB map took 3.7 s per page, and the map is re-read for each page that uses the font, so a small PDF held the extractor's one slot until the timeout. _blocks finds each opening and the first close after it with bytes.find, starting where the last block ended; the blocks it returns are the regex's. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):
The reference extractor reads a PDF font's ToUnicode map in time linear in its size, so a PDF whose map opens blocks it never closes no longer holds the extractor until its timeout.
Why
pdflayout._to_unicodefound eachbeginbfchar … endbfcharandbeginbfrange … endbfrangeblock withre.finditer(rb"beginbfchar(.*?)endbfchar", data, re.S). On a map that repeats the opening with no close, the regex tries every opening and scans from it to the end of the data, so the work grows with the square of the map's size: 22 KB took 0.21 s, 44 KB 0.94 s, 88 KB 3.7 s. The map is read up to 1 MiB, and_Widthsre-reads it for every page that uses the font, so a few KB of Flate-compressed map spread over a few dozen pages held the extractor's one slot (default concurrency 1) untilLOOM_EXTRACTOR_TIMEOUT_S._blocks(data, begin, end)finds each opening and the first close after it withbytes.find, starting each search where the last block ended, and stops at the first opening with no close. It returns the same blocks the regex did: a block runs from its opening to the first close after it, an inner opening included.Scope and risk
One function in the reference extractor. The bfchar/bfrange parsing of each block is unchanged.
Compatibility, rollout and rollback
Tests
test_an_unclosed_unicode_map_block_is_read_in_linear_time(bfchar and bfrange): four times the openings must cost under eight times the time. Onmainit fails (assert 3.85 < 8 * 0.259 + 0.05); it passes here. The regex runs in C, which a tracer does not slow, so the ratio holds under coverage.test_a_map_block_runs_to_its_first_close: an inner opening stays part of its block, as with the regex.tests/unit/test_document_extraction_pdf_layout.py: 35 passed.test_document_extraction*.py: all pass except the fourtest_the_delimiter_is_sniffed_in_bounded_timecases, which fail onmaintoo on this machine (the CSV sniffer, a separate fix).AI assistance in the code (leave one; writing this description with AI is fine):
AI-generated (an agent wrote most of the diff): Claude Code; the timings and the equivalence with the regex were checked by hand.
I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make format-check,make lint,make typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.main; no merged migration is edited.frontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code