views:

93

answers:

2

Hello,

My application based on Zend Framework and Doctrine includes > 300 files on each request. They are mostly the same files.

This is a quite huge overhead. Partially solved by Zend_Cache (and Memcache), but not all the pages may be cached.

  • How to reduce this number? How to speed up?

Doctrine has an option to compile the needed files which seems quite rational for production server and final version of the app.

My plan is to compile other libraries too (I have already stripped all require_once's).

  • Are there any tools for this task? Maybe some cache drivers do it automatically? How to set them up?
+2  A: 

The overhead of php file inclusions can usually be countered with an opcode cache such as APC, an extension available through pecl. Opcode caches generally work by caching the compiled bytecode so that the overhead of reading and parsing the source is only incurred on the first request. This will greatly negate the need or benefit of any source compilation on your php files.

Kevin
I have APC extension installed. Is there any smart way to replace file caches set up in application.ini with APC cache?
takeshin
@takshin APC will automatically cache opcodes. You can turn stat off (in production), so it won't even check if the file has been updated on the filesystem. If you want to cache application-data, you can use APC for that as well. I believe Zend_Cache has a back-end for APC which facilitates exactly that.
timdev
300 files is really nothing when used with APC. They will be served from shared memory and no problem occours.
beberlei
@beberlei So why Doctrine has compile option?
takeshin
My guess is its mostly outdated thinking left over from before the use of opcode caches had become so prevalent. The forthcoming Doctrine 2, which should be out of beta in the next few months, does not have the compile option. In fact, where the compile option used to be shown in the documentation there is now a recommendation to use an opcode cache.
Kevin
+1  A: 

Best option is to use APC or Zend_Accelerator. But still you can make these "compilation" scripts that merge classes together into one file. That lowers the required IO to minimum. Unfortunately, you also need to rewrite the autoloading process so that it looks into appropriate file. You can usually condense common classes together (Zend_Form + Elements + Decorators, frequently used validators, Request + Response + Router + Controller, Zend_Db + adapters + Zend_Db_Select, etc.). Mainly the classes always used on each request can be easily condensed and included manually in one file. Best way is to add debug call, that save all included files (http://www.php.net/get_included_files) into DB and then:

SELECT * FROM files GROUP BY filename WHERE COUNT(filename) = $numOfRequests

All the files in the result can be safely merged into a single file and included before bootstraping :)

Tomáš Fejfar
I wrote a script to gather all the includes, but the problem is that the files depend each other. How to determine the right order?
takeshin
Trial/Error or fixing errors one by one. General idea is having interfaces first, then abstract classes and others at the end. Many dependencies are in-code that are not needed before the methods are called.
Tomáš Fejfar