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

Preload all classes before forking (+5% performance) #11309

Merged
merged 9 commits into from
Feb 18, 2025
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
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"amphp/phpunit-util": "^3",
"bamarni/composer-bin-plugin": "^1.4",
"brianium/paratest": "^6.9",
"danog/class-finder": "^0.4.8",
"dg/bypass-finals": "^1.5",
"mockery/mockery": "^1.5",
"nunomaduro/mock-final-classes": "^1.1",
Expand Down Expand Up @@ -132,13 +133,14 @@
],
"gen_callmap": "./bin/stubs/gen_callmap.sh",
"gen_propertymap": "@php ./bin/stubs/update-property-map.php",
"gen_preloader": "@php ./bin/stubs/gen_preloader.php",
"generate_issues_list_doc": "@php ./bin/docs/generate_issues_list_doc.php",
"generate_levels_doc": "@php ./bin/docs/generate_levels_doc.php",
"build": [
"@gen_callmap",
"@gen_propertymap",
"@gen_preloader",
"@generate_issues_list_doc",
"@generate_levels_doc"
"@generate_levels_doc",
"@cs-fix"
]
},
"scripts-descriptions": {
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<file>tests/</file>

<!-- These are just examples and stub classes/files, so it doesn't really matter if they're PSR-2 compliant. -->
<exclude-pattern>src/Psalm/Internal/PreloaderList.php</exclude-pattern>
<exclude-pattern>src/Psalm/Internal/Stubs/</exclude-pattern>
<exclude-pattern>tests/fixtures/</exclude-pattern>

Expand Down
5 changes: 4 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="6.x-dev@c48158beaa48190a4ddcd84bae16f4f909895143">
<files psalm-version="6.x-dev@c424cac5d8d8492cb430bcad7aeda4b5d2ca6355">
<file src="examples/TemplateChecker.php">
<PossiblyUndefinedIntArrayOffset>
<code><![CDATA[$comment_block->tags['variablesfrom'][0]]]></code>
Expand Down Expand Up @@ -1639,6 +1639,9 @@
<code><![CDATA[empty($baselineFile)]]></code>
<code><![CDATA[getenv('PSALM_SHEPHERD')]]></code>
</RiskyTruthyFalsyComparison>
<UnusedPsalmSuppress>
<code><![CDATA[PossiblyNullArgument]]></code>
</UnusedPsalmSuppress>
</file>
<file src="src/Psalm/Internal/Cli/Psalter.php">
<RiskyTruthyFalsyComparison>
Expand Down
10 changes: 6 additions & 4 deletions src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Psalm\Internal\ErrorHandler;
use Psalm\Internal\Fork\PsalmRestarter;
use Psalm\Internal\IncludeCollector;
use Psalm\Internal\Preloader;
use Psalm\Internal\Provider\ClassLikeStorageCacheProvider;
use Psalm\Internal\Provider\FileProvider;
use Psalm\Internal\Provider\FileReferenceCacheProvider;
Expand Down Expand Up @@ -48,7 +49,6 @@
use function array_slice;
use function array_sum;
use function array_values;
use function assert;
use function chdir;
use function count;
use function defined;
Expand Down Expand Up @@ -1018,9 +1018,11 @@ private static function restart(array $options, int $threads, int $scanThreads,
exit(1);
}
}

Preloader::preload($progress, $hasJit);
}

/** @psalm-suppress UnusedParam $argv is being reported as unused */
/** @param array<int, string> $argv */
private static function forwardCliCall(array $options, array $argv): void
{
if (isset($options['alter'])) {
Expand All @@ -1031,9 +1033,9 @@ private static function forwardCliCall(array $options, array $argv): void

if (isset($options['review'])) {
require_once __DIR__ . '/Review.php';
assert($argv !== null);
array_shift($argv);
Review::run($argv);
/** @psalm-suppress PossiblyNullArgument */
Review::run(array_values($argv));
exit;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Psalm/Internal/Cli/Psalter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Psalm\Internal\ErrorHandler;
use Psalm\Internal\Fork\PsalmRestarter;
use Psalm\Internal\IncludeCollector;
use Psalm\Internal\Preloader;
use Psalm\Internal\Provider\ClassLikeStorageCacheProvider;
use Psalm\Internal\Provider\FileProvider;
use Psalm\Internal\Provider\FileStorageCacheProvider;
Expand Down Expand Up @@ -243,6 +244,8 @@ public static function run(array $argv): void
// If Xdebug is enabled, restart without it
$ini_handler->check();

Preloader::preload();

$paths_to_check = CliUtils::getPathsToCheck($options['f'] ?? null);

$path_to_config = CliUtils::getPathToConfig($options);
Expand Down
34 changes: 34 additions & 0 deletions src/Psalm/Internal/Preloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Psalm\Internal;

use Psalm\Progress\Progress;

use function class_exists;

use const PHP_EOL;

/** @internal */
final class Preloader
{
private static bool $preloaded = false;
public static function preload(?Progress $progress = null, bool $hasJit = false): void
{
if (self::$preloaded) {
return;
}

if ($hasJit) {
$progress?->write("JIT compilation in progress... ");
}
foreach (PreloaderList::CLASSES as $class) {
class_exists($class);
}
if ($hasJit) {
$progress?->write("Done.".PHP_EOL.PHP_EOL);
}
self::$preloaded = true;
}
}
Loading