10.04.2018 Views

Doctrine_manual-1-2-en

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 28: Improving Performance 362<br />

Chapter 28<br />

Improving Performance<br />

Introduction<br />

Performance is a very important aspect of all medium to large sized applications. <strong>Doctrine</strong> is<br />

a large abstraction library that provides a database abstraction layer as well as objectrelational<br />

mapping. While this provides a lot of b<strong>en</strong>efits like portability and ease of<br />

developm<strong>en</strong>t it's inevitable that this leads to drawbacks in terms of performance. This chapter<br />

tries to help you to get the best performance out of <strong>Doctrine</strong>.<br />

Compile<br />

<strong>Doctrine</strong> is quite big framework and usually doz<strong>en</strong>s of files are being included on each<br />

request. This brings a lot of overhead. In fact these file operations are as time consuming as<br />

s<strong>en</strong>ding multiple queries to database server. The clean separation of class per file works well<br />

in developing <strong>en</strong>vironm<strong>en</strong>t, however wh<strong>en</strong> project goes commercial distribution the speed<br />

overcomes the clean separation of class per file -conv<strong>en</strong>tion.<br />

<strong>Doctrine</strong> offers method called compile() to solve this issue. The compile method makes a<br />

single file of most used <strong>Doctrine</strong> compon<strong>en</strong>ts which can th<strong>en</strong> be included on top of your<br />

script. By default the file is created into <strong>Doctrine</strong> root by the name<br />

<strong>Doctrine</strong>.compiled.php.<br />

Compiling is a method for making a single file of most used doctrine runtime compon<strong>en</strong>ts<br />

including the compiled file instead of multiple files (in worst cases doz<strong>en</strong>s of files) can<br />

improve performance by an order of magnitude. In cases where this might fail, a<br />

<strong>Doctrine</strong>_Exception is throw detailing the error.<br />

Lets create a compile script named compile.php to handle the compiling of <strong>Doctrine</strong>:<br />

Listing<br />

28-1<br />

// compile.php<br />

require_once('/path/to/doctrine/lib/<strong>Doctrine</strong>.php');<br />

spl_autoload_register(array('<strong>Doctrine</strong>', 'autoload'));<br />

<strong>Doctrine</strong>_Core::compile('<strong>Doctrine</strong>.compiled.php');<br />

Now we can execute compile.php and a file named <strong>Doctrine</strong>.compiled.php will be<br />

g<strong>en</strong>erated in the root of your doctrine_test folder:<br />

Listing<br />

28-2<br />

$ php compile.php<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!