Skip to main content
← Back to blog

How We Halved the Cost of Profiling a PHP Function Call

B
Ben Poulson · Founder
November 22, 2026 · 5 min read

How We Halved the Cost of Profiling a PHP Function Call

A profiler that runs in production has two jobs before it can be useful: don’t make your application noticeably slower, and don’t make it less safe. Over the last few weeks we went through the Perfbase PHP extension line by line with exactly that in mind. The result is our biggest release of the extension so far:

  • a profiled function call costs about half what it did,
  • turning a trace into data (perfbase_get_data()) is 2–2.5× faster,
  • an idle extension (loaded, nothing being profiled) is cheaper on PHP 8.3+,
  • timings are precise by default instead of rounded to a kernel tick,
  • SQL literals, URL credentials and the secrets that leak into error messages are redacted before data leaves the process,
  • and it is tested against real PHP-FPM and Apache workers on every PHP version from 7.4 up.

Here’s what changed and why.

The numbers

All figures are medians on the same machine (PHP 8.3, Linux x86-64), in nanoseconds of overhead per profiled call compared with the same code running without a trace.

WorkloadBeforeNow
Empty function137 ns69 ns
A call tree156 ns85 ns
Method calls152 ns75 ns
Built-in (internal) functions132 ns71 ns
Traced calls (PDO, curl, Redis…)345 ns150 ns
Allocation-heavy code, memory tracking on1,536 ns768 ns
Three spans active at once, CPU time on1,212 ns643 ns

And for the moment a trace is collected:

perfbase_get_data()BeforeNow
Deep call tree (~50,000 call paths)51.6 ms21.9 ms
Metadata-heavy trace (thousands of distinct SQL queries)144.8 ms66.0 ms
Compressing an ordinary request’s trace1.7 ms0.19 ms

One honest caveat: the “now” column also pays for real timings. The old default clock had the resolution of a kernel tick (1–4 ms), so most short calls were recorded as zero. The new default reads the CPU’s calibrated timestamp counter. Even so, every call got cheaper.

Where the time went

One lookup per function, not four per call

Every time PHP calls a function, the profiler needs to know a few things about it: its interned name, whether it needs a trace callback (PDO, curl…), whether that callback runs on exit, and whether it is a magic method. That used to be four hash lookups on every single call. Now it is one record per function, built the first time the function is seen, with a fast path for “same function as last time”.

No thread-locals on the hot path

PHP’s non-thread-safe builds run one request per thread, so the profiler’s state can live in plain process globals. Moving it out of thread-local storage removed a hidden function call from every access.

Previously each new entry in the call tree copied its whole call path. Under deep recursion that cost grew with the square of the depth. Entries now point at their caller instead, frames remember their entry’s index (so leaving a function is a direct index, not a hash probe), and a small callee cache skips hashing entirely in tight loops. The tree is rebuilt from those links in linear passes when the data is collected.

Several spans, one measurement

If you profile with more than one span active, the clocks are now read once per call for all of them, and a trace callback (say, normalising a SQL query) runs once and is shared. The CPU-time column above is where this shows.

Compression without the fixed cost

We found that two Brotli settings we were using pick an internal match-finder with a fixed ~2 ms cost per call, regardless of input size. For a small request’s 4 KB trace that was nearly all of the time. Avoiding those settings made compressing an ordinary trace 9× faster for under 1% more bytes.

Idle really means idle on PHP 8.3+

When the extension is loaded but nothing is being profiled, it should cost as close to nothing as possible. On PHP 8.3 and later we now attach our function hooks lazily: until the first perfbase_enable() of a request, plain user functions get no profiler hooks at all. Idle cost per user call fell from 18 ns to 8 ns on PHP 8.4 / 8.5, and from 16 ns to 11 ns on PHP 8.3.

The JIT stays on

On PHP 8.1.18 and later with OPcache’s JIT configured, the extension now uses PHP’s observer API for user functions, so the JIT keeps working while you profile.

Safety: what gets redacted before it leaves your server

A profiler sees everything your application does, so it has to be careful about what it records. Every value now passes through a single sanitising step before it is stored:

  • SQL is normalised with the dialect of the connection it runs on — MySQL, PostgreSQL, SQLite and friends treat quotes differently, and getting that wrong can leak a literal. Literals become placeholders.
  • URLs lose their username, password, query string and fragment.
  • DSNs keep host, port and database name; every other parameter (passwords included) becomes ?.
  • Shell commands are reduced to the program name. Mail recipients to their domain. Mail subjects are not recorded at all.
  • Error and exception messages are redacted where they are stored: stack traces are cut, SQL after Laravel’s SQL: marker is normalised, quoted values in database errors are masked, and credential-looking key=value pairs become key=?.

Every cap lands on a character boundary, and non-UTF-8 input is handled instead of crashing.

Tested where PHP actually runs

Unit tests are necessary but not sufficient for an extension that lives inside PHP. Alongside 700+ Rust unit tests, every build now runs:

  • the PHP test suite on PHP 7.4 through 8.5, with OPcache off, on, and with both JIT modes,
  • dozens of focused “probe” scripts that check one real-PHP behaviour each — closures reused in memory, generators, fibers, forking mid-trace, hitting memory_limit while collecting data, another extension hooking the same functions, and so on,
  • a PHP-FPM fixture that drives a real FPM pool through hundreds of requests, worker recycling, fatal errors, timeouts and a 3,000-request memory soak,
  • an Apache mod_php fixture that does the same through graceful restarts.

Those fixtures found real problems before you could: settings that FPM’s environment clearing silently dropped, and an opcache.preload edge case where the FPM master started threads it should not own. Both are fixed.

Upgrading

There’s nothing to change in your code. A few behaviours are worth knowing:

  • CPU time is opt-in. Reading a thread’s CPU clock is a real system call on Linux (~250 ns, twice per call). Pass PERFBASE_FLAG_CPU_TIME when you want it.
  • The file stat family isn’t traced (file_exists, is_file, …). Autoloaders call them thousands of times per request; they were noise.
  • Very short traces may show an empty process list. The host process scan runs in the background, and your request does not wait for it.

Try it

The faster extension ships through the same installer as before:

bash -c "$(curl -fsSL https://cdn.perfbase.com/install.sh)"

If you are new to Perfbase, the setup guide walks through installing the extension, creating a project and sending your first traces. When a trace comes in, the flame graph guide shows how to find the expensive call path. Or create a free account and profile the slow request you already know about.

B
Ben Poulson · Founder

Building Perfbase - PHP performance monitoring for teams. Passionate about making PHP applications faster and more observable.