Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Updates in performance.rst #8410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
Cleaned previous merge
  • Loading branch information
Erdou committed Sep 20, 2017
commit fc069bcf33723a60bcc1dcc9f21246ab22bf248a
230 changes: 52 additions & 178 deletions 230 performance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,172 +8,7 @@ Symfony is fast, right out of the box. Of course, if you really need speed, the
following performance checklists show how to make your application even faster.

.. index::
single: Performance; Byte code cache

Symfony Application Checklist
-----------------------------

#. :ref:`Install APCu Polyfill if your server uses APC <performance-install-apcu-polyfill>`
#. :ref:`Enable APCu Caching for the Autoloader <performance-autoloader-apcu-cache>`
#. :ref:`Use Bootstrap Files <performance-use-bootstrap-files>`

.. _performance-install-apcu-polyfill:

Install APCu Polyfill if your server uses APC
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. tip::

If your production server still uses the legacy APC PHP extension instead of
OPcache, install the `APCu Polyfill component`_ in your application to enable
compatibility with `APCu PHP functions`_ and unlock support for advanced Symfony
features, such as the APCu Cache adapter.

.. _performance-autoloader-apc-cache:

Monitoring Source File Changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Most byte code caches monitor the source files for changes. This ensures that if
the source of a file changes, the byte code is recompiled automatically.
This is really convenient, but it adds overhead.

For this reason, some byte code caches offer an option to disable these checks.
For example, to disable these checks in APC, simply add ``apc.stat=0`` to your
``php.ini`` configuration.

When disabling these checks, it will be up to the server administrators to
ensure that the cache is cleared whenever any source files change. Otherwise,
the updates you've made in the application won't be seen.

For the same reasons, the byte code cache must also be cleared when deploying
the application (for example by calling ``apc_clear_cache()`` PHP function when
using APC and ``opcache_reset()`` when using OPcache).

.. note::

In PHP, the CLI and the web processes don't share the same OPcache. This
means that you cannot clear the web server OPcache by executing some command
in your terminal. You either need to restart the web server or call the
``apc_clear_cache()`` or ``opcache_reset()`` functions via the web server
(i.e. by having these in a script that you execute over the web).

Optimizing all the Files Used by Symfony
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

By default, PHP's OPcache saves up to 2,000 files in the byte code cache. This
number is too low for the typical Symfony application, so you should set a
higher limit with the `opcache.max_accelerated_files`_ configuration option:

.. code-block:: ini

; php.ini
opcache.max_accelerated_files = 20000

Configure the PHP realpath Cache
--------------------------------

PHP uses an internal cache to store the result of mapping file paths to their
real and absolute file system paths. This increases the performance for
applications like Symfony that open many PHP files, especially on Windows
systems.

By default, PHP sets a ``realpath_cache_size`` of ``16K`` which is too low for
Symfony. Consider updating this value at least to ``4096K``. In addition, cached
paths are only stored for ``120`` seconds by default. Consider updating this
value too using the ``realpath_cache_ttl`` option:

.. code-block:: ini

; php.ini
realpath_cache_size=4096K
realpath_cache_ttl=600

.. index::
single: Performance; Autoloader

Use Composer's Class Map Functionality
--------------------------------------

By default, the Symfony Standard Edition uses Composer's autoloader
in the `autoload.php`_ file. This autoloader is easy to use, as it will
automatically find any new classes that you've placed in the registered
directories.

Unfortunately, this comes at a cost, as the loader iterates over all configured
namespaces to find a particular file, making ``file_exists()`` calls until it
finally finds the file it's looking for.

The simplest solution is to tell Composer to build an optimized "class map",
which is a big array of the locations of all the classes and it's stored
in ``vendor/composer/autoload_classmap.php``.

The class map can be generated from the command line, and might become part of
your deploy process:

.. code-block:: bash

$ composer dump-autoload --optimize --no-dev --classmap-authoritative

``--optimize``
Dumps every PSR-0 and PSR-4 compatible class used in your application.
``--no-dev``
Excludes the classes that are only needed in the development environment
(e.g. tests).
``--classmap-authoritative``
Prevents Composer from scanning the file system for classes that are not
found in the class map.

Caching the Autoloader with APCu
--------------------------------

Another solution is to cache the location of each class after it's located
the first time: use `composer install --apcu-autoloader`.

.. note::

When using the APCu autoloader, if you add new classes, they will be found
automatically and everything will work the same as before (i.e. no
reason to "clear" the cache). However, if you change the location of a
particular namespace or prefix, you'll need to flush your APCu cache. Otherwise,
the autoloader will still be looking at the old location for all classes
inside that namespace.

.. index::
single: Performance; Bootstrap files

.. _performance-use-bootstrap-files:

Use Bootstrap Files
~~~~~~~~~~~~~~~~~~~

The Symfony Standard Edition includes a script to generate a so-called
`bootstrap file`_, which is a large file containing the code of the most
commonly used classes. This saves a lot of IO operations because Symfony no
longer needs to look for and read those files.

If you're using the Symfony Standard Edition, then you're probably already
using the bootstrap file. To be sure, open your front controller (usually
``app.php``) and check to make sure that the following line exists::

require_once __DIR__.'/../app/bootstrap.php.cache';

Note that there are two disadvantages when using a bootstrap file:

* the file needs to be regenerated whenever any of the original sources change
(i.e. when you update the Symfony source or vendor libraries);

* when debugging, one will need to place break points inside the bootstrap file.

If you're using the Symfony Standard Edition, the bootstrap file is automatically
rebuilt after updating the vendor libraries via the ``composer install`` command.

.. note::

Even when using a byte code cache, performance will improve when using a
bootstrap file since there will be fewer files to monitor for changes. Of
course, if this feature is disabled in the byte code cache (e.g.
``apc.stat=0`` in APC), there is no longer a reason to use a bootstrap file.
single: Performance; Byte code cache; APCu

Production Server Checklist
---------------------------
Expand All @@ -184,8 +19,6 @@ Production Server Checklist
#. :ref:`Configure the PHP realpath Cache <performance-configure-realpath-cache>`
#. :ref:`Optimize Composer Autoloader <performance-optimize-composer-autoloader>`

.. index::
single: Performance; Byte code cache

.. _performance-use-opcache:

Expand Down Expand Up @@ -260,11 +93,31 @@ It's recommended to change these settings as follows:
; save the results for 10 minutes (600 seconds)
realpath_cache_ttl=600

.. index::
single: Performance; Autoloader

.. _performance-optimize-composer-autoloader:

Configure the PHP realpath Cache
--------------------------------

PHP uses an internal cache to store the result of mapping file paths to their
real and absolute file system paths. This increases the performance for
applications like Symfony that open many PHP files, especially on Windows
systems.

By default, PHP sets a ``realpath_cache_size`` of ``16K`` which is too low for
Symfony. Consider updating this value at least to ``4096K``. In addition, cached
paths are only stored for ``120`` seconds by default. Consider updating this
value too using the ``realpath_cache_ttl`` option:

.. code-block:: ini

; php.ini
realpath_cache_size=4096K
realpath_cache_ttl=600

.. index::
single: Performance; Autoloader

Optimize Composer Autoloader
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -280,14 +133,36 @@ deployment process too):

.. code-block:: bash

$ composer dump-autoload --optimize --no-dev --classmap-authoritative
$ composer install --no-dev --optimize --classmap-authoritative --apcu

* ``--optimize`` dumps every PSR-0 and PSR-4 compatible class used in your
application;
* ``--no-dev`` excludes the classes that are only needed in the development
environment (e.g. tests);
* ``--classmap-authoritative`` prevents Composer from scanning the file
system for classes that are not found in the class map.
``--no-dev``
Excludes the classes that are only needed in the development environment
(e.g. tests).
``--optimize``
Dumps every PSR-0 and PSR-4 compatible class used in your application.
``--classmap-authoritative``
Prevents Composer from scanning the file system for classes that are not
found in the class map.
``--apcu``
You need to install APCu PHP extension to use this option.
It will cache the classmap in APCu. It won't generate the classmap though,
so you need to always use it with ``--optimize-autoloader``

.. tip::

If your production server still uses the legacy APC PHP extension instead of
OPcache, install the `APCu Polyfill component`_ in your application to enable
compatibility with `APCu PHP functions`_ and unlock support for advanced Symfony
features, such as the APCu Cache adapter.

.. note::

When using the APCu autoloader, if you add new classes, they will be found
automatically and everything will work the same as before (i.e. no
reason to "clear" the cache). However, if you change the location of a
particular namespace or prefix, you'll need to flush your APCu cache. Otherwise,
the autoloader will still be looking at the old location for all classes
inside that namespace.

Learn more
----------
Expand All @@ -300,4 +175,3 @@ Learn more
.. _`APC`: http://php.net/manual/en/book.apc.php
.. _`APCu Polyfill component`: https://github.com/symfony/polyfill-apcu
.. _`APCu PHP functions`: http://php.net/manual/en/ref.apcu.php
.. _`bootstrap file`: https://github.com/sensiolabs/SensioDistributionBundle/blob/master/Composer/ScriptHandler.php
Morty Proxy This is a proxified and sanitized view of the page, visit original site.