Home » Php » php – Doctrine 2 extreme optimization

php – Doctrine 2 extreme optimization

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m currently building a web application using Zend framework 1.11.11 and Doctrine 2.2. I searched a lot for the best ways and hacks to optimize the frameworks used.

During my search for Zend framework optimization I stumbled upon this post:
Optimising the Zend Framework

What was interesting about it is that it mentioned few optimization techniques that emerged from the usage of the framework itself and wasn’t covered in the manual itself such as:

  • disable viewRenderer and take care of view rendering yourself.

Now I’m looking for similar techniques that could be applied to Doctrine 2 other than the ones mentioned here in the manual.

Note

I know many will mention profiling the queries, caching and finding the bottle neck of the app, but that’s not um looking for 🙂 I am looking for practical enhancements discovered during Doctrine 2 usage.

Thanks 😉

How to&Answers:

Well, optimizing and hacking Doctrine 2 will not be an easy task. I can only advise you to follow the official “best practice”:

  • Use Query Cache
  • Use Metadata Cache
  • Pre-generate proxies
  • Avoid listeners (or merge them by event type (flush, update), it will avoid the lookup time and the loop for subscribed events)
  • Use lazy-loading whenever it is possible
  • Make sure your relationships or inheritance are not messed up

(Note that I didn’t mentioned Result cache which should not be a way to optimize an application)

From my use, the most important part I had to optimize was not Doctrine itself (while there are optimizations to do to the core) but the generated Query, as always, I EXPLAINed the queries and optimized indexes.

Doctrine 2 can be high memory consuming so loading a lot of entities at once may slow down your application, you may find it useful to known about clear(), detach(), iterate() methods.

Despite the fact Doctrine 2 can sometimes be slow, I mostly noticed that I was able to optimize application somewhere else, within the Zend Framework or PHP themselves.

Let’s say, Doctrine 2 takes 100ms where Zend Framework takes 300ms for a total of 450ms (I/O stuffs, PHP internal functions, etc..)

If you can divide easily by two the time taken by Zend Framework, optimizing Doctrine 2 to gain like 10% will not increase notably the speed of your application. Think about it twice.

Here are a few tips:

  • Create your own view instead of using View Helpers (avoid the helper lookup)
  • Cache your Zend_Config object (really heavy load)
  • Avoid Regex routes whenever possible (ZF routes are a big bottleneck)
  • Use a ClassMap autoloader instead of the native Zend_Loader_Autoloader

There are tons of optimization to do, some have a real impact while others don’t.

Make sure to find them by profiling your application, an easy and cross-platform is to use webgrind.