Skip to content

A PDF font's ToUnicode map is read in one linear pass, not rescanned from each block opening - #339

Merged
zlareb1 merged 2 commits into
mainfrom
fix/pdf-cmap-linear-scan
Nov 7, 2026
Merged

zlareb1 merged 2 commits into
mainfrom
fix/pdf-cmap-linear-scan

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 7, 2026

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

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_unicode found each beginbfchar … endbfchar and beginbfrange … endbfrange block with re.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 _Widths re-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) until LOOM_EXTRACTOR_TIMEOUT_S.

_blocks(data, begin, end) finds each opening and the first close after it with bytes.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

  • Wire: none.
  • Operator steps: none; the extractor image picks it up on its next build.
  • Rollback: revert.

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. On main it 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 four test_the_delimiter_is_sniffed_in_bounded_time cases, which fail on main too 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 typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version.
  • The frontend builds and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 2 commits November 7, 2026 08:36
…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]>
@zlareb1
zlareb1 merged commit 3b1404f into main Nov 7, 2026
38 checks passed
@zlareb1
zlareb1 deleted the fix/pdf-cmap-linear-scan branch November 7, 2026 09:13
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant