Skip to content

Commit 3f17a6b

Browse files
authored
Merge pull request #11274 from vimeo/enable_opcache
Enable opcache if installed
2 parents 550c8e5 + 18c8871 commit 3f17a6b

File tree

2 files changed

+31
-52
lines changed

2 files changed

+31
-52
lines changed

src/Psalm/Internal/Cli/Psalm.php

+4-17
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
use const LC_CTYPE;
9292
use const PHP_EOL;
9393
use const PHP_URL_SCHEME;
94-
use const PHP_VERSION_ID;
9594
use const STDERR;
9695

9796
// phpcs:disable PSR1.Files.SideEffects
@@ -964,11 +963,6 @@ private static function restart(array $options, int $threads, int $scanThreads,
964963
'uv',
965964
]);
966965

967-
$skipJit = defined('PHP_WINDOWS_VERSION_MAJOR') && PHP_VERSION_ID < PsalmRestarter::MIN_PHP_VERSION_WINDOWS_JIT;
968-
if ($skipJit) {
969-
$ini_handler->disableExtensions(['opcache', 'Zend OPcache']);
970-
}
971-
972966
// If Xdebug is enabled, restart without it
973967
$ini_handler->check();
974968

@@ -986,17 +980,10 @@ private static function restart(array $options, int $threads, int $scanThreads,
986980
. PHP_EOL . PHP_EOL);
987981
}
988982
} else {
989-
if ($skipJit) {
990-
$progress->write(PHP_EOL
991-
. 'JIT acceleration: OFF (disabled on Windows and PHP < 8.4)' . PHP_EOL
992-
. 'Install PHP 8.4+ to make use of JIT on Windows for a 20%+ performance boost!'
993-
. PHP_EOL . PHP_EOL);
994-
} else {
995-
$progress->write(PHP_EOL
996-
. 'JIT acceleration: OFF (opcache not installed or not enabled)' . PHP_EOL
997-
. 'Install and enable the opcache extension to make use of JIT for a 20%+ performance boost!'
998-
. PHP_EOL . PHP_EOL);
999-
}
983+
$progress->write(PHP_EOL
984+
. 'JIT acceleration: OFF (opcache not installed or not enabled)' . PHP_EOL
985+
. 'Install and enable the opcache extension to make use of JIT for a 20%+ performance boost!'
986+
. PHP_EOL . PHP_EOL);
1000987
}
1001988
if (isset($options['force-jit']) && !$hasJit) {
1002989
$progress->write('Exiting because JIT was requested but is not available.' . PHP_EOL . PHP_EOL);

src/Psalm/Internal/Fork/PsalmRestarter.php

+27-35
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use function array_splice;
1212
use function assert;
1313
use function count;
14-
use function defined;
1514
use function extension_loaded;
1615
use function file_get_contents;
1716
use function file_put_contents;
@@ -23,14 +22,11 @@
2322
use function strlen;
2423
use function strtolower;
2524

26-
use const PHP_VERSION_ID;
27-
2825
/**
2926
* @internal
3027
*/
3128
final class PsalmRestarter extends XdebugHandler
3229
{
33-
public const MIN_PHP_VERSION_WINDOWS_JIT = 8_04_03;
3430
private const REQUIRED_OPCACHE_SETTINGS = [
3531
'enable' => 1,
3632
'enable_cli' => 1,
@@ -86,31 +82,31 @@ protected function requiresRestart($default): bool
8682
static fn(string $extension): bool => extension_loaded($extension),
8783
);
8884

89-
$opcache_loaded = extension_loaded('opcache') || extension_loaded('Zend OPcache');
85+
if (!extension_loaded('opcache') && !extension_loaded('Zend OPcache')) {
86+
return true;
87+
}
9088

91-
if ($opcache_loaded) {
9289
// restart to enable JIT if it's not configured in the optimal way
93-
foreach (self::REQUIRED_OPCACHE_SETTINGS as $ini_name => $required_value) {
94-
$value = (string) ini_get("opcache.$ini_name");
95-
if ($ini_name === 'jit_buffer_size') {
96-
$value = self::toBytes($value);
97-
} elseif ($ini_name === 'enable_cli') {
98-
$value = in_array($value, ['1', 'true', true, 1]) ? 1 : 0;
99-
} elseif (is_int($required_value)) {
100-
$value = (int) $value;
101-
}
102-
if ($value !== $required_value) {
103-
return true;
104-
}
90+
foreach (self::REQUIRED_OPCACHE_SETTINGS as $ini_name => $required_value) {
91+
$value = (string) ini_get("opcache.$ini_name");
92+
if ($ini_name === 'jit_buffer_size') {
93+
$value = self::toBytes($value);
94+
} elseif ($ini_name === 'enable_cli') {
95+
$value = in_array($value, ['1', 'true', true, 1]) ? 1 : 0;
96+
} elseif (is_int($required_value)) {
97+
$value = (int) $value;
10598
}
106-
107-
$requiredMemoryConsumption = $this->getRequiredMemoryConsumption();
108-
109-
if ((int)ini_get('opcache.memory_consumption') < $requiredMemoryConsumption) {
99+
if ($value !== $required_value) {
110100
return true;
111101
}
112102
}
113103

104+
$requiredMemoryConsumption = self::getRequiredMemoryConsumption();
105+
106+
if ((int)ini_get('opcache.memory_consumption') < $requiredMemoryConsumption) {
107+
return true;
108+
}
109+
114110
return $default || $this->required;
115111
}
116112

@@ -162,27 +158,23 @@ protected function restart($command): void
162158
file_put_contents($this->tmpIni, $content);
163159
}
164160

165-
$additional_options = [];
166161
$opcache_loaded = extension_loaded('opcache') || extension_loaded('Zend OPcache');
167162

168163
// executed in the parent process (before restart)
169164
// if it wasn't loaded then we apparently don't have opcache installed and there's no point trying
170165
// to tweak it
171-
if ($opcache_loaded &&
172-
!(defined('PHP_WINDOWS_VERSION_MAJOR') && PHP_VERSION_ID < self::MIN_PHP_VERSION_WINDOWS_JIT)
173-
) {
174-
$additional_options = [];
175-
foreach (self::REQUIRED_OPCACHE_SETTINGS as $key => $value) {
176-
$additional_options []= "-dopcache.{$key}={$value}";
177-
}
166+
$additional_options = $opcache_loaded ? [] : ['-dzend_extension=opcache'];
167+
foreach (self::REQUIRED_OPCACHE_SETTINGS as $key => $value) {
168+
$additional_options []= "-dopcache.{$key}={$value}";
169+
}
178170

179-
$requiredMemoryConsumption = $this->getRequiredMemoryConsumption();
171+
$requiredMemoryConsumption = self::getRequiredMemoryConsumption();
180172

181-
if ((int)ini_get('opcache.memory_consumption') < $requiredMemoryConsumption) {
182-
$additional_options []= "-dopcache.memory_consumption={$requiredMemoryConsumption}";
183-
}
173+
if ((int)ini_get('opcache.memory_consumption') < $requiredMemoryConsumption) {
174+
$additional_options []= "-dopcache.memory_consumption={$requiredMemoryConsumption}";
184175
}
185176

177+
186178
array_splice(
187179
$command,
188180
1,
@@ -197,7 +189,7 @@ protected function restart($command): void
197189
/**
198190
* @return positive-int
199191
*/
200-
private function getRequiredMemoryConsumption(): int
192+
private static function getRequiredMemoryConsumption(): int
201193
{
202194
// Reserve for byte-codes
203195
$result = 256;

0 commit comments

Comments
 (0)