Skip to content

Commit

Permalink
Merge pull request #11274 from vimeo/enable_opcache
Browse files Browse the repository at this point in the history
Enable opcache if installed
  • Loading branch information
danog authored Feb 10, 2025
2 parents 550c8e5 + 18c8871 commit 3f17a6b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 52 deletions.
21 changes: 4 additions & 17 deletions src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
use const LC_CTYPE;
use const PHP_EOL;
use const PHP_URL_SCHEME;
use const PHP_VERSION_ID;
use const STDERR;

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

$skipJit = defined('PHP_WINDOWS_VERSION_MAJOR') && PHP_VERSION_ID < PsalmRestarter::MIN_PHP_VERSION_WINDOWS_JIT;
if ($skipJit) {
$ini_handler->disableExtensions(['opcache', 'Zend OPcache']);
}

// If Xdebug is enabled, restart without it
$ini_handler->check();

Expand All @@ -986,17 +980,10 @@ private static function restart(array $options, int $threads, int $scanThreads,
. PHP_EOL . PHP_EOL);
}
} else {
if ($skipJit) {
$progress->write(PHP_EOL
. 'JIT acceleration: OFF (disabled on Windows and PHP < 8.4)' . PHP_EOL
. 'Install PHP 8.4+ to make use of JIT on Windows for a 20%+ performance boost!'
. PHP_EOL . PHP_EOL);
} else {
$progress->write(PHP_EOL
. 'JIT acceleration: OFF (opcache not installed or not enabled)' . PHP_EOL
. 'Install and enable the opcache extension to make use of JIT for a 20%+ performance boost!'
. PHP_EOL . PHP_EOL);
}
$progress->write(PHP_EOL
. 'JIT acceleration: OFF (opcache not installed or not enabled)' . PHP_EOL
. 'Install and enable the opcache extension to make use of JIT for a 20%+ performance boost!'
. PHP_EOL . PHP_EOL);
}
if (isset($options['force-jit']) && !$hasJit) {
$progress->write('Exiting because JIT was requested but is not available.' . PHP_EOL . PHP_EOL);
Expand Down
62 changes: 27 additions & 35 deletions src/Psalm/Internal/Fork/PsalmRestarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use function array_splice;
use function assert;
use function count;
use function defined;
use function extension_loaded;
use function file_get_contents;
use function file_put_contents;
Expand All @@ -23,14 +22,11 @@
use function strlen;
use function strtolower;

use const PHP_VERSION_ID;

/**
* @internal
*/
final class PsalmRestarter extends XdebugHandler
{
public const MIN_PHP_VERSION_WINDOWS_JIT = 8_04_03;
private const REQUIRED_OPCACHE_SETTINGS = [
'enable' => 1,
'enable_cli' => 1,
Expand Down Expand Up @@ -86,31 +82,31 @@ protected function requiresRestart($default): bool
static fn(string $extension): bool => extension_loaded($extension),
);

$opcache_loaded = extension_loaded('opcache') || extension_loaded('Zend OPcache');
if (!extension_loaded('opcache') && !extension_loaded('Zend OPcache')) {
return true;
}

if ($opcache_loaded) {
// restart to enable JIT if it's not configured in the optimal way
foreach (self::REQUIRED_OPCACHE_SETTINGS as $ini_name => $required_value) {
$value = (string) ini_get("opcache.$ini_name");
if ($ini_name === 'jit_buffer_size') {
$value = self::toBytes($value);
} elseif ($ini_name === 'enable_cli') {
$value = in_array($value, ['1', 'true', true, 1]) ? 1 : 0;
} elseif (is_int($required_value)) {
$value = (int) $value;
}
if ($value !== $required_value) {
return true;
}
foreach (self::REQUIRED_OPCACHE_SETTINGS as $ini_name => $required_value) {
$value = (string) ini_get("opcache.$ini_name");
if ($ini_name === 'jit_buffer_size') {
$value = self::toBytes($value);
} elseif ($ini_name === 'enable_cli') {
$value = in_array($value, ['1', 'true', true, 1]) ? 1 : 0;
} elseif (is_int($required_value)) {
$value = (int) $value;
}

$requiredMemoryConsumption = $this->getRequiredMemoryConsumption();

if ((int)ini_get('opcache.memory_consumption') < $requiredMemoryConsumption) {
if ($value !== $required_value) {
return true;
}
}

$requiredMemoryConsumption = self::getRequiredMemoryConsumption();

if ((int)ini_get('opcache.memory_consumption') < $requiredMemoryConsumption) {
return true;
}

return $default || $this->required;
}

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

$additional_options = [];
$opcache_loaded = extension_loaded('opcache') || extension_loaded('Zend OPcache');

// executed in the parent process (before restart)
// if it wasn't loaded then we apparently don't have opcache installed and there's no point trying
// to tweak it
if ($opcache_loaded &&
!(defined('PHP_WINDOWS_VERSION_MAJOR') && PHP_VERSION_ID < self::MIN_PHP_VERSION_WINDOWS_JIT)
) {
$additional_options = [];
foreach (self::REQUIRED_OPCACHE_SETTINGS as $key => $value) {
$additional_options []= "-dopcache.{$key}={$value}";
}
$additional_options = $opcache_loaded ? [] : ['-dzend_extension=opcache'];
foreach (self::REQUIRED_OPCACHE_SETTINGS as $key => $value) {
$additional_options []= "-dopcache.{$key}={$value}";
}

$requiredMemoryConsumption = $this->getRequiredMemoryConsumption();
$requiredMemoryConsumption = self::getRequiredMemoryConsumption();

if ((int)ini_get('opcache.memory_consumption') < $requiredMemoryConsumption) {
$additional_options []= "-dopcache.memory_consumption={$requiredMemoryConsumption}";
}
if ((int)ini_get('opcache.memory_consumption') < $requiredMemoryConsumption) {
$additional_options []= "-dopcache.memory_consumption={$requiredMemoryConsumption}";
}


array_splice(
$command,
1,
Expand All @@ -197,7 +189,7 @@ protected function restart($command): void
/**
* @return positive-int
*/
private function getRequiredMemoryConsumption(): int
private static function getRequiredMemoryConsumption(): int
{
// Reserve for byte-codes
$result = 256;
Expand Down

0 comments on commit 3f17a6b

Please sign in to comment.