speed up TrinaryLogic by kaja47 · Pull Request #4833 · phpstan/phpstan-src

@kaja47 kaja47 marked this pull request as draft

January 29, 2026 12:38

staabm

- Split variadic arguments for and() and or(). Common case of single argument doesn't allocate an array.
- Add static fields mirroring $registry to skip one level of indirection.
- Numeric values chosen that & and | is the same as min and max. and()/or() optimized accordingly.
- Inline trivial create() method.
- Remove one unnecessary access to registry in method create().

this loop runs 1.8x faster (php 8.4.16, jit off)

$t = TrinaryLogic::createYes();
for ($i = 10_000_000; $i--;) {
	$t = $t->and(TrinaryLogic::createYes());
}

before: 1.125 s
now:    0.616 s

@staabm

@staabm staabm marked this pull request as ready for review

February 16, 2026 09:28