From bdf8351f08cd47292cee60012a6f38981b5e8bbc Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Mar 2026 22:49:48 +0000 Subject: [PATCH 1/2] =?UTF-8?q?Optimize=20regex=20collision=20test=20from?= =?UTF-8?q?=20O(n=C2=B2)=20to=20O(n)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of testing each pattern against every other pattern individually (~2M preg_match calls), combine all other patterns into a single regex and test once per pattern (1,427 calls). Uses the same implode technique already used in CrawlerDetect::compileRegex(). https://claude.ai/code/session_01WGabPTL71kt1patAP8ViPK --- tests/UserAgentTest.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/UserAgentTest.php b/tests/UserAgentTest.php index e2d7d26..72c050b 100644 --- a/tests/UserAgentTest.php +++ b/tests/UserAgentTest.php @@ -177,16 +177,17 @@ public function the_regex_patterns_are_unique() public function there_are_no_regex_collisions() { $crawlers = new Crawlers; + $all = $crawlers->getAll(); - foreach ($crawlers->getAll() as $key1 => $regex) { - foreach ($crawlers->getAll() as $key2 => $compare) { - // Dont check this regex against itself - if ($key1 != $key2) { - preg_match('/'.$regex.'/i', stripslashes($compare), $matches); + foreach ($all as $key => $pattern) { + $others = $all; + unset($others[$key]); - $this->assertEmpty($matches, $regex.' collided with '.$compare); - } - } + $combinedRegex = implode('|', $others); + + preg_match('/'.$combinedRegex.'/i', stripslashes($pattern), $matches); + + $this->assertEmpty($matches, $pattern.' collided with another pattern'); } } } From 5b8a81e8a272b09742affcfe9e83c8b9465f6d96 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Mar 2026 22:53:47 +0000 Subject: [PATCH 2/2] =?UTF-8?q?Revert=20"Optimize=20regex=20collision=20te?= =?UTF-8?q?st=20from=20O(n=C2=B2)=20to=20O(n)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit bdf8351f08cd47292cee60012a6f38981b5e8bbc. --- tests/UserAgentTest.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/UserAgentTest.php b/tests/UserAgentTest.php index 72c050b..e2d7d26 100644 --- a/tests/UserAgentTest.php +++ b/tests/UserAgentTest.php @@ -177,17 +177,16 @@ public function the_regex_patterns_are_unique() public function there_are_no_regex_collisions() { $crawlers = new Crawlers; - $all = $crawlers->getAll(); - foreach ($all as $key => $pattern) { - $others = $all; - unset($others[$key]); + foreach ($crawlers->getAll() as $key1 => $regex) { + foreach ($crawlers->getAll() as $key2 => $compare) { + // Dont check this regex against itself + if ($key1 != $key2) { + preg_match('/'.$regex.'/i', stripslashes($compare), $matches); - $combinedRegex = implode('|', $others); - - preg_match('/'.$combinedRegex.'/i', stripslashes($pattern), $matches); - - $this->assertEmpty($matches, $pattern.' collided with another pattern'); + $this->assertEmpty($matches, $regex.' collided with '.$compare); + } + } } } }