Skip to content

Commit

Permalink
Consume data sooner, remove redundant classes
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jan 30, 2025
1 parent aa56fa9 commit 487e9ce
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 108 deletions.
8 changes: 6 additions & 2 deletions src/Psalm/Internal/Codebase/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Psalm\Internal\Codebase;

use Amp\Future;
use InvalidArgumentException;
use PhpParser;
use Psalm\CodeLocation;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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']);

Expand Down
12 changes: 6 additions & 6 deletions src/Psalm/Internal/Codebase/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
Expand All @@ -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(
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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,
Expand Down
9 changes: 0 additions & 9 deletions src/Psalm/Internal/Fork/ForkMessage.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Psalm/Internal/Fork/ForkProcessDoneMessage.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Psalm/Internal/Fork/ForkProcessErrorMessage.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Psalm/Internal/Fork/ForkTaskDoneMessage.php

This file was deleted.

6 changes: 1 addition & 5 deletions src/Psalm/Internal/Fork/InitAnalyzerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
final class InitAnalyzerTask implements Task
{
public function run(Channel $channel, Cancellation $cancellation): mixed
{
self::runStatic();
return null;
}
public static function runStatic(): void
{
$project_analyzer = ProjectAnalyzer::getInstance();
$codebase = $project_analyzer->getCodebase();
Expand All @@ -41,5 +36,6 @@ public static function runStatic(): void
$file_reference_provider->setFileReferencesToMissingClassMembers([]);
$file_reference_provider->setReferencesToMixedMemberNames([]);
$file_reference_provider->setMethodParamUses([]);
return null;
}
}
8 changes: 2 additions & 6 deletions src/Psalm/Internal/Fork/InitScannerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
6 changes: 2 additions & 4 deletions src/Psalm/Internal/Fork/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function run(
/**
* @template T
* @param Task<T, void, void> $task
* @return array<int, T>
* @return array<int, Future<T>>
*/
public function runAll(Task $task): array
{
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Fork/ScannerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 0 additions & 8 deletions src/Psalm/Internal/Fork/ShutdownAnalyzerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 0 additions & 7 deletions src/Psalm/Internal/Fork/ShutdownScannerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 487e9ce

Please sign in to comment.