From 487e9ce313f37121fb6835fbc710c4716e3afa7f Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 30 Jan 2025 16:03:06 +0000 Subject: [PATCH] Consume data sooner, remove redundant classes --- src/Psalm/Internal/Codebase/Analyzer.php | 8 ++++++-- src/Psalm/Internal/Codebase/Scanner.php | 12 +++++------ src/Psalm/Internal/Fork/ForkMessage.php | 9 --------- .../Internal/Fork/ForkProcessDoneMessage.php | 20 ------------------- .../Internal/Fork/ForkProcessErrorMessage.php | 20 ------------------- .../Internal/Fork/ForkTaskDoneMessage.php | 20 ------------------- src/Psalm/Internal/Fork/InitAnalyzerTask.php | 6 +----- src/Psalm/Internal/Fork/InitScannerTask.php | 8 ++------ src/Psalm/Internal/Fork/Pool.php | 6 ++---- src/Psalm/Internal/Fork/ScannerTask.php | 2 +- .../Internal/Fork/ShutdownAnalyzerTask.php | 8 -------- .../Internal/Fork/ShutdownScannerTask.php | 7 ------- 12 files changed, 18 insertions(+), 108 deletions(-) delete mode 100644 src/Psalm/Internal/Fork/ForkMessage.php delete mode 100644 src/Psalm/Internal/Fork/ForkProcessDoneMessage.php delete mode 100644 src/Psalm/Internal/Fork/ForkProcessErrorMessage.php delete mode 100644 src/Psalm/Internal/Fork/ForkTaskDoneMessage.php diff --git a/src/Psalm/Internal/Codebase/Analyzer.php b/src/Psalm/Internal/Codebase/Analyzer.php index 999847c9db2..635a346bdbc 100644 --- a/src/Psalm/Internal/Codebase/Analyzer.php +++ b/src/Psalm/Internal/Codebase/Analyzer.php @@ -4,6 +4,7 @@ namespace Psalm\Internal\Codebase; +use Amp\Future; use InvalidArgumentException; use PhpParser; use Psalm\CodeLocation; @@ -31,6 +32,7 @@ use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder; use UnexpectedValueException; +use function Amp\Future\await; use function array_filter; use function array_intersect_key; use function array_merge; @@ -313,13 +315,15 @@ private function doAnalysis(ProjectAnalyzer $project_analyzer, int $pool_size): $this->progress->debug('Forking analysis' . "\n"); // Wait for all tasks to complete and collect the results. - $pool->runAll(new InitAnalyzerTask); + await($pool->runAll(new InitAnalyzerTask)); $pool->run($this->files_to_analyze, AnalyzerTask::class, $task_done_closure); $forked_pool_data = $pool->runAll(new ShutdownAnalyzerTask); $this->progress->debug('Collecting forked analysis results' . "\n"); - foreach ($forked_pool_data as $pool_data) { + foreach (Future::iterate($forked_pool_data) as $pool_data) { + $pool_data = $pool_data->await(); + IssueBuffer::addIssues($pool_data['issues']); IssueBuffer::addFixableIssues($pool_data['fixable_issue_counts']); diff --git a/src/Psalm/Internal/Codebase/Scanner.php b/src/Psalm/Internal/Codebase/Scanner.php index 0d646a953d8..36ff641f06e 100644 --- a/src/Psalm/Internal/Codebase/Scanner.php +++ b/src/Psalm/Internal/Codebase/Scanner.php @@ -25,6 +25,7 @@ use Throwable; use UnexpectedValueException; +use function Amp\Future\await; use function array_filter; use function array_merge; use function array_pop; @@ -308,7 +309,7 @@ private function scanFilePaths(int $pool_size): bool $this->progress, ); - $pool->runAll(new InitScannerTask); + await($pool->runAll(new InitScannerTask)); $pool->run($files_to_scan, ScannerTask::class, function (): void { $this->progress->taskDone(0); }); @@ -317,6 +318,8 @@ private function scanFilePaths(int $pool_size): bool $forked_pool_data = $pool->runAll(new ShutdownScannerTask); foreach ($forked_pool_data as $pool_data) { + $pool_data = $pool_data->await(); + IssueBuffer::addIssues($pool_data['issues']); $this->codebase->statements_provider->addChangedMembers( @@ -351,12 +354,9 @@ private function scanFilePaths(int $pool_size): bool } } } else { - $i = 0; - foreach ($files_to_scan as $file_path => $_) { + $this->scanAPath($file_path); $this->progress->taskDone(0); - $this->scanAPath($i, $file_path); - ++$i; } } @@ -707,7 +707,7 @@ public function isForked(): void $this->is_forked = true; } - public function scanAPath(int $_, string $file_path): void + public function scanAPath(string $file_path): void { $this->scanFile( $file_path, diff --git a/src/Psalm/Internal/Fork/ForkMessage.php b/src/Psalm/Internal/Fork/ForkMessage.php deleted file mode 100644 index 5672c7a6554..00000000000 --- a/src/Psalm/Internal/Fork/ForkMessage.php +++ /dev/null @@ -1,9 +0,0 @@ -getCodebase(); @@ -41,5 +36,6 @@ public static function runStatic(): void $file_reference_provider->setFileReferencesToMissingClassMembers([]); $file_reference_provider->setReferencesToMixedMemberNames([]); $file_reference_provider->setMethodParamUses([]); + return null; } } diff --git a/src/Psalm/Internal/Fork/InitScannerTask.php b/src/Psalm/Internal/Fork/InitScannerTask.php index 8554006f782..bdef6ce7f1e 100644 --- a/src/Psalm/Internal/Fork/InitScannerTask.php +++ b/src/Psalm/Internal/Fork/InitScannerTask.php @@ -20,12 +20,6 @@ final class InitScannerTask implements Task { final public function run(Channel $channel, Cancellation $cancellation): mixed - { - self::runStatic(); - return null; - } - - public static function runStatic(): void { $analyzer = ProjectAnalyzer::getInstance(); $analyzer->progress->debug('Initialising forked process for scanning' . PHP_EOL); @@ -40,5 +34,7 @@ public static function runStatic(): void $statements_provider->resetDiffs(); $analyzer->progress->debug('Have initialised forked process for scanning' . PHP_EOL); + + return null; } } diff --git a/src/Psalm/Internal/Fork/Pool.php b/src/Psalm/Internal/Fork/Pool.php index f11986e1484..78252943624 100644 --- a/src/Psalm/Internal/Fork/Pool.php +++ b/src/Psalm/Internal/Fork/Pool.php @@ -110,7 +110,7 @@ public function run( /** * @template T * @param Task $task - * @return array + * @return array> */ public function runAll(Task $task): array { @@ -123,8 +123,6 @@ public function runAll(Task $task): array for ($x = 0; $x < $this->threads; $x++) { $workers []= $this->pool->getWorker(); } - return await( - array_map(fn(Worker $w): Future => $w->submit($task)->getFuture(), $workers), - ); + return array_map(fn(Worker $w): Future => $w->submit($task)->getFuture(), $workers); } } diff --git a/src/Psalm/Internal/Fork/ScannerTask.php b/src/Psalm/Internal/Fork/ScannerTask.php index f95ababa791..700f810f317 100644 --- a/src/Psalm/Internal/Fork/ScannerTask.php +++ b/src/Psalm/Internal/Fork/ScannerTask.php @@ -20,6 +20,6 @@ public function __construct(private string $file) } public function run(Channel $channel, Cancellation $cancellation): mixed { - return ProjectAnalyzer::getInstance()->getCodebase()->scanner->scanAPath(0, $this->file); + return ProjectAnalyzer::getInstance()->getCodebase()->scanner->scanAPath($this->file); } } diff --git a/src/Psalm/Internal/Fork/ShutdownAnalyzerTask.php b/src/Psalm/Internal/Fork/ShutdownAnalyzerTask.php index d59b1b2b422..000f9ccad6e 100644 --- a/src/Psalm/Internal/Fork/ShutdownAnalyzerTask.php +++ b/src/Psalm/Internal/Fork/ShutdownAnalyzerTask.php @@ -24,14 +24,6 @@ final class ShutdownAnalyzerTask implements Task * @return WorkerData */ public function run(Channel $channel, Cancellation $cancellation): mixed - { - return self::getPoolData(); - } - - /** - * @return WorkerData - */ - public static function getPoolData(): array { $project_analyzer = ProjectAnalyzer::getInstance(); $codebase = $project_analyzer->getCodebase(); diff --git a/src/Psalm/Internal/Fork/ShutdownScannerTask.php b/src/Psalm/Internal/Fork/ShutdownScannerTask.php index 38d403501da..3962f6acad0 100644 --- a/src/Psalm/Internal/Fork/ShutdownScannerTask.php +++ b/src/Psalm/Internal/Fork/ShutdownScannerTask.php @@ -24,13 +24,6 @@ final class ShutdownScannerTask implements Task * @return PoolData */ public function run(Channel $channel, Cancellation $cancellation): mixed - { - return self::getPoolData(); - } - /** - * @return PoolData - */ - public static function getPoolData(): array { $project_analyzer = ProjectAnalyzer::getInstance(); $project_analyzer->progress->debug('Collecting data from forked scanner process' . PHP_EOL);