Skip to content

Commit 73bae4d

Browse files
Copilotsamdark
andauthored
Fix #51: Add PHP code micro-optimizations to the guide
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Alexander Makarov <[email protected]>
1 parent 2921bad commit 73bae4d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

guide/en/tutorial/performance-tuning.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ A well-configured PHP environment is important. To get maximum performance:
1616
- Make sure [XDebug](https://xdebug.org/) isn't installed in the production environment.
1717
- Try [PHP 7 preloading](https://wiki.php.net/rfc/preload).
1818

19+
## Optimizing your code <span id="optimizing-code"></span>
20+
21+
Beyond environment configuration, there are code-level optimizations that can improve your application's performance:
22+
23+
- Look out for [algorithm complexity](https://en.wikipedia.org/wiki/Time_complexity).
24+
Especially give attention to `foreach` within `foreach` loops but look out for using
25+
[heavy PHP functions](https://stackoverflow.com/questions/2473989/list-of-big-o-for-php-functions) in loops as well.
26+
- [Speeding up array_merge()](https://www.exakat.io/speeding-up-array_merge/)
27+
- [Move that foreach() inside the method](https://www.exakat.io/move-that-foreach-inside-the-method/)
28+
- [Array, classes and anonymous classes memory usage](https://www.exakat.io/array-classes-and-anonymous-memory-usage/)
29+
- Use fully qualified function names with leading backslashes to optimize opcache performance.
30+
When calling [certain global functions](https://github.com/php/php-src/blob/944b6b6bbd6f05ad905f5f4ad07445792bee4027/Zend/zend_compile.c#L4291-L4353)
31+
from within a namespace, PHP first searches in the current namespace before falling back to the global namespace.
32+
Adding a leading backslash (e.g., `\count()` instead of `count()`) tells PHP to directly use the global function,
33+
avoiding the namespace lookup and improving opcache efficiency. This optimization is best implemented automatically
34+
using tools like [PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) with the `native_function_invocation` rule.
35+
36+
The above optimizations would give you a significant performance boost only if the code in question is executed
37+
frequently. That is usually the case for big loops or batch processing.
38+
1939
## Using caching techniques <span id="using-caching-techniques"></span>
2040

2141
You can use various caching techniques to significantly improve the performance of your application. For example,

0 commit comments

Comments
 (0)