I created an empty blog on Laravel. The project is empty but the default libraries and components, i.e. there is nothing here but the boilerplate of Laravel.
7545 files in total. (including files that aren't PHP files)
Note: Laravel requires 7000 files for a simple hello world. Are they kidding us?
Anyways, I’m not talking about Laravel (yet) but about autoload
It’s a free library that generates a single autoload.php file. It has an interface visual and it doesn’t care if the project is PSR-0 or PSR-4, it works in any case. It even works if there are more than two classes in the same file or if the class doesn’t have namespace.
How it work?
- Copy the library on the root project.
- Execute the page, push the button and.
- That’s it.
So, I generated a Laravel project, I run the autoload generator and it returns the next values:
Number of Classes: 5565 Number of Namespaces: 765 Number of Maps: 2305 (you want to reduce it) Number of PHP Files: 6302 Number of PHP Autorun: 0 Number of conflicts: 31
I separated PHPUnit and Mockery from the project. Why?. Both libraries are for unit test.
excluded namespace = /vendor/phpunit/,/vendor/mockery/
Number of Classes: 5565 Number of Namespaces: 765 Number of Maps: 1535 (you want to reduce it) Number of PHP Files: 6302 Number of PHP Autorun: 0 Number of conflicts: 13
composer dump-autoload -o
Generated optimized autoload files containing 3519 classes Number of Maps: 3519 (static and not-static)
Autoload uses one of the next methods:
Static: (fast method, it uses more memory and it requires to be calculated manually)
Not static: (default method)
Let's say we are calling a single webpage that uses autoload.
If we use Composer's autoload (static), we are also calling a file that uses 468kb (plus other files), and this memory is loaded into the memory. It could use (an average of) 609kb of ram per call (it's around PHP file x 1.3 x 1kb)
For example, what if we have 1000 concurrent users. It will use 609kb x 1000 = 609mb of ram thanks to Autoload alone at the same time and with 10k concurrent users we will use 6gb of ram only because autoload.
With AutoLoadOne, it is optimized to 302mb (1000 users) or 3gb (10k users), it is for the version not optimized.
AutoLoadOne tags all classes from the project, including classes that aren't defined in composer.json (unless they are excluded from the project). ** Composer autoload found only 3519 classes, while AutoLoadOne found all classes of the project (5565).**
However, some classes are not required to be loaded by the project (for example unit test classes), so we could exclude those classes of the project.
For example, excluding PHPUnit and Mockery reduces the use to 206mb (1000 users) or 2gb (10k users) but we could optimize it even further.
|Concurrent Users||Composer's autoload (Optimized)||AutoLoadOne||AutoLoadOne Optimized|
Let's say we have a "map" with different elements. How much time does it takes to find the element of the map?.
|Map size||Time (1 million of lookup sequentially)|
|100||0.271 seconds +/-|
|10.000||0.299 seconds +/-|
|1.000.000||0.376 seconds +/-|
So, the size of the map/lookup time is not important. The difference between a small map (100 elements) versus a huge map (1 million of elements) is 0.1 second in total (per 1 million of queries). However, the memory usage matters and it could impact the performance considerably.
Let's say we have 10k concurrent users and each one calls 100 different classes. It means we are doing 10k x 100 = 1 million of lookup at the same time.
Magento is a huge project, it has 22k PHP files and from it, 20k are classes. However, it generates a map of 9.2k elements (without optimization)
Number of Classes: 20596 Number of Namespaces: 6868 Number of Maps: 9242 (you want to reduce it) Number of PHP Files: 22227 Number of PHP Autorun: 0 Number of conflicts: 6 File size 1.06mb
It takes 38 seconds to generate the autoload.php
While using Composer's autoload (optimized)
Generated optimized autoload files containing 11329 classes Number of Maps: 11329 (1.6mb file size)
|Concurrent Users(*)||Composer's autoload (Optimized)||AutoLoadOne (not optimized)|
(*) However, Magento wasn't created for concurrency. But, what we are measuring is not the number of concurrent users but the number of concurrent calls (for example rest-json, opening a page and such).
Both AutoLoadOne and Composer's autoload execute a code when it is initialized/executed.
When AutoLoadOne generates the map, it consists of two relational arrays as follow:
private $_arrautoloadCustom = array( 'Magento\AdminNotification' => '/app/code/', 'Magento\Notice' => '/app/code/Developer/',...
While Composer's autoload generates an array that requires concatenation.
array( 'Magento\\AdminNotification' => $baseDir . '/app/code/MagentoActions.php', 'Magento\\Notice' => $baseDir . '/app/code/Developer/Notice.php',
So it requires to concatenate each map (with a variable called $baseDir). So Composer's autoload impacts slightly the performance.
- Composer’s autoload could be optimized (or to use a different one like in my case).
- The number of files COUNTS!. Of course, we don’t want a single big file but it’s impractical to create thousand of PHP file and it has hidden costs.
Note: Also published on Medium https://medium.com/cook-php/comparing-phps-autoload-cae183817a6a