Skip to content

Restore the common-type magic fast path without preempting subtype matchers - #174

Merged
jeremy merged 1 commit into
mainfrom
common-magic-fast-path
Aug 22, 2026
Merged

jeremy merged 1 commit into
mainfrom
common-magic-fast-path

Conversation

@jeremy

@jeremy jeremy commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

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:

Fixture Before #172 After #172 (main) This PR
JPEG 25 reads, index 20 255 reads, index 137 25 reads, index 20
PNG 26, index 21 256, index 138 26, index 21
GIF 28, index 22 258, index 139 28, index 22
PDF 72, index 38 293, index 152 85, index 45
MP3 71, index 37 292, index 151 84, index 44
CR2 31 → image/tiff (the #141 bug) 176 → image/x-canon-cr2 32 → image/x-canon-cr2, index 23

(reads = calls to IO#read during Marcel::MimeType.for; measured with a counting StringIO over 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-cr2 with image/tiff; audio/mp4, video/mp4, video/x-m4v, image/heif, image/heif-sequence, image/heic, image/heic-sequence, image/x-canon-cr3 with video/quicktime; the Ogg video and audio codecs with video/ogg / audio/ogg; application/x-tika-msworks-spreadsheet with application/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.
  • The CR2/CRW fixtures from Probe common magic types within their priority band #172 still pass, so [Bug] Cannon .CR2 file is not properly detected #141 stays fixed.

bundle exec rake: 605 runs green, tables:check clean. Independent of #173: a synthetic merge of main + #173 + #174 applies cleanly in either order (the two PRs insert at distinct anchors), runs 647 tests green, and regenerates tables.rb byte-identically.

…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
jeremy force-pushed the common-magic-fast-path branch from 69c9b26 to ef1cbbd Compare August 21, 2026 17:20
@jeremy
jeremy merged commit 90855e0 into main Aug 22, 2026
20 checks passed
@jeremy
jeremy deleted the common-magic-fast-path branch August 22, 2026 05:42
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