TubeSum ← Transcribe a video

Did You Know? PHP Optimizes These Function Calls for You!

0h 10m video Published Aug 14, 2025 Transcribed Aug 3, 2026 T Tideways
Intermediate 5 min read For: PHP developers interested in performance optimization and understanding compiler internals.
AI Trust Score 70/100
⚠️ Average / Some Fluff

"Delivers on the promise with a clear explanation and benchmarks, though the title slightly oversells the impact."

AI Summary

This video explains compiler optimized functions in PHP, a hidden performance feature that optimizes calls to certain internal functions when they are imported from the global namespace. The presenter, Benjamin, discusses how this optimization works, its performance impact, and whether it's worth migrating code to use it.

[00:02]
Introduction to Compiler Optimized Functions

Function calls in PHP automatically fall back to the global namespace if not defined in the current namespace, but there's a hidden performance gem in the PHP compiler and opcache that can optimize function calls under special circumstances.

[00:30]
What Are Compiler Optimized Functions?

Compiler optimized functions are a list of hard-coded functions in the PHP source code (zend_compile.c) that can be optimized when called unambiguously from the global namespace. Examples include strlen, count, sprintf, is_array, is_object, and other type-checking functions.

[01:52]
Visualizing the Optimization with Opcodes

By running PHP with opcache debug flags, you can see the opcodes emitted. Without importing, the compiler emits generic function calls; with importing, it uses specialized opcodes like 'STRLEN'.

[02:52]
Benchmarking the Performance Difference

A synthetic benchmark calling strlen 1 million times shows the compiler optimized version runs 1.46 times faster (20-60% faster) than the fallback version, with a statistical corridor of 0.24 times.

[04:46]
When Does This Optimization Matter?

For most application code, the optimization is less important, but it matters when functions are called hundreds of thousands of times and response times are already low (20-200ms).

[05:19]
PHP 8.4: sprintf Optimization

In PHP 8.4, sprintf was added to the list. If only %s and %d modifiers are used, sprintf calls can be optimized down to native string interpolation, allowing migration from string interpolation to more readable sprintf without performance penalty.

[05:47]
Tools for Automatic Conversion

Three tools can automatically convert code to use global namespace imports: PHP-CS-Fixer, PHP CodeSniffer with the 'used_names_only' rule, and PHPStorm's auto-import feature. PHP-CS-Fixer is the most comprehensive.

[08:47]
Should You Import Functions?

It depends. If the overhead is significant, yes; otherwise, it's too micro. Tideways profiler can detect code that would benefit, and this happens in 2-5% of customers.

Compiler optimized functions are a real but micro optimization. Importing functions from the global namespace can yield 20-60% speedups in specific hot paths, but it's rarely necessary for most applications. Tools like PHP-CS-Fixer can automate the migration, and profilers can identify when it's worth doing.

Mentioned in this Video

Study Flashcards (7)

What are compiler optimized functions in PHP?

easy Click to reveal answer

A list of hard-coded internal functions that the PHP compiler and opcache can optimize when called unambiguously from the global namespace.

00:30

Which functions are included in the compiler optimized list?

easy Click to reveal answer

Examples include strlen, count, sprintf, is_array, is_object, and other type-checking functions.

01:23

How much faster is the compiler optimized version in the benchmark?

medium Click to reveal answer

It runs 1.46 times faster (20-60% faster) than the fallback version.

04:03

What is the condition for the optimization to kick in?

medium Click to reveal answer

The function must be imported directly from the global namespace, not relying on the automatic fallback mechanism.

01:11

What did PHP 8.4 add to the list of compiler optimized functions?

medium Click to reveal answer

sprintf, but only when using %s and %d modifiers, allowing optimization to native string interpolation.

05:19

What tools can automatically convert code to use global namespace imports?

medium Click to reveal answer

PHP-CS-Fixer, PHP CodeSniffer with the 'used_names_only' rule, and PHPStorm's auto-import feature.

05:47

In what percentage of Tideways customers does this optimization matter?

hard Click to reveal answer

In 2-5% of customers.

09:14

💡 Key Takeaways

💡

Hidden Performance Gem

Reveals a little-known PHP optimization that can significantly speed up function calls.

00:30
📊

Benchmark Results

Provides concrete numbers showing 20-60% performance improvement.

04:03
📊

PHP 8.4 sprintf Optimization

Highlights a recent addition that allows using sprintf without performance penalty.

05:19
⚖️

It Depends

Offers a balanced perspective on when to apply this optimization.

08:47

[00:02] or a fully qualified count with namespace operator in a codebase before and thought this seems pointless and annoying? Technically, it's not needed. Function calls in PHP automatically fall back to the global namespace if they are

[00:16] not defined in the current namespace they are called in. But there's a hidden performance gem in the PHP compiler and opcache that can optimize function calls under special circumstances. In this video, I'm going to explain compiler

[00:30] optimized functions and discuss if it's worth it to migrate your code base to use them. Mo, I am Benjamin and I'm working on PHP performance related topics for the last 10 years helping thousands of developers, companies, and

[00:44] open source projects along the way. In a recent Reddit post titled excessive micro optimization, did you know the author shows a code benchmark example of timing string lang against a globally imported string lang and ask if the

[00:58] impressive performance gains are visible in real world applications. The feature they described is called compiler optimized functions and it's only activated for a list of hard-coded functions that are uh listed in the

[01:11] zcore compile C file in the PHP source code. Most importantly, they need to be uh imported directly and can't rely on the automatic fallback mechanism in PHP.

[01:23] The list includes functions such as string leng count sprint f and many is array is object type checking functions and others. When the compiler encounters them called without relying on the fallback mechanism unambiguously from

[01:38] the global namespace, then this optimization kicks in. You can see the difference by looking at op codes that PHP emits when compiling the different code examples. You can see op codes of a script by running PHP on the CLI with a

[01:52] special flag. So in this case here PHP we enable opcache and opcache debug level um to a certain bit mask and then we run this on our string lang namespace

[02:05] example where we don't import the function function and there we can see that we initialize a namespace function called by name app string repeat also uh initial function

[02:20] called by name app string lang and then a generic function call do function call by name. And then if we uh compare the same with same with uh the script imported from

[02:40] codes we can see that we use a specialized string lang op code here. You might raise the point that this sounds like a micro optimization and you are correct. Nevertheless, let me measure the difference of this

[02:52] optimization in a madeup synthetic benchmark that calls string leng 1 million times and we use hyperfine to measure the difference. Hyperfine is a benchmarking tool that uh takes care of statistical averaging and making sure

[03:07] that the load on the machine is comparable between runs and then uh calculates the values so that we can actually u make out a good difference. So I'm running the command here with the minus vi flag which is um w which is

[03:23] warm up runs. Then I'm calling the benchmark compiler optimized code which uses the namespace argument here. So benchmark benchmark compiler optimize PHP against the

[03:38] version which is using the fallback mechanism. So, PHP benchmark fallback

[03:50] fallback mechanism.php. times um and compares the the output with each other. In this case, we can

[04:03] see here um the compiler optimized version uh um the compiler optimized version uh runs 1.46 46 times faster with a um runs 1.46 46 times faster with a um statistic um corridor of 0.24 times

[04:19] faster or slower than this value. So it's really about 20 to 60% faster than it's really about 20 to 60% faster than not using um uh the fallback me uh than

[04:31] using the fallback mechanism for open source code. You could argue that there should be a matter of good style to provide the lowest overhead possible. But for your own application code, this optimization is less important. But

[04:46] there are cases where this optimization does matter as well. When you call these compiler optimized functions hundreds of thousands of times in your application code and it already has a low response time in the 20 to 200 milliseconds range

[05:03] then uh you will see a difference um when compiler optimized functions take up a huge share. Granted these cases are rare but it's good uh to having heard about this optimization nonetheless. In PHP 8.4 Before my colleague Tim added

[05:19] sprintf to the list of compiler optimized functions. If you use only the percentage s and percentage d modifiers then php and opcache can optimize the sprintf calls down to native string interpolation.

[05:34] This allows you to migrate your code from using string interpolation to a more readable sprint f code with no performance penalty due to function call overhead. There are a few ways to automatically convert your code to use

[05:47] global namespace imports that I want to quickly show you. You'll find them in the blog post what are compiler optimized internal PHP functions uh in the middle somewhere. So the first one is the PHPCS fixer

[06:10] and you can run that one with um invocations. And if you do that

[06:29] watch project uh that I have you'll see it will append all the compiler optimized functions with a slash but not the ones that are not compiler optimized. The second uh option is to use PHP

[06:47] The second uh option is to use PHP uh code sniffer rule set reference used names only and then run PHPCBF it will make this

[07:00] change. This requires the sleomat coding standard

[07:15] this makes a few changes and to look at the file here we are using this rule set the file here we are using this rule set and we can look at the changes here. It does a little bit more. It also imports classes. Um, makes sure everything is

[07:27] classes. Um, makes sure everything is referenced there. And it seems that at least in the version I looked at here, it's not perfect because it only imports it's not perfect because it only imports the assert function, but it didn't

[07:39] import the the count function or string length functions. So, it um it does not do everything. And to my knowledge, previously it even referenced all functions from the global name space regardless of them being compiler

[07:54] optimized or not. So um the PHP code sniffer approach is a little bit less powerful than PHPCs fixer. The third option is and I only found

[08:06] that through the PHP Reddit um thread uh on on compiler optimized functions is to use PHP storm's um general autoimp import to

[08:18] don't prefer the fallback but prefer full qualified name. So in that case for full qualified name. So in that case for example if I have an assert call here

[08:31] I can write assert and if I autocomp complete it then it uses the um import automatically but um this one is done for all the but um this one is done for all the functions. So uh also the ones that are

[08:47] not compiler optimized. So should you import functions that the compiler can optimize for performance reasons? The answer is it depends. When the overhead is significant then yes. Otherwise you could ignore this as being too micro in

[09:02] optimization. Tideway profiler can automatically detect if you have code that would benefit from this optimization. And across all our optimization. And across all our customers this happens in maybe 2 to 5%

[09:14] of customers. For example, here in this example when 20k calls to in array cost 349 milliseconds or in this other case of a customer

[09:27] or in this other case of a customer where array key exists was called 9,550 times and it took like about 10 milliseconds to do that. For each of uh these cases, there's um compiler optimization available and if you import

[09:43] them, you will see some improvements. Obviously, these functions calls won't do go down to zero. So, it's not like they are completely free afterwards, but um you will see them take way less time than the uh the time measured in a

[10:00] profiler. If engine optimizations are interesting for you, I made a recent video on opc code specializations, a similar hidden gam in the engine. And if you're interested in PHP performance topics in general, please subscribe to

[10:15] our channel or our newsletter. The link is in the description. Bye.

More from Tideways

View all

⚡ Saved you 0h 10m reading this? Transcribe any YouTube video for free — no signup needed.