Author: zinsou A.A.E.Mo´se
Last month viewers: 8
Package: PHP LGCF Loader
The LGCF package is an attempt to implement this concept real for the PHP classes auto-loading, as well for the global functions loading.
Despite the fact that the first version was functional and has even been nominated to the PHP Innovation Awards organized by PHP Classes, its performance was not really up to its potential.
Read this article to know more about how the latest version of this package has drastically been improved to reach 1000% of performance compared to the first one.
In the programming world, and in particular in the PHP language, the copying and pasting of code is not a valued practice.
In fact, the PHP language provides the
include a piece of code any where it is needed in order to reduce
rewriting the same code, allowing easier maintenance benefiting code re-use.
The standardization of programming practices also pushes programmers to adopt a style of coding which leads to the practices like "one class one file" in spite of the fact that PHP itself does not force it.
Undeniably, it is a good practice, so classes auto loading has first been added to the language and then improved to avoid multiple file inclusion.
Different types of auto loading techniques have appeared after that. Some of them have even been standardized and adopted by many programmers.
The LCFG package is an attempt to make not only the classes loading easier but also autoloading of global functions loading on demand possible anywhere.
The first version of this package was only a draft, thus not really high-performance. But the newer versions was very much improve on the speed aspect.
A Brief Introduction to What Was the First Version
The first version of the package only used PCRE Regex to parse one file which had to contain all the classes or functions written in a specific format. Despite the fact that this approach was cool in my opinion, it was too restrictive.
After this parsing step,
there was also another limitation. The package had to use the superglobal variable
$_SESSION array to store the parsed
data. I was really not glad to use this array but for the package draft version it was the only
one possibility that came to my mind.
After the edition of October 2017 of PHP Innovation Award, I noticed that the package ranked well and it encouraged me to dig a little more to make some changes in order to improve it.
So I created the PHP Code Gleaner And Analyzer Tool. This new package of which I am really proud, became the core for the second major version of the Loader.
The loader was from then on able to traverse any specific directory recursively and parse PHP content without restrictions of format, of names or of any other type.
It was able to parse files containing mixed data, functions, classes, HTML content, classes with content and functions with content. The data could also be parsed progressively and only once it was needed.
There were no more specific restrictions imposed by the loader. I am really proud of this.There were of course some issues in my gleaner package once I found those issues, I brought the fixes to this loader package too.
What is Really New in this Updated Version of the Loader?
The real cause of the old version weak performance
Starting from the version 1.0.0 to version 2.0.1 superglobal
$_SESSION array was the only storage means which was used to achieve the main purpose of this class. As
I already commented above I was not very proud of that fact.
The class was working perfectly but the performance I wanted to reach was not achieved. In fact, using this superglobal as storage means led to a big memory performance issue.
Since PHP by default uses one session file per user, these files were filled with the same contents and this increased seriously their size and thus lowered seriously disk space.
A simple test on a basic Symfony site crawled give us as statistics:
- About 5549 classes including Traits and Interfaces but also test case classes.
- The average size of the data stored in each
$_SESSION file was about 18000 KB
When we consider the time it takes to crawl a site as well the time to load a page, assuming that you have an average of 10000 users online at the same time, a simple multiplication give as result about 171.2 GB.
Now imagine just for a moment if you got 100000 active sessions, you will reach 1.7 TB of useless data on your disk. And this is without regard to a custom session handling which could use database.
And this is for this reason that I had to completely avoided this approach of using the session super global which must only be used to store a small amount of specific data for each user.
The Progress Achieved in the Latest Version
Fortunately I developed a new package published in the PHP classes site which also confirmed that it was a good idea to replace
$_SESSION super global.
I called it Persistent PHP Super Globals Array Maker. As its name indicates, its purpose is to allow any one to build super globals of their chosen name and then be able to access them any where.
This package allows you to build session type global variables which uses one file per user but also global variables which are only specific to the current site, and so use only one file.
The progress is notable. Now only one global file will be used for all users. If we go back to previous approach example, only 18000ko for all users, about 0.01% of the size used before.
And the process is really optimized. The data is cached once and forever unless you clear the log yourself. And this for every user that accesses the site.
The data is cached progressively as soon as user browse your site pages for the first time. The file paths are cached automatically and the process is never repeated for another user.
The performance is better than ever. No more disk
space issues or other linked to the usage of
Consider the example above, the loading time of this global is between 0.8 sec and 1.5 sec and the auto loading time for a random undefined class is always less than 0.001 sec.
I must emphasize that the
test has been running on a very old Compaq Evo with the following characteristics:
OS: Windows 7 (x86)
System evaluation by Windows: 1
Processor: Intel(R) Pentium(R) 4 CPU 2.40GHz 2.39Ghz
Memory: 1.00 GB
Disk space: 40GB
Free space: 1,40GB
PHP version: 5.6.30
Concerned Defaults ini settings :
APC and APCu extensions are not activated
OPcache is not activated
Have You Hear about the Assetic Package? Now this Package Actually Works like Assetic
I think the main purpose of auto loading, as I already said above, is for programmers to keep their code separated and clean and easy to maintain.
The version 3.0.0 of this loader package does the same thing with your PHP classes and functions files.
It lets you keep your classes and functions files separated and easy to maintain, but it creates a global storage which gathers all code and data in one file that can be lazy loaded from any of your site script pages.
How Can You Migrate Your Old Auto Loading Methods to This One and Benefit from the Speed Optimizations?
There are is no need to migrate your old autoloading code. PHP allows to register as many auto loading functions as you want.
So this package can be used together with the other auto-loading methods, as you only need specify the path and classes or functions you want to make global.
Finally if you want to migrate completely from other auto-loading approach, there is only one step besides the installation if the package. Just use the following code at the beggining of your page scripts, instead of usind your old autoloader code.
Just make sure you perform this package installation correctly, as explained above. Traversing the code of all your site pages is necessary only for projects that are already using another autoloading method.
Final Remarks and Conclusion
With the progress of the latest version, as the introduction of this article mentions, we reached 1000 per cent of performance if we compare this new version to the old one.
Some may ask: what is the gain compared to other auto-loading system? My answer is: go to the Loader homepage and read the comment of the nomination.
It actually helps to load faster your classes and your functions on demand. On every page of your site, all your classes will be available but they will not be loaded unless you need them. Your global functions too.
It is just like having a big file containing all the functions and classes and include it once except that the classes and the functions will not defined immediately. It also avoids multiple file inclusion while allowing a structure of multiple files.
It is really much better than the previous version. However we are conscious that nothing is perfect in this world and certainly not this loader. All we hope is that we will find the way to make it better from version to version.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
No comments were submitted yet.