Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move controlling the parallelization settings to Box #1190

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ final class Box implements Countable
private function __construct(
private Phar $phar,
private readonly string $pharFilePath,
private readonly bool $enableParallelization,
) {
$this->compactors = new Compactors();
$this->placeholderCompactor = new Placeholder([]);
Expand All @@ -86,15 +87,20 @@ private function __construct(
*
* @see RecursiveDirectoryIterator
*/
public static function create(string $pharFilePath, int $pharFlags = 0, ?string $pharAlias = null): self
{
public static function create(
string $pharFilePath,
int $pharFlags = 0,
?string $pharAlias = null,
bool $enableParallelization = false,
): self {
// Ensure the parent directory of the PHAR file exists as `new \Phar()` does not create it and would fail
// otherwise.
FS::mkdir(dirname($pharFilePath));

return new self(
new Phar($pharFilePath, $pharFlags, $pharAlias),
$pharFilePath,
$enableParallelization,
);
}

Expand Down Expand Up @@ -450,14 +456,15 @@ private function processContents(array $files): array
$mapFile = $this->mapFile;
$compactors = $this->compactors;
$cwd = getcwd();
$enableParallelization = $this->enableParallelization;

$processFile = static function (string $file) use ($cwd, $mapFile, $compactors): array {
$processFile = static function (string $file) use ($cwd, $mapFile, $compactors, $enableParallelization): array {
chdir($cwd);

// Keep the fully qualified call here since this function may be executed without the right autoloading
// mechanism
\KevinGH\Box\register_aliases();
if (true === \KevinGH\Box\is_parallel_processing_enabled()) {
if ($enableParallelization) {
\KevinGH\Box\register_error_handler();
}

Expand All @@ -470,7 +477,7 @@ private function processContents(array $files): array
return [$local, $processedContents, $compactors->getScoperSymbolsRegistry()];
};

if ($this->scoper instanceof NullScoper || false === is_parallel_processing_enabled()) {
if ($this->scoper instanceof NullScoper || !$enableParallelization) {
return array_map($processFile, $files);
}

Expand Down
11 changes: 6 additions & 5 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
use function implode;
use function is_callable;
use function is_string;
use function KevinGH\Box\disable_parallel_processing;
use function KevinGH\Box\format_size;
use function KevinGH\Box\format_time;
use function memory_get_peak_usage;
Expand Down Expand Up @@ -192,8 +191,9 @@ public function execute(IO $io): int

PhpSettingsChecker::check($io);

if ($io->getTypedOption(self::NO_PARALLEL_PROCESSING_OPTION)->asBoolean()) {
disable_parallel_processing();
$enableParallelization = $io->getTypedOption(self::NO_PARALLEL_PROCESSING_OPTION)->asBoolean();

if ($enableParallelization) {
$io->writeln(
'<info>[debug] Disabled parallel processing</info>',
OutputInterface::VERBOSITY_DEBUG,
Expand Down Expand Up @@ -224,7 +224,7 @@ public function execute(IO $io): int
$restoreLimit = OpenFileDescriptorLimiter::bumpLimit(2048, $io);

try {
$box = $this->createPhar($config, $logger, $io, $debug);
$box = $this->createPhar($config, $logger, $io, $debug, $enableParallelization);
} finally {
$restoreLimit();
}
Expand All @@ -244,10 +244,11 @@ private function createPhar(
Configuration $config,
CompilerLogger $logger,
IO $io,
bool $enableParallelization,
bool $debug,
): Box {
$tmpOutputPath = $config->getTmpOutputPath();
$box = Box::create($tmpOutputPath);
$box = Box::create($tmpOutputPath, enableParallelization: $enableParallelization);
$composerOrchestrator = new ComposerOrchestrator(
ComposerProcessFactory::create(
$config->getComposerBin(),
Expand Down
3 changes: 0 additions & 3 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,4 @@ final class Constants
public const MEMORY_LIMIT = 'BOX_MEMORY_LIMIT';
public const ALLOW_XDEBUG = 'BOX_ALLOW_XDEBUG';
public const BIN = 'BOX_BIN';

/** @internal */
public const NO_PARALLEL_PROCESSING = '_BOX_NO_PARALLEL_PROCESSING';
}
21 changes: 0 additions & 21 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
use function bin2hex;
use function class_alias;
use function class_exists;
use function constant;
use function define;
use function defined;
use function floor;
use function is_float;
use function is_int;
Expand Down Expand Up @@ -168,24 +165,6 @@ class_alias(\Symfony\Component\Finder\Finder::class, \Isolated\Symfony\Component
}
}

/**
* @private
*/
function disable_parallel_processing(): void
{
if (false === defined(Constants::NO_PARALLEL_PROCESSING)) {
define(Constants::NO_PARALLEL_PROCESSING, true);
}
}

/**
* @private
*/
function is_parallel_processing_enabled(): bool
{
return false === defined(Constants::NO_PARALLEL_PROCESSING) || false === constant(Constants::NO_PARALLEL_PROCESSING);
}

/**
* @private
*
Expand Down