Repository navigation
fix(font): wrong numbers once the glyph cache fills; keep long strings out of it - #54
Open
serpentblade wants to merge 2 commits into
Open
serpentblade wants to merge 2 commits into
serpentblade wants to merge 2 commits into
Conversation
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>
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.
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.
decodeUtf8caches glyphs by text. PHP stores a key like"1"as the integer1, 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:
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