Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of isEqual() by skipping Comparator Factory #1478

Merged
merged 1 commit into from
Oct 21, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Framework/Constraint/IsEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,25 @@ public function __construct($value, $delta = 0.0, $maxDepth = 10, $canonicalize
*/
public function evaluate($other, $description = '', $returnResult = false)
{
$comparatorFactory = new SebastianBergmann\Comparator\Factory;
if ($this->value === $other)
{
/*
* Shortcut: if $this->value and $other are identical, they are also equal. This is the most common path
* and will allow us to skip initialization of all the comparators.
*/
return true;
}

static $comparatorFactory;

if (NULL === $comparatorFactory)
{
/*
* Initializing the Comparator Factory is not free, keep a static copy around to avoid initializing it for
* every evaluation.
*/
$comparatorFactory = new SebastianBergmann\Comparator\Factory;
}

try {
$comparator = $comparatorFactory->getComparatorFor(
Expand Down