@@ -6,39 +6,81 @@ Performance
6
6
7
7
Symfony is fast, right out of the box. Of course, if you really need speed,
8
8
there are many ways that you can make Symfony even faster. In this chapter,
9
- you'll explore many of the most common and powerful ways to make your Symfony
10
- application even faster.
9
+ you'll explore some of the ways to make your Symfony application even faster.
11
10
12
11
.. index ::
13
12
single: Performance; Byte code cache
14
13
15
- Use a Byte Code Cache (e.g. APC)
16
- --------------------------------
14
+ Use a Byte Code Cache (e.g. OPcache)
15
+ ------------------------------------
16
+
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.
17
20
18
- One of the best (and easiest) things that you should do to improve your performance
19
- is to use a "byte code cache". The idea of a byte code cache is to remove
20
- the need to constantly recompile the PHP source code. There are a number of
21
- `byte code caches `_ available, some of which are open source. As of PHP 5.5,
22
- PHP comes with `OPcache `_ built-in. For older versions, the most widely used
23
- byte code cache is probably `APC `_
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 `_.
24
24
25
25
Using a byte code cache really has no downside, and Symfony has been architected
26
26
to perform really well in this type of environment.
27
27
28
- Further Optimizations
29
- ~~~~~~~~~~~~~~~~~~~~~
28
+ Monitoring Source File Changes
29
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30
30
31
- Byte code caches usually monitor the source files for changes. This ensures
32
- that if the source of a file changes, the byte code is recompiled automatically.
33
- This is really convenient, but obviously adds overhead.
31
+ Most byte code caches monitor the source files for changes. This ensures that if
32
+ the source of a file changes, the byte code is recompiled automatically.
33
+ This is really convenient, but it adds overhead.
34
34
35
35
For this reason, some byte code caches offer an option to disable these checks.
36
- Obviously, when disabling these checks, it will be up to the server admin
37
- to ensure that the cache is cleared whenever any source files change. Otherwise,
38
- the updates you've made won't be seen.
36
+ For example, to disable these checks in APC, simply add ``apc.stat=0 `` to your
37
+ ``php.ini `` configuration.
38
+
39
+ When disabling these checks, it will be up to the server administrators to
40
+ ensure that the cache is cleared whenever any source files change. Otherwise,
41
+ the updates you've made in the application won't be seen.
42
+
43
+ For the same reasons, the byte code cache must also be cleared when deploying
44
+ the application (for example by calling ``apc_clear_cache() `` PHP function when
45
+ using APC and ``opcache_reset() `` when using OPcache).
46
+
47
+ .. note ::
48
+
49
+ In PHP, the CLI and the web processes don't share the same OPcache. This
50
+ means that you cannot clear the web server OPcache by executing some command
51
+ in your terminal. You either need to restart the web server or call to the
52
+ ``apc_clear_cache() `` and ``opcache_reset() `` functions via the web server.
53
+
54
+ Optimizing all the Files Used by Symfony
55
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56
+
57
+ By default, PHP's OPcache saves up to 2,000 files in the byte code cache. This
58
+ number is too low for the typical Symfony applications, so you should set a
59
+ higher limit with the `opcache.max_accelerated_files `_ configuration option:
39
60
40
- For example, to disable these checks in APC, simply add ``apc.stat=0 `` to
41
- your ``php.ini `` configuration.
61
+ .. code-block :: ini
62
+
63
+ ; php.ini
64
+ opcache.max_accelerated_files = 20000
65
+
66
+ Configure the PHP realpath Cache
67
+ --------------------------------
68
+
69
+ PHP uses an internal cache to store the result of mapping file paths to their
70
+ real and absolute file system paths. This increases the performance for
71
+ applications like Symfony that open many PHP files, specially on Windows
72
+ systems.
73
+
74
+ By default PHP sets a ``realpath_cache_size `` of ``16K `` which is too low for
75
+ Symfony. Consider updating this value at least to ``4096K ``. In addition, cached
76
+ paths are only stored for ``120 `` seconds by default. Consider updating this
77
+ value too using the ``realpath_cache_ttl `` option:
78
+
79
+ .. code-block :: ini
80
+
81
+ ; php.ini
82
+ realpath_cache_size =4096K
83
+ realpath_cache_ttl =600
42
84
43
85
.. index ::
44
86
single: Performance; Autoloader
@@ -55,15 +97,25 @@ Unfortunately, this comes at a cost, as the loader iterates over all configured
55
97
namespaces to find a particular file, making ``file_exists() `` calls until it
56
98
finally finds the file it's looking for.
57
99
58
- The simplest solution is to tell Composer to build a "class map" (i.e. a
59
- big array of the locations of all the classes). This can be done from the
60
- command line, and might become part of your deploy process:
100
+ The simplest solution is to tell Composer to build an optimized "class map",
101
+ which is a big array of the locations of all the classes and it's stored
102
+ in ``vendor/composer/autoload_classmap.php ``.
103
+
104
+ The class map can be generated from the command line, and might become part of
105
+ your deploy process:
61
106
62
- .. code-block :: terminal
107
+ .. code-block :: bash
63
108
64
- $ composer dump-autoload --optimize
109
+ $ composer dump-autoload --optimize --no-dev --classmap-authoritative
65
110
66
- Internally, this builds the big class map array in ``vendor/composer/autoload_classmap.php ``.
111
+ ``--optimize ``
112
+ Dumps every PSR-0 and PSR-4 compatible class used in your application.
113
+ ``--no-dev ``
114
+ Excludes the classes that are only needed in the development environment
115
+ (e.g. tests).
116
+ ``--classmap-authoritative ``
117
+ Prevents Composer from scanning the file system for classes that are not
118
+ found in the class map.
67
119
68
120
Caching the Autoloader with APC
69
121
-------------------------------
@@ -147,6 +199,7 @@ Learn more
147
199
148
200
.. _`byte code caches` : https://en.wikipedia.org/wiki/List_of_PHP_accelerators
149
201
.. _`OPcache` : http://php.net/manual/en/book.opcache.php
202
+ .. _`opcache.max_accelerated_files` : http://php.net/manual/en/opcache.configuration.php#ini.opcache.max-accelerated-files
150
203
.. _`APC` : http://php.net/manual/en/book.apc.php
151
204
.. _`autoload.php` : https://github.com/symfony/symfony-standard/blob/master/app/autoload.php
152
205
.. _`bootstrap file` : https://github.com/sensiolabs/SensioDistributionBundle/blob/master/Composer/ScriptHandler.php
0 commit comments