WritingNo. 0048 min readby Bernardo Cotrim

Native PHP in Studio: How Radical Speed Month Went

Radical Speed Month is over, and native PHP shipped as a beta in Studio 1.10. The numbers, the Windows detour, a 138% outlier that turned out to be queueing, and a Unix lesson about killing process groups.

Four weeks ago I wrote a hopeful post about a working Studio build: a native PHP binary, Blueprints installing WordPress, and green end-to-end tests. “Works on my computer.” The fun question, is it actually any good?, was still open.

Yesterday it shipped as a beta feature in Studio 1.10. Sites use a fraction of the memory, and the regression I’d been worried about is gone. This is the story of what happened in between.

Radical Speed Month officially ended on 22 May. Fredrik Rombach Ekelund and I kept going until it shipped. Part one ended with three open questions: real benchmarks, the concurrency story, and how to surface this in the UI. The UI one was the easy answer: a single “Native PHP runtime” switch under Beta features, global for all sites (#3456). The other two took the rest of the month.

Getting Windows to run at all

Before I could benchmark Windows, I had to get it running. That was most of the middle of the month. I knew Windows would be different. I didn’t know it would be different in every layer at the same time.

It starts with the binary. On macOS and Linux we build static binaries with static-php-cli, with every extension compiled in. Windows ships the official windows.php.net build instead, with each extension as a separate DLL11 static-php-cli can’t compile Xdebug into a static binary, and its support for shared extensions only works on Unix-like systems. Fredrik switched Windows to the official build in #3439.. PHP doesn’t load those DLLs unless you tell it to, so the server needed explicit extension= flags (#3471).

Blueprints run in their own PHP subprocess, and that subprocess didn’t get the flags. So every Blueprint failed with “WordPress requires the sqlite extension.” The fix was a temporary php.ini for each Blueprint run that loads Studio’s bundled Windows extensions, including pdo_sqlite and sqlite3, passed with -c. HTTPS needed the same treatment. PHP on Windows needs a CA bundle for openssl and curl, and the build we ship doesn’t come with one. So we write a temporary bundle from Node’s trusted root certificates and point PHP at it with openssl.cafile and curl.cainfo (#3491).

Along the way I tripped over two Windows-only problems in blueprints.phar itself, around cleaning up after a Blueprint run. I opened WordPress/php-toolkit#280 upstream for them.

How we measured

The benchmark drives Studio’s CLI in both modes, STUDIO_RUNTIME=playground and STUDIO_RUNTIME=native-php, and times two things. The first is an empty site’s lifecycle: create, start, restart. The second is the editor session from Studio’s site editor benchmark: open the site editor, browse templates, open one, add blocks, save. We also take memory snapshots at the same points. Five runs each, medians reported.

The numbers below are from Windows. Most of the month’s surprises were Windows-flavored, and Windows is where the runtime difference is easiest to see.

What the numbers say

The lifecycle and editor results. Negative percentages mean native PHP is faster:

MetricPlaygroundNative PHPΔ
start5.93 s4.00 s−32.6%
restart12.69 s10.69 s−15.8%
siteEditorLoad5.56 s5.36 s−3.6%
templateSave0.45 s0.39 s−14.0%
create+start20.43 s23.97 s+17.3%
templatesViewLoad2.78 s3.20 s+15.1%
blockAdd1.67 s1.90 s+13.4%
templateOpen4.23 s10.06 s+138.0%

And memory, where native PHP is simply better:

MetricPlaygroundNative PHPΔ
site working set541 MB154 MB−71.6%
site private memory692 MB113 MB−83.7%
standalone working set587 MB200 MB−66.0%
standalone private memory754 MB174 MB−76.9%

Memory wins by a wide margin. Lifecycle wins more than it loses. And there’s one big, honest outlier: templateOpen, 138% slower. That’s the part I had to go figure out.

The outlier

I added a second harness that waits for the browser’s network to go idle before clicking “Open template.” That takes the editor’s startup burst of REST requests out of the measurement. That run looked very different:

ScenarioTime to first visible block (median)
Playground CLI1.08 s
Native PHP, single worker0.83 s

When the template request doesn’t have to compete with the editor’s other requests, native PHP is faster. So the 138% regression isn’t slow rendering. It’s queueing. PHP’s built-in server on Windows handles one request at a time22 PHP_CLI_SERVER_WORKERS forks workers on macOS and Linux only. Windows support is an open request upstream: php/php-src#12071., so the editor’s burst of concurrent requests lines up behind whatever slow response is in flight. OPcache and a few other PHP settings didn’t move the needle. The bottleneck is the server, not the runtime.

So the fix is more than one server. The proof of concept: spawn several php -S processes and spread requests across them. With four workers, in a fresh run of the original harness that clicks immediately:

ScenarioTime to first visible blockvs Playground
Native PHP, 4-worker POC3.50 s−28.8%

A 138% regression turned into a 28.8% win. Native PHP on Windows wasn’t slow. A single php -S was serializing the editor’s burst of requests. Add more php -S processes and the outlier goes away.

The bug that taught me Unix again

On macOS and Linux, PHP already has an answer for concurrency: PHP_CLI_SERVER_WORKERS. Fredrik turned it on with four workers (#3580), and that’s where the most interesting bug of the month came from. It wasn’t on Windows. It was about restarting.

The scenario: change the WordPress version on a Studio site. Studio stops PHP, swaps the WordPress files, and starts PHP again on the same port. The restart fails with Address already in use.

The PHP server was supposed to be stopped. I had stopped it. The OS disagreed.

With PHP_CLI_SERVER_WORKERS=4, PHP forks four workers from the parent php -S. Studio supervises the parent. The workers are PHP’s children, not Studio’s. When Studio sent SIGTERM to the parent, the parent exited cleanly. The workers didn’t. They were still bound to the listening socket, and the next php -S on that port got EADDRINUSE.

In hindsight this is obvious. Killing a parent does not kill its children. I knew this. I had still written code that assumed it did.

The right primitive is a process group. Every Unix process belongs to one. Send a signal to the negative PID and it reaches the group leader and every process in its group. In Node, detached: true makes a child the leader of its own group. The fix that mattered (#3588) was basically this:

// Spawn the long-running PHP server in its own process group on Unix.
const serverChild = spawnPhpProcess( args, {
    // ...
    detached: process.platform !== 'win32',
} );

// On stop / restart / cleanup, signal the whole group.
function killProcessGroup( child, signal ) {
    if ( process.platform !== 'win32' && child.pid ) {
        try {
            process.kill( -child.pid, signal );
            return;
        } catch {
            // Group already gone — fall back to the parent.
        }
    }
    child.kill( signal );
}Code language: JavaScript (javascript)

Use that everywhere we used to call child.kill() on the long-running server. Leave Windows alone: different process model. Restart now does what the user expected the first time.

The lesson I keep relearning: when your application code starts to look like an operating system, check whether the operating system already has the primitive you’re building. More often than I’d like to admit, it does.

From proof of concept to pool

That left two ways to get concurrency: PHP_CLI_SERVER_WORKERS on macOS and Linux, and nothing at all on Windows. We wanted one mechanism everywhere. Fredrik and I turned the proof of concept into the real thing side by side at our team meetup (#3602). We found no noticeable drawback compared to PHP_CLI_SERVER_WORKERS, so the pool replaced it on every platform.

The shape is the same as the proof of concept: a poor man’s php-fpm. Four php -S workers each listen on their own loopback port, and a small Node HTTP proxy on the site’s port forwards requests to them33 In part one I worried that more than one worker would hit SQLite locks fast. That turned out to be a Playground-shaped worry. SQLite handles concurrent access on a normal file system; the workarounds were for WebAssembly.. A counter per worker tracks requests in flight. Each request goes to the worker with the fewest, which means an idle one if there is one. Anything that isn’t a GET, HEAD or OPTIONS goes to a primary worker, and so does phpMyAdmin. Everything else fans out.

Restarts had to stay correct, too. With four workers running, stopping a site has to take all of them down, or the next start gets EADDRINUSE from whichever one survived. That’s the bug from the previous section, now on every platform. On Unix it’s the same process-group kill. On Windows it’s taskkill /T on the wrapper and on every worker PID it reports. Same problem, two operating systems, two different primitives.

Net result: the templateOpen regression is gone, and queueing is no longer the bottleneck. Whenever native PHP is on, the pool is on, on every platform. No extra flag.

What’s next

Studio 1.10 shipped on 2 June with native PHP as a beta feature. Turn on “Native PHP runtime” under Beta features, then start or restart your sites, and they run on the new runtime. If it holds up with real users, the next step is making it the default.

One side effect I’m curious about is AI agents. Studio Code, the agent in Studio’s CLI, leans on WP-CLI, and WP-CLI on native PHP is just WP-CLI: the same binary, with access to the site’s real files. I suspect a lot of edge cases get easier for free.

One honest caveat: the numbers here come from Windows, one machine, an empty site, and the editor workflow we picked. They’re enough to ship a beta. They’re not the whole picture.

The first weeks were about whether the new runtime could work. The last stretch was about whether the boring parts would hold. They did, and Studio 1.10 shipped it as a beta.

  1. static-php-cli can’t compile Xdebug into a static binary, and its support for shared extensions only works on Unix-like systems. Fredrik switched Windows to the official build in #3439. ↩︎
  2. PHP_CLI_SERVER_WORKERS forks workers on macOS and Linux only. Windows support is an open request upstream: php/php-src#12071. ↩︎
  3. In part one I worried that more than one worker would hit SQLite locks fast. That turned out to be a Playground-shaped worry. SQLite handles concurrent access on a normal file system; the workarounds were for WebAssembly. ↩︎

Tags: PHPStudio

Spotted a mistake? Tell me.

Get new posts by email.