[
https://issues.apache.org/jira/browse/TIKA-4796?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18099839#comment-18099839
]
ASF GitHub Bot commented on TIKA-4796:
--------------------------------------
gsingers opened a new pull request, #2972:
URL: https://github.com/apache/tika/pull/2972
Backport of #2968 (`65a2f3ab6`) from `main` to `branch_3x`.
JIRA: https://issues.apache.org/jira/browse/TIKA-4796
## What
`MagicDetector` recompiled its `Pattern` on every regex `detect()` call,
even though both inputs to the compile — the pattern bytes and the
case-insensitivity flag — are fixed at construction time. This compiles it once
in the constructor and reuses it.
`Pattern` is immutable and a new `Matcher` is still created per call, so the
regex path remains safe for the shared, long-lived detector instances that
`MimeTypes` hands out.
## Not a cherry-pick
3.x compiles the `Pattern` inline in `detect()`; `main` had already
extracted the matching logic into `matchesBuffer()` behind `matches(byte[])`.
The change is therefore hand-ported to the 3.x structure rather than
cherry-picked.
The same applies to the tests. The `main` PR added
`testMatchesByteArrayRegEx` to cover the regex branch of `matches(byte[])`,
which does not exist on 3.x — and 3.x already exercises the regex `detect()`
path through `testDetectRegExPDF`, `testDetectRegExGreedy` and
`testDetectRegExOptions`. Only the two guards that apply here are carried over,
rewritten against `detect(InputStream, Metadata)`:
- `testRegExDetectorRepeatedCallsStable` — repeated calls on one instance
stay independent
- `testRegExDetectorConcurrent` — 8 threads x 200 iterations against a
shared detector
## Behavior change
A malformed regex now raises `PatternSyntaxException` from the constructor
rather than from the first `detect()`. Via `MagicMatch` this is still lazy,
since `getDetector()` builds on first `eval()`, so loading `tika-mimetypes.xml`
is unaffected. Documented with `@throws` on the constructor.
## Testing
`./mvnw clean install -pl :tika-core` — 416 tests, 0 failures;
`MagicDetectorTest` 13/13. Checkstyle and Spotless clean.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
> Caching of MagicDetector pattern compilations
> ---------------------------------------------
>
> Key: TIKA-4796
> URL: https://issues.apache.org/jira/browse/TIKA-4796
> Project: Tika
> Issue Type: Improvement
> Reporter: Grant Ingersoll
> Priority: Minor
>
> I've been doing some profiling of Tika as part of a broader focus on a
> document extraction pipeline and one of the areas that shows up more than
> expected and appears to be an easy fix is the
> {code:java}
> Pattern p = Pattern.compile(new String(this.pattern, UTF_8), flags);{code}
> line in `matchesBuffer` (trunk/main) and the `detect` method in the 3.x line.
>
> The fix is to compile the pattern in the initialization. Patch/fix coming
> shortly.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)