Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 4, 2025

This PR adds documentation about code-level optimizations for PHP applications, with a focus on opcache performance improvements using fully qualified function names with leading backslashes.

What was added

Added a new "Optimizing your code" section to the performance tuning guide that covers code-level optimizations separate from PHP environment configuration. The section explains:

  • The concept: When calling global functions from within a namespace (e.g., count(), array_merge()), PHP first searches in the current namespace before falling back to the global namespace
  • The optimization: Adding a leading backslash (\count() instead of count()) tells PHP to directly use the global function, avoiding the namespace lookup
  • The benefit: This improves opcache efficiency by eliminating unnecessary namespace resolution steps
  • Implementation: Recommends using automated tools like PHP-CS-Fixer with the native_function_invocation rule rather than manual implementation

Example

// Instead of this (requires namespace lookup):
namespace MyApp\Service;
$count = count($items);

// Use this (direct global function call):
namespace MyApp\Service;  
$count = \count($items);

This follows the preference expressed in the issue comments that such optimizations should be automated rather than manually maintained. The documentation properly separates environment-level PHP optimizations from code-level optimizations, maintaining consistency with the existing guide structure and formatting.

Fixes #51


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add to guide/en/tutorial/performance-tuning.md the concept of adding \ in namespaced to optimize PHP opcodes @yiisoft/docs/issues/51 . Mention that it is better to be done via automatically. Add PHP opcache optimization with leading backslashes to performance guide Sep 4, 2025
Copilot AI requested a review from samdark September 4, 2025 23:22
.gitignore Outdated
Comment on lines 19 to 22
*.mo

# Vale temporary styles
.github/styles/Microsoft/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot revert these changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the .gitignore changes in commit 4339289.

Copilot AI changed the title Add PHP opcache optimization with leading backslashes to performance guide Add PHP code optimization guide with opcache performance improvements Sep 4, 2025
Copilot AI requested a review from samdark September 4, 2025 23:43
@samdark samdark marked this pull request as ready for review September 5, 2025 07:22
@samdark samdark merged commit 73bae4d into master Sep 5, 2025
6 checks passed
@samdark samdark deleted the copilot/fix-3853ebf8-d545-4b46-a167-862a6d69e352 branch September 5, 2025 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve performance with leading backslash

2 participants