20

Does APC module in PHP when running in CLI mode support code optimization? For example, when I run a file with php -f <file> will the file be optimized with APC before executing or not? Presuming APC is set to load in config file. Also, will the scripts included with require_once be also optimized?

I know optimization works fine when running in fastcgi mode, but I'm wondering if it also works in CLI.

apc_* functions work, but I'm wondering about the code optimization, which is the main thing I'm after here.

Happy day, Matic

Matic
  • 469
  • 2
  • 8
  • 16

4 Answers4

29

The documentation of apc.enable_cli, which control whether APC should be activated in CLI mode, says (quoting) :

Mostly for testing and debugging. Setting this enables APC for the CLI version of PHP. Under normal circumstances, it is not ideal to create, populate and destroy the APC cache on every CLI request, but for various test scenarios it is useful to be able to enable APC for the CLI version of PHP easily.

Maybe APC will store the opcodes in memory, but as the PHP executable dies at the end of the script, that memory will be lost : it will not persist between executions of the script.

So opcode-cache in APC is useless in CLI mode : it will not optimize anything, as PHP will still have to re-compile the source to opcodes each time PHP's executable is launched.


Actually, APC doesn't "optimize" : the standard way of executing a PHP script is like this :

  • read the file, and compile it into opcodes
  • execute the opcodes

What APC does is store in opcodes in memory, so the execution of a PHP script becomes :

  • read the opcodes from memory (much faster than compiling the source-code)
  • execute the opcodes

But this means you must have some place in memory to store the opcodes. When running PHP as an Apache module, Apache is responsible for the persistence of that memory segment... When PHP is run from CLI, there is nothing to keep the memory segment there, so it is destroyed at the end of PHP's execution.
(I don't know how it works exactly, but it's something like that, at least in the principles, even if my words are not very "technical" ^^ )


Or, by "optimization" you mean something else than opcode cache, like the configuration directive apc.optimization ? If so, this one has been removed in APC 3.0.13

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • From PHP documentation: APC is a free, open, and robust framework for caching and optimizing PHP intermediate code. Guess I'm wondering about the optimization of intermediate code. I plan to execute the CLI script only once and then it will run for a few days, executing some code inside a loop. So APC doesn't really speed up the execution itself, only the time it takes to start executing? – Matic Aug 07 '09 at 16:32
  • http://pecl.php.net/package-info.php?package=APC&version=3.0.13 says "Obsolete and remove apc optimizer" ; I've never heard APC (at least in recent versions) did any kind of "optimization" like what you could think about the compiler can do when you're programming in C. And I don't think it'll reduce the time taken to start executing, as the PHP will have to be compiled to opcode (it's on this part that APC allows a huge gain... When it can keep those opcodes in memory between executions of the script -- which I don't think it can do in CLI) – Pascal MARTIN Aug 07 '09 at 18:07
  • Just to be clear, using APC in CLI mode is still quite useful for caching user-generated objects. In a script I'm working on, we use apc_store() and apc_fetch() to leverage APC's user object caching in a long-running cron job. – rinogo Sep 05 '13 at 21:45
7

If you have CLI code that generates any configuration based on the environment, then the CLI code will think that APC isn't enabled. For example, when generating Symfony's DI container through the CLI, it will tell Doctrine not to use APC (details).

Also, I have not tested it but there's a chance APC may improve the speed of scripts for files included after a pcntl_fork(). Edit: I've asked the question about APC & pcntl_fork() here.

For completeness, to enable APC on the CLI (in Ubuntu):

echo 'apc.enable_cli = 1' > /etc/php5/cli/conf.d/enable-apc-cli.ini
Community
  • 1
  • 1
dave1010
  • 15,135
  • 7
  • 67
  • 64
3

Well, there's a good reason for APC in CLI Mode: UnitTesting: I wanna do my unit test using an environment as close to the later production environment as possible. Zend Framework has an internal caching solution, which may use APC's Variable Cache as Storage Backend - and I wanna use this.

Laph
  • 311
  • 2
  • 6
  • I think in this case you may want to look into another backend for unit testing. Good thing this is pluggable in Zend_Cache. – Till Jan 25 '11 at 18:44
  • I know, but those Backends don't behave exactly the same. Therefore I'd like to do Unittests with the same backend. – Laph Feb 15 '11 at 08:05
3

There is another reason to use it in CLI mode: some scripts are able to use it as a cache

Natmaka
  • 89
  • 3