Skip to content

Commit 12e1dc5

Browse files
committed
Look up StringSet members instead of scanning them
An operator on a union of constant strings folds the members into a StringSet one at a time, and union() checked every new member against all the constants collected so far, quadratic in the size of the union. union() and intersect() now look the values up in an array keyed by them, built once per set and handed on to the union. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c574-f264-7659-8779-5e71ca99b89b
1 parent 1447c34 commit 12e1dc5

4 files changed

Lines changed: 67 additions & 22 deletions

File tree

‎src/Type/StringSet.php‎

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ private function __construct(
5050
{
5151
}
5252

53+
/** @var array<string, true>|null */
54+
private ?array $values = null;
55+
5356
public static function createEmpty(): self
5457
{
5558
return new self([], null, false, false);
@@ -92,43 +95,56 @@ public function isEmpty(): bool
9295
public function union(self $other): self
9396
{
9497
$constants = $this->constants;
98+
$values = $this->getValues();
9599
foreach ($other->constants as $constant) {
96-
if (self::contains($constants, $constant->getValue())) {
100+
$value = $constant->getValue();
101+
if (isset($values[$value])) {
97102
continue;
98103
}
99104

105+
$values[$value] = true;
100106
$constants[] = $constant;
101107
}
102108

103109
if ($this->type === null || $other->type === null) {
104110
$typed = $this->type === null ? $other : $this;
105111

106-
return new self($constants, $typed->type, $typed->digits, $typed->whitespaceFree);
112+
$union = new self($constants, $typed->type, $typed->digits, $typed->whitespaceFree);
113+
} else {
114+
$union = new self(
115+
$constants,
116+
TypeCombinator::union($this->type, $other->type),
117+
$this->digits && $other->digits,
118+
$this->whitespaceFree && $other->whitespaceFree,
119+
);
107120
}
108121

109-
return new self(
110-
$constants,
111-
TypeCombinator::union($this->type, $other->type),
112-
$this->digits && $other->digits,
113-
$this->whitespaceFree && $other->whitespaceFree,
114-
);
122+
// a union of many members is folded one member at a time - handing the
123+
// lookup on spares rebuilding it from the constants at every step
124+
$union->values = $values;
125+
126+
return $union;
115127
}
116128

117129
public function intersect(self $other): self
118130
{
119131
$constants = [];
132+
$seen = [];
120133
foreach ([[$this, $other], [$other, $this]] as [$set, $otherSet]) {
134+
$otherValues = $otherSet->getValues();
121135
foreach ($set->constants as $constant) {
136+
$value = $constant->getValue();
122137
if (
123-
self::contains($constants, $constant->getValue())
138+
isset($seen[$value])
124139
|| (
125-
!self::contains($otherSet->constants, $constant->getValue())
140+
!isset($otherValues[$value])
126141
&& ($otherSet->type === null || $otherSet->type->isSuperTypeOf($constant)->no())
127142
)
128143
) {
129144
continue;
130145
}
131146

147+
$seen[$value] = true;
132148
$constants[] = $constant;
133149
}
134150
}
@@ -396,16 +412,26 @@ private static function isNumeric(string $value, PhpVersion $phpVersion): bool
396412
return self::parseNumericString($value, $phpVersion) !== null;
397413
}
398414

399-
/** @param list<ConstantStringType> $constants */
400-
private static function contains(array $constants, string $value): bool
415+
/**
416+
* The values of the constants as array keys, so membership is a lookup
417+
* rather than a scan - scanning made merging two sets quadratic. Distinct
418+
* strings stay distinct keys: PHP turns only canonical decimal integer
419+
* strings into integer keys, and no other string maps to the same one.
420+
*
421+
* @return array<string, true>
422+
*/
423+
private function getValues(): array
401424
{
402-
foreach ($constants as $constant) {
403-
if ($constant->getValue() === $value) {
404-
return true;
405-
}
425+
if ($this->values !== null) {
426+
return $this->values;
427+
}
428+
429+
$values = [];
430+
foreach ($this->constants as $constant) {
431+
$values[$constant->getValue()] = true;
406432
}
407433

408-
return false;
434+
return $this->values = $values;
409435
}
410436

411437
}

‎tests/bench/data/big-constant-string-union-operators.php‎

Lines changed: 18 additions & 0 deletions
Large diffs are not rendered by default.

‎turbo-ext/src/StringSet.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Declared as PHPStan\Type\StringSet itself at activation: final, no parent,
55
* no interfaces. State is the twin's `private array $constants`, `private
66
* ?Type $type`, `private bool $digits` and `private bool $whitespaceFree`,
7+
* plus its `private ?array $values` lookup of the constants (unused here),
78
* declared typed property slots, so the std object handlers do GC/clone. The
89
* twin's private constructor is not declared: the sets are made by the
910
* factories only, natively.

‎turbo-ext/src/generated/StringSet.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ inline constexpr uint32_t constants = 0;
1414
inline constexpr uint32_t type = 1;
1515
inline constexpr uint32_t digits = 2;
1616
inline constexpr uint32_t whitespaceFree = 3;
17+
inline constexpr uint32_t values = 4;
1718
} // namespace slot
1819

1920
inline void declareClass(reg::Class &cls)
@@ -28,6 +29,7 @@ inline void declareProperties(reg::Class &cls)
2829
cls.property("type", ZEND_ACC_PRIVATE, reg::PropertyKind::Typed, MAY_BE_NULL, "PHPStan\\Type\\Type");
2930
cls.property("digits", ZEND_ACC_PRIVATE, reg::PropertyKind::Typed, MAY_BE_BOOL);
3031
cls.property("whitespaceFree", ZEND_ACC_PRIVATE, reg::PropertyKind::Typed, MAY_BE_BOOL);
32+
cls.property("values", ZEND_ACC_PRIVATE, reg::PropertyKind::TypedNull, MAY_BE_NULL | MAY_BE_ARRAY);
3133
}
3234

3335
/* the string and parameter tables the signatures below index into (see reg::Sig) */
@@ -76,7 +78,7 @@ inline constexpr char strings[] =
7678
"matches\0" /* 512 */
7779
"toNumber\0" /* 520 */
7880
"isNumeric\0" /* 529 */
79-
"contains"; /* 539 */
81+
"getValues"; /* 539 */
8082
inline constexpr reg::PackedArg args[] = {
8183
reg::packed(0, MAY_BE_ARRAY), /* __construct $constants */
8284
reg::packed(10, MAY_BE_NULL, 15), /* __construct $type */
@@ -124,9 +126,7 @@ inline constexpr reg::PackedArg args[] = {
124126
reg::packed(468, MAY_BE_STRING), /* isNumeric $value */
125127
reg::packed(271, 0, 282), /* isNumeric $phpVersion */
126128
reg::packed(67, MAY_BE_BOOL), /* isNumeric return */
127-
reg::packed(0, MAY_BE_ARRAY), /* contains $constants */
128-
reg::packed(468, MAY_BE_STRING), /* contains $value */
129-
reg::packed(67, MAY_BE_BOOL), /* contains return */
129+
reg::packed(67, MAY_BE_ARRAY), /* getValues return */
130130
};
131131
using Sig = reg::Sig<strings, args>;
132132
} // namespace sigtab
@@ -159,7 +159,7 @@ inline constexpr sigtab::Sig parseNumericString = { { 474 /* parseNumericString
159159
inline constexpr sigtab::Sig parseNumericPrefix = { { 493 /* parseNumericPrefix */, 1, 39, 1, 40, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC } };
160160
inline constexpr sigtab::Sig toNumber = { { 520 /* toNumber */, 1, 41, 1, 42, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
161161
inline constexpr sigtab::Sig isNumeric = { { 529 /* isNumeric */, 2, 43, 2, 45, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
162-
inline constexpr sigtab::Sig contains = { { 539 /* contains */, 2, 46, 2, 48, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } };
162+
inline constexpr sigtab::Sig getValues = { { 539 /* getValues */, 0, 46, 0, 46, ZEND_ACC_PRIVATE } };
163163
} // namespace sig
164164

165165
} // namespace ptdecl::StringSet

0 commit comments

Comments
 (0)