Skip to content

Commit

Permalink
[Performance] Remove CollectedData on ProcessResult (#5520)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jan 30, 2024
1 parent 171fddb commit 58ef131
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 131 deletions.
8 changes: 2 additions & 6 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Application;

use Nette\Utils\FileSystem as UtilsFileSystem;
use PHPStan\Collectors\CollectedData;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
Expand Down Expand Up @@ -58,7 +57,7 @@ public function run(Configuration $configuration, InputInterface $input): Proces

// no files found
if ($filePaths === []) {
return new ProcessResult([], [], []);
return new ProcessResult([], []);
}

$this->configureCustomErrorHandler();
Expand Down Expand Up @@ -118,9 +117,6 @@ public function processFiles(
/** @var FileDiff[] $fileDiffs */
$fileDiffs = [];

/** @var CollectedData[] $collectedData */
$collectedData = [];

foreach ($filePaths as $filePath) {
if ($preFileCallback !== null) {
$preFileCallback($filePath);
Expand Down Expand Up @@ -156,7 +152,7 @@ public function processFiles(
}
}

return new ProcessResult($systemErrors, $fileDiffs, $collectedData);
return new ProcessResult($systemErrors, $fileDiffs);
}

private function processFile(File $file, Configuration $configuration): FileProcessResult
Expand Down
2 changes: 1 addition & 1 deletion src/Autoloading/BootstrapFilesIncluder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Rector\Autoloading;

use Phar;
use FilesystemIterator;
use Phar;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Exception\ShouldNotHappenException;
Expand Down
5 changes: 4 additions & 1 deletion src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,14 @@ public function withPhpSets(
return $this;
}
}

throw new InvalidConfigurationException(sprintf(
'We could not find local "composer.json" to determine your PHP version.%sPlease, fill the PHP version set in withPhpSets() manually.',
PHP_EOL
));
} elseif ($php53) {
}

if ($php53) {
$this->sets[] = LevelSetList::UP_TO_PHP_53;
} elseif ($php54) {
$this->sets[] = LevelSetList::UP_TO_PHP_54;
Expand Down
18 changes: 0 additions & 18 deletions src/Console/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use PHPStan\Collectors\CollectedData;
use React\EventLoop\StreamSelectLoop;
use React\Socket\ConnectionInterface;
use React\Socket\TcpConnector;
Expand Down Expand Up @@ -132,22 +131,6 @@ private function runWorker(
return;
}

$previouslyCollectedDataItems = $json[Bridge::PREVIOUSLY_COLLECTED_DATA] ?? [];
if ($previouslyCollectedDataItems !== []) {
// turn to value objects
$previouslyCollectedDatas = [];
foreach ($previouslyCollectedDataItems as $previouslyCollectedDataItem) {
Assert::keyExists($previouslyCollectedDataItem, 'data');
Assert::keyExists($previouslyCollectedDataItem, 'filePath');
Assert::keyExists($previouslyCollectedDataItem, 'collectorType');

$previouslyCollectedDatas[] = CollectedData::decode($previouslyCollectedDataItem);
}

$configuration->setCollectedData($previouslyCollectedDatas);
$configuration->enableSecondRun();
}

/** @var string[] $filePaths */
$filePaths = $json[Bridge::FILES] ?? [];

Expand All @@ -169,7 +152,6 @@ private function runWorker(
Bridge::FILES_COUNT => count($filePaths),
Bridge::SYSTEM_ERRORS => $processResult->getSystemErrors(),
Bridge::SYSTEM_ERRORS_COUNT => count($processResult->getSystemErrors()),
Bridge::COLLECTED_DATA => $processResult->getCollectedData(),
],
]);
});
Expand Down
25 changes: 3 additions & 22 deletions src/Parallel/Application/ParallelFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use Nette\Utils\Random;
use PHPStan\Collectors\CollectedData;
use React\EventLoop\StreamSelectLoop;
use React\Socket\ConnectionInterface;
use React\Socket\TcpServer;
Expand Down Expand Up @@ -80,28 +79,17 @@ public function process(
/** @var FileDiff[] $fileDiffs */
$fileDiffs = [];

/** @var CollectedData[] $collectedData */
$collectedData = [];

/** @var SystemError[] $systemErrors */
$systemErrors = [];

$tcpServer = new TcpServer('127.0.0.1:0', $streamSelectLoop);
$this->processPool = new ProcessPool($tcpServer);

$tcpServer->on(ReactEvent::CONNECTION, function (ConnectionInterface $connection) use (
&$jobs,
$configuration
): void {
$tcpServer->on(ReactEvent::CONNECTION, function (ConnectionInterface $connection) use (&$jobs): void {
$inDecoder = new Decoder($connection, true, 512, 0, 4 * 1024 * 1024);
$outEncoder = new Encoder($connection);

$inDecoder->on(ReactEvent::DATA, function (array $data) use (
&$jobs,
$inDecoder,
$outEncoder,
$configuration
): void {
$inDecoder->on(ReactEvent::DATA, function (array $data) use (&$jobs, $inDecoder, $outEncoder): void {
$action = $data[ReactCommand::ACTION];
if ($action !== Action::HELLO) {
return;
Expand All @@ -121,7 +109,6 @@ public function process(
$parallelProcess->request([
ReactCommand::ACTION => Action::MAIN,
Content::FILES => $jobsChunk,
Bridge::PREVIOUSLY_COLLECTED_DATA => $configuration->getCollectedData(),
]);
});
});
Expand Down Expand Up @@ -162,7 +149,6 @@ public function process(
$processSpawner = function () use (
&$systemErrors,
&$fileDiffs,
&$collectedData,
&$jobs,
$postFileCallback,
&$systemErrorsCount,
Expand Down Expand Up @@ -198,7 +184,6 @@ function (array $json) use (
&$jobs,
$postFileCallback,
&$systemErrorsCount,
&$collectedData,
&$reachedInternalErrorsCountLimit,
$processIdentifier,
&$fileChunksBudgetPerProcess,
Expand All @@ -218,10 +203,6 @@ function (array $json) use (
$fileDiffs[] = FileDiff::decode($jsonFileDiff);
}

foreach ($json[Bridge::COLLECTED_DATA] as $collectedDataItem) {
$collectedData[] = CollectedData::decode($collectedDataItem);
}

$postFileCallback($json[Bridge::FILES_COUNT]);

$systemErrorsCount += $json[Bridge::SYSTEM_ERRORS_COUNT];
Expand Down Expand Up @@ -290,6 +271,6 @@ function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): v
));
}

return new ProcessResult($systemErrors, $fileDiffs, $collectedData);
return new ProcessResult($systemErrors, $fileDiffs);
}
}
10 changes: 0 additions & 10 deletions src/Parallel/ValueObject/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,4 @@ final class Bridge
* @var string
*/
public const FILES_COUNT = 'files_count';

/**
* @var string
*/
public const COLLECTED_DATA = 'collected_data';

/**
* @var string
*/
public const PREVIOUSLY_COLLECTED_DATA = 'previously_collected_data';
}
74 changes: 13 additions & 61 deletions src/ValueObject/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,28 @@

namespace Rector\ValueObject;

use PHPStan\Collectors\CollectedData;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Webmozart\Assert\Assert;

final class Configuration
final readonly class Configuration
{
private bool $isSecondRun = false;

/**
* @var CollectedData[]
*/
private array $collectedData = [];

/**
* @param string[] $fileExtensions
* @param string[] $paths
*/
public function __construct(
private readonly bool $isDryRun = false,
private readonly bool $showProgressBar = true,
private readonly bool $shouldClearCache = false,
private readonly string $outputFormat = ConsoleOutputFormatter::NAME,
private readonly array $fileExtensions = ['php'],
private readonly array $paths = [],
private readonly bool $showDiffs = true,
private readonly string | null $parallelPort = null,
private readonly string | null $parallelIdentifier = null,
private readonly bool $isParallel = false,
private readonly string|null $memoryLimit = null,
private readonly bool $isDebug = false,
private bool $isDryRun = false,
private bool $showProgressBar = true,
private bool $shouldClearCache = false,
private string $outputFormat = ConsoleOutputFormatter::NAME,
private array $fileExtensions = ['php'],
private array $paths = [],
private bool $showDiffs = true,
private string | null $parallelPort = null,
private string | null $parallelIdentifier = null,
private bool $isParallel = false,
private string|null $memoryLimit = null,
private bool $isDebug = false,
) {
}

Expand Down Expand Up @@ -103,44 +95,4 @@ public function isDebug(): bool
{
return $this->isDebug;
}

/**
* @param CollectedData[] $collectedData
*/
public function setCollectedData(array $collectedData): void
{
$this->collectedData = $collectedData;
}

/**
* @return CollectedData[]
*/
public function getCollectedData(): array
{
return $this->collectedData;
}

/**
* @api
*/
public function enableSecondRun(): void
{
$this->isSecondRun = true;
}

/**
* @api
*/
public function isSecondRun(): bool
{
return $this->isSecondRun;
}

/**
* @api used in tests
*/
public function reset(): void
{
$this->isSecondRun = false;
}
}
12 changes: 0 additions & 12 deletions src/ValueObject/ProcessResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\ValueObject;

use PHPStan\Collectors\CollectedData;
use Rector\ValueObject\Error\SystemError;
use Rector\ValueObject\Reporting\FileDiff;
use Webmozart\Assert\Assert;
Expand All @@ -14,16 +13,13 @@ final class ProcessResult
/**
* @param SystemError[] $systemErrors
* @param FileDiff[] $fileDiffs
* @param CollectedData[] $collectedData
*/
public function __construct(
private array $systemErrors,
private readonly array $fileDiffs,
private readonly array $collectedData
) {
Assert::allIsInstanceOf($systemErrors, SystemError::class);
Assert::allIsInstanceOf($fileDiffs, FileDiff::class);
Assert::allIsInstanceOf($collectedData, CollectedData::class);
}

/**
Expand All @@ -42,14 +38,6 @@ public function getFileDiffs(): array
return $this->fileDiffs;
}

/**
* @return CollectedData[]
*/
public function getCollectedData(): array
{
return $this->collectedData;
}

/**
* @param SystemError[] $systemErrors
*/
Expand Down

0 comments on commit 58ef131

Please sign in to comment.