Repository navigation
Restore the common-type magic fast path without preempting subtype matchers - #174
Merged
Merged
Conversation
…tchers #172 fixed CR2 detection by folding the common-types rank into the priority sort, so common types were only probed first within their own priority band. That put the ~120 matchers Tika ranks above 50 ahead of every common type: detecting a JPEG went from 25 reads to 255, PNG from 26 to 256, PDF from 72 to 293, with wall time rising accordingly on every call. Move the common types back to the front of the table, but have each one carry along the higher-priority matchers for its own subtypes, so promoting a common type can never preempt a more specific verdict for the same content. image/x-canon-cr2 (priority 60, a subtype of image/tiff) still probes immediately ahead of image/tiff, the MP4 and HEIF family ahead of video/quicktime's broad ftyp match, and the Ogg codecs ahead of their containers. Parent links are now collected for every type during generation, not only glob-bearing ones, so the subtype walk sees the whole hierarchy. JPEG, PNG and GIF are back to 25, 26 and 28 reads at the same table positions as before #172; PDF takes 85, the cost of the carried matchers. A generator test pins the ordering rule and a read-budget test pins the fast path.
jeremy
force-pushed
the
common-magic-fast-path
branch
from
August 21, 2026 17:20
69c9b26 to
ef1cbbd
Compare
This was referenced Aug 22, 2026
Merged
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.
Follow-up to #172, which fixed Canon CR2 detection (#141) but regressed detection cost for every common type.
The regression
#172 folded the common-types rank into the priority sort so common types probed first only within their priority band. Tika ranks 121 matchers above 50 (94 of them at 60), so every common type moved behind all of them:
main)(
reads= calls toIO#readduringMarcel::MimeType.for; measured with a countingStringIOover the suite's fixtures.)The rule
Common types go back to the front of the table, but each one carries along the higher-priority matchers for its own subtypes (parent closure over
sub-class-of, now collected for every type rather than only glob-bearing ones). Promoting a common type therefore cannot preempt a more specific verdict for the same content — which is exactly the failure #141 was: CR2 is a subtype of TIFF whose priority-60 matcher Tika ranks above TIFF's.What rides along, from the data:
image/x-canon-cr2withimage/tiff;audio/mp4,video/mp4,video/x-m4v,image/heif,image/heif-sequence,image/heic,image/heic-sequence,image/x-canon-cr3withvideo/quicktime; the Ogg video and audio codecs withvideo/ogg/audio/ogg;application/x-tika-msworks-spreadsheetwithapplication/vnd.ms-excel. 22 matchers, which is the PDF/MP3 difference above.What this deliberately does not cover — same exposure as the years before #172: an unrelated higher-priority type whose matcher happens to overlap a common type's bytes. None is known; if one surfaces it is a subtype-link or data fix, not a reason to walk 120 matchers on every JPEG.
Tests
generate_tables_test: synthetic data pins the ordering —[common, subtype@60 of common, common, unrelated@60, other@50]→jpeg, tiff-flavour, tiff, unrelated, other(via a two-hop parent chain).magic_test: read-budget regression for jpeg/png/gif (≤ 48) and pdf (≤ 128), with measured values and the one legitimate reason to raise them (runtime matchers registered ahead of the tables) in the comment.bundle exec rake: 605 runs green,tables:checkclean. Independent of #173: a synthetic merge ofmain+ #173 + #174 applies cleanly in either order (the two PRs insert at distinct anchors), runs 647 tests green, and regeneratestables.rbbyte-identically.