Repository navigation
Expand file tree
/
Copy pathscope_expr_dispatch_bench.php
More file actions
218 lines (194 loc) · 6.37 KB
/
Copy pathscope_expr_dispatch_bench.php
File metadata and controls
218 lines (194 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
declare(strict_types=1);
/**
* Benchmark + equivalence check for the expr-bearing node guard in PHPStanNodeScopeResolver.
*
* The resolver propagates the scope to $node->expr for a hand-listed set of node types via a long
* instanceof chain. This compares that chain against the shorter property_exists($node, 'expr').
*
* Two things are measured:
* 1. Equivalence - which node types each predicate matches. They are NOT the same set.
* 2. Speed - CPU time (user+sys) over a REPEAT loop; wall is reported but noisy under load.
*
* Run:
* php bench/scope_expr_dispatch_bench.php
*/
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\BitwiseNot;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\Clone_;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\ErrorSuppress;
use PhpParser\Node\Expr\Eval_;
use PhpParser\Node\Expr\Exit_;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Print_;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Expr\UnaryPlus;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\Stmt\EnumCase;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Return_;
require __DIR__ . '/../vendor/autoload.php';
const REPEAT = 500_000;
/**
* Current production guard: the explicit instanceof chain.
*/
function matchesChain(Node $node): bool
{
return (
$node instanceof Expression ||
$node instanceof Return_ ||
$node instanceof EnumCase ||
$node instanceof Cast ||
$node instanceof YieldFrom ||
$node instanceof UnaryMinus ||
$node instanceof UnaryPlus ||
$node instanceof Throw_ ||
$node instanceof Empty_ ||
$node instanceof BooleanNot ||
$node instanceof Clone_ ||
$node instanceof ErrorSuppress ||
$node instanceof BitwiseNot ||
$node instanceof Eval_ ||
$node instanceof Print_ ||
$node instanceof Exit_ ||
$node instanceof Include_ ||
$node instanceof Instanceof_
) && $node->expr instanceof Expr;
}
/**
* Candidate guard: a single property lookup.
*
* NOT equivalent to the chain on its own - it also matches Assign/AssignOp/AssignRef,
* ArrowFunction and Foreach_.
*/
function matchesPropertyExists(Node $node): bool
{
return property_exists($node, 'expr') && $node->expr instanceof Expr;
}
/**
* Shipped guard: property lookup minus the 5 node groups the chain deliberately excludes.
* This matches the chain exactly across all 42 expr-bearing php-parser node classes
* (AssignOp is abstract and covers its 13 subclasses).
*/
function matchesPropertyExistsGuarded(Node $node): bool
{
return property_exists($node, 'expr')
&& $node->expr instanceof Expr
&& ! $node instanceof Assign
&& ! $node instanceof AssignOp
&& ! $node instanceof AssignRef
&& ! $node instanceof ArrowFunction
&& ! $node instanceof Foreach_;
}
$v = new Variable('x');
// nodes that carry an $expr property (set it so the Expr guard passes)
$withExpr = [
new Expression($v),
new Return_($v),
new EnumCase('CASE', $v),
new Cast\Int_($v),
new YieldFrom($v),
new UnaryMinus($v),
new UnaryPlus($v),
new Throw_($v),
new Empty_($v),
new BooleanNot($v),
new Clone_($v),
new ErrorSuppress($v),
new BitwiseNot($v),
new Eval_($v),
new Print_($v),
new Exit_($v),
new Include_($v, Include_::TYPE_INCLUDE),
new Instanceof_($v, new Node\Name('Foo')),
// the five extra types property_exists also matches, the chain does not:
new Assign($v, $v),
new AssignOp\Plus($v, $v),
new AssignRef($v, $v),
new ArrowFunction([
'expr' => $v,
]),
new Foreach_($v, $v),
];
// nodes without an $expr property (both predicates must reject)
$withoutExpr = [
new Variable('y'),
new Nop(),
new Node\Name('Bar'),
new Node\Arg($v),
];
$all = [...$withExpr, ...$withoutExpr];
// --- 1. equivalence ----------------------------------------------------------
$divergent = [];
foreach ($all as $node) {
if (matchesChain($node) !== matchesPropertyExists($node)) {
$divergent[] = $node::class;
}
}
echo "Equivalence check\n";
echo "-----------------\n";
if ($divergent === []) {
echo "identical on sample set\n\n";
} else {
echo "DIVERGENT - property_exists matches but chain does not:\n";
foreach ($divergent as $class) {
echo ' - ' . $class . "\n";
}
echo "\n";
}
$guardedDivergent = [];
foreach ($all as $node) {
if (matchesChain($node) !== matchesPropertyExistsGuarded($node)) {
$guardedDivergent[] = $node::class;
}
}
echo "Guarded variant (shipped) vs chain\n";
echo "----------------------------------\n";
echo $guardedDivergent === [] ? "identical on sample set\n\n" : "DIVERGENT: " . implode(', ', $guardedDivergent) . "\n\n";
// --- 2. speed ----------------------------------------------------------------
function cpuSeconds(): float
{
$usage = getrusage();
return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1e6
+ $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1e6;
}
function run(callable $predicate, array $all): void
{
for ($r = 0; $r < REPEAT; ++$r) {
foreach ($all as $node) {
$predicate($node);
}
}
}
// warmup (excluded)
run(matchesChain(...), $all);
run(matchesPropertyExists(...), $all);
$cpu0 = cpuSeconds();
$wall0 = microtime(true);
run(matchesChain(...), $all);
$chainCpu = cpuSeconds() - $cpu0;
$chainWall = microtime(true) - $wall0;
$cpu0 = cpuSeconds();
$wall0 = microtime(true);
run(matchesPropertyExists(...), $all);
$peCpu = cpuSeconds() - $cpu0;
$peWall = microtime(true) - $wall0;
$calls = REPEAT * count($all);
printf("Speed (%s calls each)\n", number_format($calls));
echo "---------------------\n";
printf("instanceof chain : CPU %.3fs wall %.3fs (%.1f ns/call)\n", $chainCpu, $chainWall, $chainCpu / $calls * 1e9);
printf("property_exists : CPU %.3fs wall %.3fs (%.1f ns/call)\n", $peCpu, $peWall, $peCpu / $calls * 1e9);
printf("ratio (chain/pe) : %.2fx CPU\n", $peCpu > 0 ? $chainCpu / $peCpu : 0.0);