Skip to content

Commit 123515c

Browse files
committed
[book][performance] Bootstrapping the performance chapter from existing cookbook entries
This chapter should cover many other topics as well, it will need to be filled out
1 parent 57ff6d6 commit 123515c

File tree

9 files changed

+140
-99
lines changed

9 files changed

+140
-99
lines changed

book/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Book
1919
http_cache
2020
translation
2121
service_container
22+
performance
2223
internals
2324
stable_api
2425

book/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
* :doc:`/book/http_cache`
1414
* :doc:`/book/translation`
1515
* :doc:`/book/service_container`
16+
* :doc:`/book/performance`
1617
* :doc:`/book/internals`
1718
* :doc:`/book/stable_api`

book/performance.rst

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
.. index::
2+
single: Tests
3+
4+
Performance
5+
===========
6+
7+
Symfony2 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 chapter,
9+
you'll explore many of the most common and powerful ways to make your Symfony
10+
application even faster.
11+
12+
.. index::
13+
single: Performance; Byte code cache
14+
15+
Use a Byte Code Cache (e.g. APC)
16+
--------------------------------
17+
18+
One 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. The most widely
22+
used byte code cache is probably `APC`_
23+
24+
Using a byte code cache really has no downside, and Symfony2 has been architected
25+
to perform really well in this type of environment.
26+
27+
Further Optimizations
28+
~~~~~~~~~~~~~~~~~~~~~
29+
30+
Byte code caches usually monitor the source files for changes. This ensures
31+
that if the source of a file changes, the byte code is recompiled automatically.
32+
This is really convenient, but obviously adds overhead.
33+
34+
For this reason, some byte code caches offer an option to disable these checks.
35+
Obviously, when disabling these checks, it will be up to the server admin
36+
to ensure that the cache is cleared whenever any source files change. Otherwise,
37+
the updates you've made won't be seen.
38+
39+
For example, to disable these checks in APC, simply add ``apc.stat=0`` to
40+
your php.ini configuration.
41+
42+
.. index::
43+
single: Performance; Autoloader
44+
45+
Use an Autoloader that caches (e.g. ``ApcUniversalClassLoader``)
46+
----------------------------------------------------------------
47+
48+
By default, the Symfony2 standard edition uses the ``UniversalClassLoader``
49+
in the `autoloader.php`_ file. This autoloader is easy to use, as it will
50+
automatically find any new classes that you've placed in the registered
51+
directories.
52+
53+
Unfortunately, this comes at a cost, as the loader iterates over all configured
54+
namespaces to find a particular file, making ``file_exists`` calls until it
55+
finally finds the file it's looking for.
56+
57+
The simplest solution is to cache the location of each class after it's located
58+
the first time. Symfony comes with a class - ``ApcUniversalClassLoader`` -
59+
loader that extends the ``UniversalClassLoader`` and stores the class locations
60+
in APC.
61+
62+
To use this class loader, simply adapt your ``autoloader.php`` as follows:
63+
64+
.. code-block:: php
65+
66+
// app/autoload.php
67+
require __DIR__.'/../vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
68+
require __DIR__.'/../vendor/symfony/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php';
69+
70+
use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
71+
72+
$loader = new ApcUniversalClassLoader('some caching unique prefix');
73+
// ...
74+
75+
.. note::
76+
77+
When using the APC autoloader, if you add new classes, they will be found
78+
and automatically and everything will work the same as before (i.e. no
79+
reason to "clear" the cach). However, if you change the location of a
80+
particular namespace or prefix, you'll need to flush your APC cache. Otherwise,
81+
the autoloader will still be looking at the old location for all classes
82+
inside that namespace.
83+
84+
.. index::
85+
single: Performance; Bootstrap files
86+
87+
Use Bootstrap Files
88+
-------------------
89+
90+
To ensure optimal flexibility and code reuse, Symfony2 applications leverage
91+
a variety of classes and 3rd party components. But loading all of these classes
92+
from separate files on each request can result in some overhead. To reduce
93+
this overhead, the Symfony2 Standard Edition provides a script to generate
94+
a so-called `bootstrap file`_, consisting of multiple classes definitions
95+
in a single file. By including this file (which contains a copy of many of
96+
the core classes), Symfony no longer needs to include any of the source files
97+
containing those classes. This will reduce disc IO quite a bit.
98+
99+
If you're using the Symfony2 Standard Edition, then you're probably already
100+
using the bootstrap file. To be sure, open your front controller (usually
101+
``app.php``) and check to make sure that one of the following lines exists
102+
and is uncommented (exactly which you need depends on if you're using Symfony's
103+
:doc:`HTTP Caching layer</book/http_cache>`)::
104+
105+
require_once __DIR__.'/../app/bootstrap.php.cache';
106+
require_once __DIR__.'/../app/bootstrap_cache.php.cache';
107+
108+
Note that there are two disadvantages when using a bootstrap file:
109+
110+
* the file needs to be regenerated whenever any of the original sources change
111+
(i.e. when you update the Symfony2 source or vendor libraries);
112+
113+
* when debugging, one will need to place break points inside the bootstrap file.
114+
115+
If you're using Symfony2 Standard Edition, the bootstrap files are automatically
116+
rebuilt after updating the vendor libraries via the ``php bin/vendors install``
117+
command.
118+
119+
Bootstrap Files and Byte Code Caches
120+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
121+
122+
Even when using a byte code cache, performance will improve when using a bootstrap
123+
file since there will be less files to monitor for changes. Of course if this
124+
feature is disabled in the byte code cache (e.g. ``apc.stat=0`` in APC), there
125+
is no longer a reason to use a bootstrap file.
126+
127+
.. _`byte code caches`: http://en.wikipedia.org/wiki/List_of_PHP_accelerators
128+
.. _`APC`: http://php.net/manual/en/book.apc.php
129+
.. _`autoloader.php`: https://github.com/symfony/symfony-standard/blob/master/app/autoload.php
130+
.. _`bootstrap file`: https://github.com/sensio/SensioDistributionBundle/blob/master/Resources/bin/build_bootstrap.php

cookbook/debugging.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.. index::
2+
single: Debugging
3+
14
How to optimize your development Environment for debugging
25
==========================================================
36

@@ -11,6 +14,11 @@ configuration is optimized for two main purposes:
1114
* Be as similar as possible as the production environment to avoid problems
1215
when deploying the project.
1316

17+
.. _cookbook-debugging-disable-bootstrap:
18+
19+
Disabling the Bootstrap File and Class Caching
20+
----------------------------------------------
21+
1422
And to make the production environment as fast as possible, Symfony creates
1523
big PHP files in your cache containing the aggregation of PHP classes your
1624
project needs for every request. However, this behavior can confuse your IDE

cookbook/index.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ Cookbook
7171
debugging
7272
logging/monolog
7373

74-
performance/autoloader
75-
performance/bootstrap_files
76-
performance/bytecodecache
77-
7874
event_dispatcher/class_extension
7975
event_dispatcher/method_behavior
8076
request/mime_type

cookbook/map.rst.inc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@
9292
* :doc:`/cookbook/debugging`
9393
* :doc:`/cookbook/logging/monolog`
9494

95-
* **Performance**
96-
97-
* :doc:`/cookbook/performance/autoloader`
98-
* :doc:`/cookbook/performance/bootstrap_files`
99-
* :doc:`/cookbook/performance/bytecodecache`
100-
10195
* **Extending Symfony**
10296

10397
* :doc:`/cookbook/event_dispatcher/class_extension`

cookbook/performance/autoloader.rst

Lines changed: 0 additions & 32 deletions
This file was deleted.

cookbook/performance/bootstrap_files.rst

Lines changed: 0 additions & 29 deletions
This file was deleted.

cookbook/performance/bytecodecache.rst

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)