Skip to content

fix(font): wrong numbers once the glyph cache fills; keep long strings out of it - #54

Open
serpentblade wants to merge 2 commits into
dskripchenko:mainfrom
serpentblade:fix/decode-cache-numeric-keys
Open

serpentblade wants to merge 2 commits into
dskripchenko:mainfrom
serpentblade:fix/decode-cache-numeric-keys

Conversation

@serpentblade

@serpentblade serpentblade commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Two changes to PdfFont::decodeUtf8's glyph cache: a fix for wrong numbers once it fills, and a cut in what it holds.

Wrong numbers once the cache fills

With an embedded font, a document of more than ~2,048 distinct words starts printing wrong numbers. A 100-page invoice showed "42", "51" or "21" in place of "1" in 2,900 of its 5,000 quantity cells, from page 43 on.

decodeUtf8 caches glyphs by text. PHP stores a key like "1" as the integer 1, and the eviction (array_shift) renumbers integer keys, so "1" could come back with the glyphs of "15". Eviction now unsets the oldest key instead, which leaves the others alone.

Long strings out of the large cache

The cache held up to 2,048 results per font whatever their length, and each costs ~400 bytes per character. When whole lines are drawn as one run, it fills with strings that rarely come back: an outside caller drawing 6,900 runs held 37 MB of its 38 MB there.

Strings up to 32 bytes (words, numbers, labels) keep the large cache. Longer ones go in a cache of the 16 most recent, which catches the repeat decodes of a run while it is drawn (shaped, then encoded).

Replaying every lookup from two documents in Liberation Sans:

Document Misses, before Misses, after Flat cap of 8 Peak memory
Prose (USAGE.md + CHANGELOG) 8.9% 8.7% 53.7% 23.4 → 13.3 MB
100-page invoice 2.6% 2.6% 22.4% 52.2 → 52.2 MB

Render time is unchanged, and output is byte-identical on both.

Six tests: two for the eviction (both failing before the fix), four for the two caches. Full suite and phpstan pass on PHP 8.2 and 8.4. Still merges cleanly with #55 and #56.

🤖 Generated with Claude Code

PdfFont::decodeUtf8 caches glyphs by text, evicting the oldest entry
with array_shift once 2048 are held. PHP stores a key like "1" as the
integer 1, and array_shift renumbers integer keys, so after the first
eviction a numeric word could be served another one's glyphs: "1"
came back as "15", "21" or "42". With an embedded font, a document of
more than ~2048 distinct words printed wrong numbers from then on — a
100-page invoice showed 2,900 of its 5,000 quantities wrong, and line
breaks were measured with the wrong widths.

Eviction now unsets the first key, which leaves the others alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
decodeUtf8 held up to 2,048 results per font whatever their length,
and each costs ~400 bytes per character. When whole lines are drawn as
one run, the cache fills with strings that rarely come back: an
outside caller drawing 6,900 runs held 37 MB of its 38 MB there.

Strings up to 32 bytes - words, numbers, labels - keep the large
cache. Longer ones go in a cache of the 16 most recent, which catches
the repeat decodes of a run while it is drawn (shaped, then encoded).

Replaying every lookup from a prose document and a 100-page invoice,
the miss rate is unchanged (8.9% -> 8.7%, 2.6% -> 2.6%); a flat cap of
8 would have raised it to 54% and 22%. Prose peak memory goes from
23.4 MB to 13.3 MB at the same speed; output is byte-identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@serpentblade serpentblade changed the title fix(font): wrong numbers printed once the glyph cache fills fix(font): wrong numbers once the glyph cache fills; keep long strings out of it Nov 3, 2026
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