Skip to content

Commit ad6f609

Browse files
ondrejmirtesclaude
andcommitted
Short-circuit no-op remove() comparisons by identity before equals()
The equals() calls that replaced identity comparisons (they are what makes no-op removal results safe to memoize) have no identity fast path of their own, and a no-op removeFalsey() is the common case on the assignment hot path. Checking identity first restores the cheap path without relying on remove()'s same-instance behavior as a contract. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LVQhbBxDjDVAVeJVvLpWYE
1 parent cfc4fbb commit ad6f609

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

‎src/Analyser/ExprHandler/AssignHandler.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,10 @@ public function processAssignVar(
465465
$truthyType = TypeCombinator::removeFalsey($type);
466466
// Value comparison, not identity: remove() happens to hand back the very same
467467
// instance when it removes nothing, but that is not part of its contract — the
468-
// falsey loop below already compares with equals().
469-
if (!$truthyType->equals($type)) {
468+
// falsey loop below already compares with equals(). The identity check is only
469+
// a fast path (equals() has no such shortcut, and no-op removal is the common
470+
// case here).
471+
if ($truthyType !== $type && !$truthyType->equals($type)) {
470472
$truthySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $assignedExpr, TypeSpecifierContext::createTruthy());
471473
$conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyType, $impurePoints, $assignedExpr);
472474
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyType, $impurePoints, $assignedExpr);

‎src/Type/Generic/TemplateTypeTrait.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,9 @@ public function tryRemove(Type $typeToRemove): ?Type
404404

405405
$bound = TypeCombinator::remove($this->getBound(), $typeToRemove);
406406
// Value comparison, not identity: remove() returning the very same instance when it
407-
// removes nothing is an implementation detail, not part of its contract.
408-
if ($this->getBound()->equals($bound)) {
407+
// removes nothing is an implementation detail, not part of its contract. The identity
408+
// check is only a fast path (equals() has no such shortcut).
409+
if ($this->getBound() === $bound || $this->getBound()->equals($bound)) {
409410
return null;
410411
}
411412

0 commit comments

Comments
 (0)