Does merging PHP files into one only increase performance?

Asked

Viewed 847 times

29

I get the impression that every time I execute the command dump-autoload to generate the autoload of the classes via composer, an archive bootstrap/compiled.php is created.

Within it, there seems to be a tangle of classes and namespaces (it is as if they have united several files into one).

Generating PHP code in which the classes used are in the same file would be "faster" than having them "scattered" across directories?

  • The archive compiled.php can slow down the application ? Because when installing Laravel 5 you are asked to delete the file. Once installed, you should upgrade to the Latest Packages. First, remove {directory}/vendor/compiled.php file then change your Current directory to {directory} and Issue Composer update command.

  • @Zooboomafoo in this case is asked to delete this file because the source class will be different from the one in Compiled. It is usually already deleted, but sometimes some problem may occur.

  • Related: http://answall.com/q/15005/250

3 answers

35


Yeah, up. So what?

First, it will be a minimum increase. Irrelevant in most cases, if not all. The gain will be only in charge. And you probably won’t even be able to measure.

You probably don’t need to have that gain. And if you do, there are many other things more important to improve performance.

Forget it, think about the organization.

Obviously, the less disk access, the better. But if this is used little makes no difference, if it is used a lot will already be in the operating system cache and the difference will be very close to zero.

You will have better benefit if you use one default language code cache system or third-party.

I don’t know exactly what does this that you mentioned but it’s probably all in one file because it’s not meant for a human being to read, it doesn’t have to be organized, so it’s easier to do it that way. He doesn’t put it all together because of the performance.

  • Of course I won’t put it all together! but the idea would be cool in a system where some command would generate it automatically. In fact, I find microtimization ridiculous... And the question was more curiosity... rsrsrs

  • 2

    I understood that.

  • Complementing the answer, a good read is the Psrs that are recommendations on code organization that bring more relevant benefits than the performance of the code itself. (http://www.php-fig.org/psr/)

  • One thing that has not been mentioned is that despite having a gain with the decrease of read on disk, you may have an excessive use of memory. For example, if you have 3 classes in a file, the 3 classes will be loaded in memory, when it is divided into several files and an autoload is used, the class will only be loaded into memory when used. That is the performance gain with the read on disk can.

  • Excellent answer +1.... the most important is to have ease in maintaining the code... this is the speed that matters: Speed in meeting new demands of your customer.

21

Generate PHP code in which the classes used are in the same file would be "faster" than having them "scattered" by directories?

Theoretically performing a single file decreases access to the disk, which does not need to search for several other files.

But worrying about this can be an early optimization, because when using the OpCache, which comes by default from the PHP 5.5+, but deactivated, a cache of the files in memory is made and the I/O neck dies there.

I get the impression that every time I execute the command dump-autoload to generate the autoload of the classes via bootstrap/Compiled.php file is created.

Composer does not generate the file compiled.php when executing dump-autoload, this command only generates the autoloaders of the briefcase vendor\composer from what is configured in composer.json of the project.

You must be confusing this file with the php artisan otimize of Laravel, who uses a library to generate this automatically compiled file to join framework classes, file that can bring more trouble than help.

In short: don’t worry about it. In production environments enable PHP Opcache and be happy.

More about Opcache you can read hereen.

  • 2

    If I could, I’d give +2, because of that OpCache :)

  • Ah, and the command to create this file compiled.php exists :)

3

Yes, it increases performance, but the end result is derisory, in fact not even worth the work.

Because you should remember that in the future you will need to take the minification that you made in the code, and as any PHP system is more than 1 file, you will have a lot of work to do for a ridiculous performance difference as I said earlier.

If you want to better understand how PHP’s performance works on your systems, try to study how memory allocation and variable types work, so you can start coding and typing (even if it’s not a common practice in PHP) its variables and thus prevent a variable from having a larger allocation than it should in memory.

  • 1

    In fact, if I were to minify the code, I would use a tool that would do this from an original source. I mean, minifying wouldn’t be the problem. Only with the other answers, I ended up getting convinced that really not worth making this minification. Thanks for the answer

  • Oh yes, there are several generated that does this for you, but remember that you would have to file to the file, you could use some plugin in sublime text that does this (or develop it).

  • But as I said, it’s not worth it, because that ultimately makes no difference in its application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.