An open tool case with screwdrivers, spanners, and pliers, seen from above. In the foreground, two hands hold a screwdriver handle with a bit holder attached.
The attachment reaches screws that the handle cannot reach on its own. Nobody drives a screw faster with it, and everything else in the case works without it.

In the previous two articles, I looked at rewrites of other people's software: how to tell whether a rewrite is correct, and whether you can depend on it. It is only fair to apply the same questions to my own project.

The obvious question is whether PHPUnit should follow PHPStan's example and move its hot code paths into a native extension, or be rewritten in a systems language altogether. It is a reasonable question. PHPUnit runs in the CI pipelines of nearly every PHP project, and every second it saves is multiplied many times over.

If the goal is speed, my answer is no. There is, however, a different reason why a native extension might be worth building, and it has little to do with making PHPUnit faster.

PHPUnit spends little time in its own code

These are the parts of PHPUnit and its components that do real computational work:

The parts of PHPUnit and its components that do real computational work
Area Component Why it costs time
Comparing values sebastian/comparator, sebastian/recursion-context Walks large arrays and object graphs and tracks the objects it has already seen. Runs for every assertEquals(), successful or not.
Exporting values sebastian/exporter Builds string representations of large structures recursively. Runs only when a test fails.
Diffs sebastian/diff Expensive only for large strings, and runs only when a test fails.
Global state sebastian/global-state Copies globals and static properties before a test and restores them afterwards. Opt-in via backupGlobals or backupStaticProperties, disabled by default, and not needed for modern code.
Test doubles PHPUnit's test double generator Uses reflection, generates code as a string, then calls eval().
Events PHPUnit's event system Creates several event objects for every test. Events for every assertion were removed in PHPUnit 11 because of their cost.

In a typical test suite, however, almost all of the time is spent in the code under test: bootstrapping the application, talking to the database, waiting for I/O. This is the case I described in the first article of this series: a tool that executes code spends its time in the code it executes. Comparison is the only expensive operation that runs on every successful assertion. Exporting values and computing diffs happen only when a test fails. The normal state of a test suite is that all of its tests pass, so neither is a cost worth optimising. Moving all of this to native code might shave a few percent off a real-world suite.

I should be honest about the word “might”. I have not measured this systematically yet. The right way to settle it is to run a handful of large real-world test suites under a profiler such as the one in Xdebug, SPX, or Tideways and measure the share of time spent in code from the PHPUnit\ and SebastianBergmann\ namespaces. Until those numbers exist, the estimate above is an expectation based on how such suites usually behave, not a measurement.

A second implementation would cost more than it saves

Suppose the measurements came back and showed that comparison accounts for a noticeable share of a test run. Moving it into an extension would still come at a high price.

The comparator would need a pure-PHP fallback. Many PHPUnit users cannot install extensions: they run on shared CI images, on Windows, or use the PHAR, which cannot carry native code. So there would be two implementations of the same behaviour, and that behaviour would have to match exactly. For a comparator, “exactly” means reaching the same verdict in every edge case, from floating-point deltas to recursive object graphs, and reporting a failure in exactly the same way.

The first article of this series described what that takes: a reference implementation, a test suite that runs in both configurations, differential tests, and a version gate. PHPStan has all of that, and it pays off for PHPStan because a static analyser spends its time in its own code. Saving 58 % of the time on the work that dominates every run is worth the cost of a second implementation. A few percent on work that is dwarfed by the code under test is not.

The second article asked who maintains a dependency, and for how long. A native comparator would be a second code base that needs reviewers who understand both versions. That would narrow the circle of people who can contribute to code that every PHPUnit user relies on. It is a bad trade: a small gain for a large and lasting cost.

An extension is worth it for what PHP code cannot do

The stronger case for an extension is not speed but capability: things PHP code cannot do at all, or only clumsily. There is a precedent for this in PHPUnit's own ecosystem. For many years, php-code-coverage has used Xdebug and, later, PCOV to collect code coverage. PHPUnit works without them and does more when they are loaded. A PHPUnit extension would follow the same model.

  • Per-test data for test impact analysis. The observer API, available since PHP 8.0, lets an extension see every function call. Recording which functions run during each test is far cheaper than collecting full line coverage, and it may well be enough for test impact analysis.
  • Reliable time limits. PHPUnit's time limits (--enforce-time-limit and --default-time-limit) currently rely on SIGALRM via the pcntl extension. That does not work on Windows, and it gets in the way when the code under test uses SIGALRM itself. The engine has its own interrupt mechanism, which it uses for max_execution_time. An extension could set the interrupt flag when a test's time is up, and the engine would stop at the next safe point. One caveat: a test that is blocked inside a single internal function call, waiting for I/O for instance, would only be stopped once that call returns.
  • A fork server for process isolation. Every test that runs in a separate process currently gets a fresh PHP process, which has to bootstrap everything again. A fork server would bootstrap once and fork a child process for each isolated test. The pcntl extension can do part of this today; an extension could reset the engine's state more reliably. There is no fork on Windows, so this would remain a feature for Linux and macOS.
  • Precise tracking per test of memory use, output, and error and exception handlers, by hooking into the engine instead of comparing snapshots before and after a test. PHPUnit already detects some of this by comparison, such as output buffers or error handlers that a test leaves behind.

All of these would be optional. PHPUnit would work exactly as it does today without the extension and do more when it is loaded. None of them requires a second implementation of anything that decides whether a test passes. The extension would provide mechanisms and observations; the decisions would remain in PHP code.

The extension would be written in C

In the second article, I quoted an article on heise.de that argues languages lose importance when agents write the code. For an engine extension, I do not think that holds, and the reason is the one the second article was about: who can review and maintain it.

For a PHP extension, the answer is C:

  • PHP's internal API is C, and it leans heavily on macros. Every reference, every existing extension, and nearly everyone who could review or maintain this code works in C.
  • The build tooling and the distribution channels assume C: phpize, PIE, the Windows builds, and the packagers of Linux distributions.
  • Keeping up with every PHP release, with thread-safe and non-thread-safe builds, and with engine APIs such as the observer API and the interrupt mechanism is easiest in the language php-src itself is written in.

The alternatives each have a specific problem:

  • Rust is a defensible second choice. ext-php-rs exists, and memory safety is a real benefit for code that runs inside every test process. The bindings have caught up in places; the observer API, for instance, is supported behind a feature flag. But they still change in breaking ways between releases, anything beyond the APIs they wrap means working with raw bindings, and on Windows they require a nightly Rust toolchain. Rust is worth considering only if the extension's surface stays small and its maintainers already know Rust.
  • C++ offers no real advantage here and adds problems with the C++ runtime and its ABI. PHPStan's extension shows what that costs: on Linux, it links the C++ runtime statically so that its binaries do not depend on the library versions of the machine they were built on.
  • Go is a poor fit. Its runtime and garbage collector would run inside the PHP process, every call through cgo adds overhead, and forking a process while the Go runtime is running is not safe. That alone rules out the fork server.

So yes, I think the language matters here, even if an agent writes much of the code. Not because of how pleasant it is to write, but because of who can read, review, and maintain it, and how well it fits the platform it extends.

The logic stays in PHP

Putting this together:

  1. Do not port PHPUnit's logic to native code. Comparison, export, diff, and everything else that shapes the result of a test or its output stays in PHP, so that its behaviour is identical everywhere and anyone who knows PHP can contribute to it.
  2. Profile first. Measure where real-world test suites spend their time before deciding anything.
  3. If an extension is built, keep it small, optional, and written in C. It exposes a few engine-level mechanisms: observer-based tracking per test, interrupt-based time limits, a fork server, and precise tracking of what a test leaves behind. PHPUnit uses them when the extension is loaded and works as before when it is not.

This is where the three articles meet. The best way to avoid needing a parity harness is not to have two implementations of the same behaviour in the first place. An extension that adds capabilities instead of duplicating logic has no oracle problem.

It also passes the questions from the second article more easily. Its surface is small, and the people who could maintain it already work in the language it is written in. If it were ever abandoned, PHPUnit would lose a few capabilities and nothing else. That is what makes it something I would be willing to depend on, and to ask others to depend on.