Skip to content

Commit 8f57789

Browse files
committed
Merge branch '3.3' into 3.4
* 3.3: Reword a code comment uploading example: explaining the need for md5 soap_web_service: code style in examples Updated the main performance article to make it more actionable Fix typos Added a note about using slashes and the special _format param
2 parents 05f22ac + cdc9070 commit 8f57789

File tree

5 files changed

+193
-94
lines changed

5 files changed

+193
-94
lines changed

controller/soap_web_service.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ In this case, the SOAP service will allow the client to call a method called
4040
{
4141

4242
$message = new \Swift_Message('Hello Service')
43-
->setTo('me@example.com')
44-
->setBody($name . ' says hi!');
43+
->setTo('me@example.com')
44+
->setBody($name.' says hi!');
4545

4646
$this->mailer->send($message);
4747

controller/upload_file.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ Finally, you need to update the code of the controller that handles the form::
132132
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
133133
$file = $product->getBrochure();
134134

135-
// Generate a unique name for the file before saving it
136-
$fileName = md5(uniqid()).'.'.$file->guessExtension();
135+
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
137136

138137
// Move the file to the directory where brochures are stored
139138
$file->move(
@@ -154,6 +153,16 @@ Finally, you need to update the code of the controller that handles the form::
154153
'form' => $form->createView(),
155154
));
156155
}
156+
157+
/**
158+
* @return string
159+
*/
160+
private function generateUniqueFileName()
161+
{
162+
// md5() reduces the similarity of the file names generated by
163+
// uniqid(), which is based on timestamps
164+
return md5(uniqid());
165+
}
157166
}
158167

159168
Now, create the ``brochures_directory`` parameter that was used in the

introduction/from_flat_php_to_symfony2.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ nice way to group related pages. The controller functions are also sometimes cal
580580
The two controllers (or actions) are still lightweight. Each uses the
581581
:doc:`Doctrine ORM library </doctrine>` to retrieve objects from the
582582
database and the Templating component to render a template and return a
583-
``Response`` object. The list ``list.php`` template is now quite a bit simpler:
583+
``Response`` object. The ``list.php`` template is now quite a bit simpler:
584584

585585
.. code-block:: html+php
586586

@@ -623,7 +623,7 @@ The ``layout.php`` file is nearly identical:
623623

624624
.. note::
625625

626-
The show ``show.php`` template is left as an exercise: updating it should be
626+
The ``show.php`` template is left as an exercise: updating it should be
627627
really similar to updating the ``list.php`` template.
628628

629629
When Symfony's engine (called the Kernel) boots up, it needs a map so
@@ -673,10 +673,10 @@ It's a beautiful thing.
673673
Better Templates
674674
~~~~~~~~~~~~~~~~
675675

676-
If you choose to use it, Symfony comes standard with a templating engine
676+
If you choose to use it, Symfony comes with a templating engine
677677
called `Twig`_ that makes templates faster to write and easier to read.
678-
It means that the sample application could contain even less code! Take,
679-
for example, rewriting ``list.html.php`` template in Twig would look like
678+
It means that the sample application could contain even less code! For
679+
example, rewriting the ``list.html.php`` template in Twig would look like
680680
this:
681681

682682
.. code-block:: html+twig
@@ -699,7 +699,7 @@ this:
699699
</ul>
700700
{% endblock %}
701701

702-
And rewriting ``layout.html.php`` template in Twig would look like this:
702+
And rewriting the ``layout.html.php`` template in Twig would look like this:
703703

704704
.. code-block:: html+twig
705705

@@ -721,9 +721,9 @@ be discussed. For more information, see the :doc:`templating article </templatin
721721
Where Symfony Delivers
722722
----------------------
723723

724-
In the rest of documentation articles, you'll learn more about how each piece of
725-
Symfony works and how you can organize your project. For now, celebrate at how
726-
migrating the blog from flat PHP to Symfony has improved life:
724+
In the rest of the documentation articles, you'll learn more about how each piece of
725+
Symfony works and how you can organize your project. For now, celebrate how
726+
migrating the blog from flat PHP to Symfony has improved your life:
727727

728728
* Your application now has **clear and consistently organized code** (though
729729
Symfony doesn't force you into this). This promotes **reusability** and

performance.rst

Lines changed: 162 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,210 @@
11
.. index::
2-
single: Tests
2+
single: Performance; Byte code cache; OPcache; APC
33

44
Performance
55
===========
66

7-
Symfony is fast, right out of the box. Of course, if you really need speed,
8-
there are many ways that you can make Symfony even faster. In this article,
9-
you'll explore some of the ways to make your Symfony application even faster.
7+
Symfony is fast, right out of the box. However, you can make it faster if you
8+
optimize your servers and your applications as explained in the following
9+
performance checklists.
1010

11-
.. index::
12-
single: Performance; Byte code cache
11+
Symfony Application Checklist
12+
-----------------------------
13+
14+
#. :ref:`Install APCu Polyfill if your server uses APC <performance-install-apcu-polyfill>`
15+
#. :ref:`Enable APC Caching for the Autoloader <performance-autoloader-apc-cache>`
16+
#. :ref:`Use Bootstrap Files <performance-use-bootstrap-files>`
17+
18+
Production Server Checklist
19+
---------------------------
20+
21+
#. :ref:`Use the OPcache byte code cache <performance-use-opcache>`
22+
#. :ref:`Configure OPcache for maximum performance <performance-configure-opcache>`
23+
#. :ref:`Don't check PHP files timestamps <performance-dont-check-timestamps>`
24+
#. :ref:`Configure the PHP realpath Cache <performance-configure-realpath-cache>`
25+
#. :ref:`Optimize Composer Autoloader <performance-optimize-composer-autoloader>`
26+
27+
.. _performance-install-apcu-polyfill:
28+
29+
Install APCu Polyfill if your Server Uses APC
30+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31+
32+
If your production server still uses the legacy APC PHP extension instead of
33+
OPcache, install the `APCu Polyfill component`_ in your application to enable
34+
compatibility with `APCu PHP functions`_ and unlock support for advanced Symfony
35+
features, such as the APCu Cache adapter.
36+
37+
.. _performance-autoloader-apc-cache:
38+
39+
Enable APC Caching for the Autoloader
40+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41+
42+
The class autoloading mechanism is one of the slowest parts in PHP applications
43+
that make use of lots of classes, such as Symfony. A simple way to improve its
44+
performance is to use the :class:`Symfony\\Component\\ClassLoader\\ApcClassLoader`,
45+
which caches the location of each class after it's located the first time.
46+
47+
To use it, adapt your front controller file like this::
48+
49+
// app.php
50+
// ...
51+
52+
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
53+
54+
// Change 'sf' by something unique to this app to prevent
55+
// conflicts with other applications running in the same server
56+
$loader = new ApcClassLoader('sf', $loader);
57+
$loader->register(true);
58+
59+
// ...
60+
61+
For more details, see :doc:`/components/class_loader/cache_class_loader`.
62+
63+
.. note::
64+
65+
When using the APC autoloader, if you add new classes, they will be found
66+
automatically and everything will work the same as before (i.e. no
67+
reason to "clear" the cache). However, if you change the location of a
68+
particular namespace or prefix, you'll need to flush your APC cache. Otherwise,
69+
the autoloader will still be looking at the old location for all classes
70+
inside that namespace.
1371

14-
Use a Byte Code Cache (e.g. OPcache)
15-
------------------------------------
72+
.. _performance-use-bootstrap-files:
1673

17-
The first thing that you should do to improve your performance is to use a
18-
"byte code cache". These caches store the compiled PHP files to avoid having
19-
to recompile them for every request.
74+
Use Bootstrap Files
75+
~~~~~~~~~~~~~~~~~~~
2076

21-
There are a number of `byte code caches`_ available, some of which are open
22-
source. As of PHP 5.5, PHP comes with `OPcache`_ built-in. For older versions,
23-
the most widely used byte code cache is `APC`_.
77+
.. caution::
2478

25-
.. tip::
79+
Thanks to the optimizations introduced in PHP 7, bootstrap files are no
80+
longer necessary when running your Symfony applications with PHP 7 or a
81+
newer PHP version.
2682

27-
If your server still uses the legacy APC PHP extension, install the
28-
`APCu Polyfill component`_ in your application to enable compatibility with
29-
`APCu PHP functions`_ and unlock support for advanced Symfony features, such
30-
as the APCu Cache adapter.
83+
The Symfony Standard Edition includes a script to generate a so-called
84+
`bootstrap file`_, which is a large file containing the code of the most
85+
commonly used classes. This saves a lot of IO operations because Symfony no
86+
longer needs to look for and read those files.
3187

32-
Using a byte code cache really has no downside, and Symfony has been designed
33-
to perform really well in this type of environment.
88+
If you're using the Symfony Standard Edition, then you're probably already
89+
using the bootstrap file. To be sure, open your front controller (usually
90+
``app.php``) and check to make sure that the following line exists::
3491

35-
Monitoring Source File Changes
36-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92+
require_once __DIR__.'/../app/bootstrap.php.cache';
3793

38-
Most byte code caches monitor the source files for changes. This ensures that if
39-
the source of a file changes, the byte code is recompiled automatically.
40-
This is really convenient, but it adds overhead.
94+
Note that there are two disadvantages when using a bootstrap file:
4195

42-
For this reason, some byte code caches offer an option to disable these checks.
43-
For example, to disable these checks in APC, simply add ``apc.stat=0`` to your
44-
``php.ini`` configuration.
96+
* the file needs to be regenerated whenever any of the original sources change
97+
(i.e. when you update the Symfony source or vendor libraries);
4598

46-
When disabling these checks, it will be up to the server administrators to
47-
ensure that the cache is cleared whenever any source files change. Otherwise,
48-
the updates you've made in the application won't be seen.
99+
* when debugging, one will need to place break points inside the bootstrap file.
49100

50-
For the same reasons, the byte code cache must also be cleared when deploying
51-
the application (for example by calling ``apc_clear_cache()`` PHP function when
52-
using APC and ``opcache_reset()`` when using OPcache).
101+
If you're using the Symfony Standard Edition, the bootstrap file is automatically
102+
rebuilt after updating the vendor libraries via the ``composer install`` command.
53103

54104
.. note::
55105

56-
In PHP, the CLI and the web processes don't share the same OPcache. This
57-
means that you cannot clear the web server OPcache by executing some command
58-
in your terminal. These are some of the possible solutions:
106+
Even when using a byte code cache, performance will improve when using a
107+
bootstrap file since there will be fewer files to monitor for changes. Of
108+
course, if this feature is disabled in the byte code cache (e.g.
109+
``apc.stat=0`` in APC), there is no longer a reason to use a bootstrap file.
59110

60-
#. Restart the web server;
61-
#. Call the :phpfunction:`apc_clear_cache` or :phpfunction:`opcache_reset`
62-
functions via the web server (i.e. by having these in a script that
63-
you execute over the web);
64-
#. Use the `cachetool`_ utility to control APC and OPcache from the CLI.
111+
.. _performance-use-opcache:
65112

66-
Optimizing all the Files Used by Symfony
67-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113+
Use the OPcache Byte Code Cache
114+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68115

69-
By default, PHP's OPcache saves up to 2,000 files in the byte code cache. This
70-
number is too low for the typical Symfony application, so you should set a
71-
higher limit with the `opcache.max_accelerated_files`_ configuration option:
116+
OPcache stores the compiled PHP files to avoid having to recompile them for
117+
every request. There are some `byte code caches`_ available, but as of PHP
118+
5.5, PHP comes with `OPcache`_ built-in. For older versions, the most widely
119+
used byte code cache is `APC`_.
120+
121+
.. _performance-configure-opcache:
122+
123+
Configure OPcache for Maximum Performance
124+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125+
126+
The default OPcache configuration is not suited for Symfony applications, so
127+
it's recommended to change these settings as follows:
72128

73129
.. code-block:: ini
74130
75131
; php.ini
76-
opcache.max_accelerated_files = 20000
132+
; maximum memory that OPcache can use to store compiled PHP files
133+
opcache.memory_consumption=256M
77134
78-
Configure the PHP realpath Cache
79-
--------------------------------
135+
; maximum number of files that can be stored in the cache
136+
opcache.max_accelerated_files=20000
137+
138+
.. _performance-dont-check-timestamps:
80139

81-
PHP uses an internal cache to store the result of mapping file paths to their
82-
real and absolute file system paths. This increases the performance for
83-
applications like Symfony that open many PHP files, especially on Windows
84-
systems.
140+
Don't Check PHP Files Timestamps
141+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85142

86-
Consider increasing the ``realpath_cache_size`` and ``realpath_cache_ttl``:
143+
In production servers, PHP files should never change, unless a new application
144+
version is deployed. However, by default OPcache checks if cached files have
145+
changed their contents since they were cached. This check introduces some
146+
overhead that can be avoided as follows:
87147

88148
.. code-block:: ini
89149
90150
; php.ini
91-
; 4096k is the default value in PHP 7.2
92-
realpath_cache_size=4096K
93-
realpath_cache_ttl=600
151+
opcache.validate_timestamps=0
94152
95-
.. index::
96-
single: Performance; Autoloader
153+
After each deploy, you must empty and regenerate the cache of OPcache. Otherwise
154+
you won't see the updates made in the application. Given than in PHP, the CLI
155+
and the web processes don't share the same OPcache, you cannot clear the web
156+
server OPcache by executing some command in your terminal. These are some of the
157+
possible solutions:
158+
159+
1. Restart the web server;
160+
2. Call the ``apc_clear_cache()`` or ``opcache_reset()`` functions via the
161+
web server (i.e. by having these in a script that you execute over the web);
162+
3. Use the `cachetool`_ utility to control APC and OPcache from the CLI.
163+
164+
.. _performance-configure-realpath-cache:
97165

98-
Use Composer's Class Map Functionality
99-
--------------------------------------
166+
Configure the PHP realpath Cache
167+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168+
169+
When a relative path is transformed into its real and absolute path, PHP
170+
caches the result to improve performance. The default config of this cache
171+
is not suited for applications that open many PHP files, such as Symfony.
172+
It's recommended to change these settings as follows:
173+
174+
.. code-block:: ini
175+
176+
; php.ini
177+
; maximum memory allocated to store the results
178+
realpath_cache_size=4096K
179+
180+
; save the results for 10 minutes (600 seconds)
181+
realpath_cache_ttl=600
100182
101-
Symfony uses Composer's autoloader. This autoloader is easy to use, as it will
102-
automatically find any new classes that you've placed in the registered
103-
directories.
183+
.. _performance-optimize-composer-autoloader:
104184

105-
Unfortunately, this comes at a cost, as the loader iterates over all configured
106-
namespaces to find a particular file, making ``file_exists()`` calls until it
107-
finally finds the file it's looking for.
185+
Optimize Composer Autoloader
186+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108187

109-
The simplest solution is to tell Composer to build an optimized "class map",
188+
The class loader used while developing the application is optimized to find
189+
new and changed classes. In production servers, PHP files should never change,
190+
unless a new application version is deployed. That's why you can optimize
191+
Composer's autoloader to scan the entire application once and build a "class map",
110192
which is a big array of the locations of all the classes and it's stored
111193
in ``vendor/composer/autoload_classmap.php``.
112194

113-
The class map can be generated from the command line, and might become part of
114-
your deploy process:
195+
Execute this command to generate the class map (and make it part of your
196+
deployment process too):
115197

116198
.. code-block:: bash
117199
118200
$ composer dump-autoload --optimize --no-dev --classmap-authoritative
119201
120-
``--optimize``
121-
Dumps every PSR-0 and PSR-4 compatible class used in your application.
122-
``--no-dev``
123-
Excludes the classes that are only needed in the development environment
124-
(e.g. tests).
125-
``--classmap-authoritative``
126-
Prevents Composer from scanning the file system for classes that are not
127-
found in the class map.
202+
* ``--optimize`` dumps every PSR-0 and PSR-4 compatible class used in your
203+
application;
204+
* ``--no-dev`` excludes the classes that are only needed in the development
205+
environment (e.g. tests);
206+
* ``--classmap-authoritative`` prevents Composer from scanning the file
207+
system for classes that are not found in the class map.
128208

129209
Learn more
130210
----------
@@ -134,7 +214,8 @@ Learn more
134214

135215
.. _`byte code caches`: https://en.wikipedia.org/wiki/List_of_PHP_accelerators
136216
.. _`OPcache`: http://php.net/manual/en/book.opcache.php
137-
.. _`opcache.max_accelerated_files`: http://php.net/manual/en/opcache.configuration.php#ini.opcache.max-accelerated-files
217+
.. _`bootstrap file`: https://github.com/sensiolabs/SensioDistributionBundle/blob/master/Composer/ScriptHandler.php
218+
.. _`Composer's autoloader optimization`: https://getcomposer.org/doc/articles/autoloader-optimization.md
138219
.. _`APC`: http://php.net/manual/en/book.apc.php
139220
.. _`APCu Polyfill component`: https://github.com/symfony/polyfill-apcu
140221
.. _`APCu PHP functions`: http://php.net/manual/en/ref.apcu.php

0 commit comments

Comments
 (0)