Skip to content

If available override the current process with pcntl_exec - #176

Open
staabm wants to merge 1 commit into
composer:mainfrom
staabm:pcntl_exec
Open

staabm wants to merge 1 commit into
composer:mainfrom
staabm:pcntl_exec

Conversation

@staabm

@staabm staabm commented Oct 18, 2026 •

Copy link
Copy Markdown

inspired by AI driven work in PHPStan-src, I realized that the pcntl extension has a pcntl_exec feature which allows to override the current process (instead of starting a sub-process and bridging signals, output back into the main process).

while this does not make things faster in my testing, I can see it saves memory

➜  xdebug-handler git:(pcntl_exec) ✗ hyperfine 'php test.php' --runs=5
Benchmark 1: php test.php (5 runs)
                mean     ±       σ          min     …     max
  Wall Time    3.255 s   ±   0.009 s      3.247 s   …   3.266 s
  Memory        28.4 MiB ±     0.1 MiB     28.3 MiB …    28.4 MiB
 
➜  xdebug-handler git:(main) ✗ hyperfine 'php test.php' --runs=5         
Benchmark 1: php test.php (10 runs)
                mean     ±       σ          min     …     max
  Wall Time    3.254 s   ±   0.010 s      3.243 s   …   3.269 s
  Memory        29.6 MiB ±     0.1 MiB     29.4 MiB …    29.8 MiB 

when running my super-simplified test.php:

<?php

use Composer\XdebugHandler\XdebugHandler;

require_once __DIR__ . '/vendor/autoload.php';

var_dump(extension_loaded('xdebug'));
echo "booting a second...\n";
sleep(1);

$xdebug = new XdebugHandler('myapp');
$xdebug->check();
unset($xdebug);

var_dump(extension_loaded('xdebug'));
echo "running a second...\n";
sleep(1);

I guess perf numbers might be different in case xdebug-handler needs to bridge a lot of data between the subprocess and the parent process. but I was not yet able to proof that with examples.


env:

➜  xdebug-handler git:(main) ✗ php -v                                   
PHP 8.5.11 (cli) (built: Sep 22 2026 13:32:06) (NTS)
Copyright (c) The PHP Group
Built by Homebrew
Zend Engine v4.5.11, Copyright (c) Zend Technologies
    with Xdebug v3.5.3, Copyright (c) 2002-2026, by Derick Rethans
    with Zend OPcache v8.5.11, Copyright (c), by Zend Technologies

➜  composer git:(main) php bin/composer diagnose                
Composer version: 2.11.999-dev+source
Checking Composer and its dependencies for vulnerabilities: OK
PHP version: 7.2.5 - Package overridden via config.platform, actual: 8.5.11
PHP binary path: /opt/homebrew/Cellar/php/8.5.11_1/bin/php
OpenSSL version: OpenSSL 4.0.3 29 Sep 2026
curl version: 8.23.0 libz 1.2.12 brotli brotli/1.2.0 zstd supported ssl OpenSSL/4.0.3 HTTP 1.0, 1.1, 2, 3
zip: extension present, unzip present, 7-Zip not available
Active plugins: 
Checking composer.json: OK
Checking composer.lock: OK
Checking platform settings: OK
Checking git settings: OK git version 2.56.0
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com oauth access: OK does not expire
Checking disk free space: OK

we are using this technique in phpstan-src 2.2.10 and 2.3.1 releases and it seems todo quite well. we do not yet have long term experience with it though.

this PR is mostly meant to get a discussion going and test this approach more widely and in more use-cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant