Skip to content

Drop unused capture group and memoize compiled regex#605

Merged
JayBizzle merged 1 commit into
masterfrom
perf/compile-regex
May 11, 2026
Merged

Drop unused capture group and memoize compiled regex#605
JayBizzle merged 1 commit into
masterfrom
perf/compile-regex

Conversation

@JayBizzle
Copy link
Copy Markdown
Owner

Summary

Two small changes to CrawlerDetect::compileRegex:

  • Non-capturing group. The combined pattern wraps the alternation in (...), but getMatches() returns $matches[0] — the full match — so the capturing backreference is never read. Switched to (?:...) so PCRE doesn't allocate the group.
  • Process-wide compile cache. A static array keyed by md5(serialize($patterns)) skips the implode of the ~1500-entry crawler list on every new CrawlerDetect. Frameworks that instantiate per request (Laravel middleware, etc.) get the win without changing the public API.

No behavioural change — getMatches() still returns the full matched substring.

Test plan

  • vendor/bin/phpunit — 19 tests, 2,274,786 assertions, all pass on PHP 8.4
  • CI matrix (PHP 7.1 → 8.4)

🤖 Generated with Claude Code

The outer parentheses around the alternation produced a capturing group
whose backreference was never read — preg_match's $matches[0] returns
the full match regardless. Switched to (?:...) so PCRE doesn't have to
allocate the group.

Also added a process-wide static cache for the compiled pattern string,
keyed by hash of the pattern list. Repeated `new CrawlerDetect` calls
(common in per-request frameworks) now skip the implode of the
~1500-entry crawler list after the first instance.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@JayBizzle JayBizzle merged commit c617d86 into master May 11, 2026
10 checks passed
@JayBizzle JayBizzle deleted the perf/compile-regex branch May 11, 2026 18:27
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