Conversation
|
//cc @SanderMuller |
|
This pull request has been marked as ready for review. |
|
I ran this against 2.3.x The first pass used to rewrite every Three results that change, all with the snippets below at level 8: <?php declare(strict_types = 1);
namespace PipeArrow\Sub;
class Helper { /** @return non-empty-string */ public static function run(string $s): string { return $s . 'x'; } }
/** @return list<int> */
function listy(string $s): array { return [1]; }
namespace PipeArrow;
use PipeArrow\Sub\Helper;
use function PipeArrow\Sub\listy;
function g(string $s): void
{
$a = $s |> Helper::run(...) |> (fn ($v) => \PHPStan\dumpType($v));
$b = $s |> listy(...) |> (fn ($v) => \PHPStan\dumpType($v));
$c = $s |> Helper::run(...) |> (function ($v) { \PHPStan\dumpType($v); });
}
<?php declare(strict_types = 1);
namespace PipeLast;
function f(int $x): void
{
if ($x > 0) {
} elseif ($x |> is_int(...)) {
}
echo match (true) {
$x > 0 => 1,
$x |> is_int(...) => 2,
};
}
All three give the same result with the turbo extension loaded ( The reverse pass alone can join the main pass without these effects: reverse in --- a/src/Parser/ReversePipeTransformerVisitor.php
+++ b/src/Parser/ReversePipeTransformerVisitor.php
#[Override]
- public function enterNode(Node $node): ?Node
+ public function leaveNode(Node $node): ?Node
{
--- a/src/Parser/RichParser.php
+++ b/src/Parser/RichParser.php
$nodeTraverser = new NodeTraverser();
+ // first, so that its leaveNode() runs after every other visitor's: leave order is the reverse of enter order
+ $nodeTraverser->addVisitor(new ReversePipeTransformerVisitor());
$nodeTraverser->addVisitor($this->nameResolver);
@@
- $reversePipeTransformer = new NodeTraverser(new ReversePipeTransformerVisitor());
- /** @var array<Node\Stmt> */
- $nodes = $reversePipeTransformer->traverse($nodes);
-That gives the 2.3.x result for all three snippets. It saves much less, though: see the numbers below. Whether about 2% of parse time is worth the change is your call. What I checked, with 2.3.x
Performance: CI: each of the 12 red checks also fails on #6611 or #6608 today. The two
|
|
yeah, that's why the source works like it works today |
82a6978 to
53df5b7
Compare
|
The new version keeps the tree as it is on 2.3.x. The reverse pass only acts on calls that carry I checked it against 2.3.x
Whole-parse timings were too noisy here today, at a load of 20-50. So I timed the passes themselves over the 1519 Tempest files, as the minimum of 15 runs, twice. The forward pass costs 48.6-49.4 ms and the reverse pass 56.6-59.2 ms, against 1220-1239 ms for all of The forward pass can be skipped the same way. A + // a Pipe node needs the |> token, so without it neither pipe pass can change anything
+ $hasPipe = str_contains($sourceCode, '|>');
$pipeTransformerVisitor = new PipeTransformerVisitor();
- $pipeTransformer = new NodeTraverser($pipeTransformerVisitor);
- /** @var array<Node\Stmt> */
- $nodes = $pipeTransformer->traverse($nodes);
+ if ($hasPipe) {
+ $pipeTransformer = new NodeTraverser($pipeTransformerVisitor);
+ /** @var array<Node\Stmt> */
+ $nodes = $pipeTransformer->traverse($nodes);
+ }With that change, the same 325 files still give an identical AST, and Each of the 13 red checks also fails on #6609, #6604 or #6611 today. |
a53c857 to
b9411de
Compare
|
in my measures the e2e time does not get faster :-( |
node-traversal is the slowest part in RichParser. reduce some traversal, which also simplifies the implementation.
running hyperfine on it does not yield meaningful improvements/regressions though.