Skip to content

Commit efa6d34

Browse files
ondrejmirtesclaude
andcommitted
Return the union as it is when the other operand is a constant scalar it holds
union(U, x) rebuilt U's members whenever x was a constant scalar U already held: every member was re-bucketed by its description and a new union was assembled. ArrayType::setOffsetValueType() unions the key type with the offset for every offset written, so a rule checking a write to an array keyed by a union of a few hundred locale strings paid that once per member of the offset type. The union is now handed back untouched when it is a supertype of the scalar - the same members the general path produces. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GvDxWpzXzticTTm5eLCsb2
1 parent 2f472e5 commit efa6d34

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

‎src/Type/TypeCombinator.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ public static function union(Type ...$types): Type
237237
return self::doUnion(...$types);
238238
}
239239

240+
private static function isUnionHoldingConstantScalar(Type $union, Type $scalar): bool
241+
{
242+
return $union instanceof UnionType
243+
&& !$union instanceof BenevolentUnionType
244+
&& !$union instanceof TemplateType
245+
&& $scalar->isConstantScalarValue()->yes()
246+
&& $union->isSuperTypeOf($scalar)->yes();
247+
}
248+
240249
/** @internal Delegated to from TypeCombinatorCache, which the native extension shadows to memoize it. */
241250
public static function doUnion(Type ...$types): Type
242251
{
@@ -292,6 +301,17 @@ public static function doUnion(Type ...$types): Type
292301
if ($a === $b || ($a->equals($b) && $a->isArray()->yes())) {
293302
return $a;
294303
}
304+
305+
// union(U, x) = U when x is a constant scalar U already holds: the
306+
// general path would rebuild the same members. Adding a known key to
307+
// a large array key union (a few hundred locale strings) hits this
308+
// once per key.
309+
if (self::isUnionHoldingConstantScalar($a, $b)) {
310+
return $a;
311+
}
312+
if (self::isUnionHoldingConstantScalar($b, $a)) {
313+
return $b;
314+
}
295315
}
296316

297317
// A member passed more than once contributes nothing; dropping the

0 commit comments

Comments
 (0)