Some days after the last time I wrote about how Composer could help preloading your project, Seldaek (member of the Composer team) shot down any hope to open a Composer-way to easily preload your project into the PHP process from the package manager itself.
(…) But just to be clear, I am fairly confident that in the near future we are not going to add anything to Composer relating to preloading.
The reason? Preloading in PHP it’s more about a deployment problem than a dependency problem, which is reinforced with manually editing
php.ini to make it work — something only the developer can do, manually, if he controls PHP itself.
But that alone cannot stop me from doing my own package to preload my own project, and yours.
Preloading and metrics
If you just want the package, go here, but if you want to know WHY, keep reading.
Preloading can become a good tool to push more performance from an application without major reworks, easily and cheaply.
The problem is not how to preload, but what. Preloading whole frameworks and thousand of files will quickly deplete your memory, so blindly guessing is no go, at least, for larger projects. A proper strategy is to just preload files that are the most requested, but how to know that?
Luckly, Opcache offers statistics about what files are “hit” the most thanks to the
opcache_get_status(). We should get some nice numbers about what files most requested, and even how much memory they consume, after the application is running for a while.
The recommended time is to wait a week, or when Opcache reaches a number of hits. That will depend on the application, but you get the idea.
So, what about creating a preload list of files based on the most requested? I made a package for that.
This package will automatically create a preload list for your application. It will gather some basic Opcache statistics, order the list by the most requested first, and create a list until the size of the files hit a memory limit set.
After giving it too much thought about what strategies to use to create the list, is just better to include all the files you can until a given memory limit, which in the package is 32MB as default. These files are sorted by most hit first, and also excludes the package itself automatically.
In other words, PHP will make your application more performant for most of the requests done to it.
And how you use it? Well, point the Composer Autoloader, where to write the Preloader script, and you’re set.
Of course, the “when” to generate is up to you, but that’s the gist. You can even do a random chance and overwrite the list when a request hits the lottery, like one in 100 requests.
You will receive a preload script like this, ready to put in your
And that’s all. Try it and tell me what you think: