Skip to content

Commit

Permalink
Merge pull request #131 from Toflar/improve-compilingmatcher-performance
Browse files Browse the repository at this point in the history
Improve CompilingMatcher by caching the result too
  • Loading branch information
Seldaek authored Mar 14, 2022
2 parents a951f61 + e0eb238 commit af3620d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/CompilingMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class CompilingMatcher
* @phpstan-var array<string, callable>
*/
private static $compiledCheckerCache = array();
/**
* @var array
* @phpstan-var array<string, bool>
*/
private static $resultCache = array();

/** @var bool */
private static $enabled;

Expand Down Expand Up @@ -51,11 +57,17 @@ class CompilingMatcher
*/
public static function match(ConstraintInterface $constraint, $operator, $version)
{
$resultCacheKey = $operator.$constraint.$version;

if (isset(self::$resultCache[$resultCacheKey])) {
return self::$resultCache[$resultCacheKey];
}

if (self::$enabled === null) {
self::$enabled = !\in_array('eval', explode(',', (string) ini_get('disable_functions')), true);
}
if (!self::$enabled) {
return $constraint->matches(new Constraint(self::$transOpInt[$operator], $version));
return self::$resultCache[$resultCacheKey] = $constraint->matches(new Constraint(self::$transOpInt[$operator], $version));
}

$cacheKey = $operator.$constraint;
Expand All @@ -66,6 +78,6 @@ public static function match(ConstraintInterface $constraint, $operator, $versio
$function = self::$compiledCheckerCache[$cacheKey];
}

return $function($version, strpos($version, 'dev-') === 0);
return self::$resultCache[$resultCacheKey] = $function($version, strpos($version, 'dev-') === 0);
}
}

0 comments on commit af3620d

Please sign in to comment.