Home > Development Tools > composer > Share composer autoload automatic loading performance optimization guide

Share composer autoload automatic loading performance optimization guide

藏色散人
Release: 2020-08-05 13:10:51
forward
3074 people have browsed it

The following tutorial column of composer will introduce to you the composer autoload automatic loading performance optimization guide. I hope it will be helpful to friends in need!

Share composer autoload automatic loading performance optimization guide

The autoload mechanism provided by composer makes it very convenient for us to organize code and introduce new class libraries, but it also reduces the performance of the project a lot.

The main reason why composer autoload is slow is due to the support for PSR-0 and PSR-4. When the loader gets a class name, it needs to find the corresponding class file location in the file system, which leads to a lot of trouble. Performance loss, of course, this is still useful when we are developing, so that the new class files we add can take effect immediately. But in production mode, we want to find these class files and load them as quickly as possible.

So composer provides several optimization strategies. These optimization strategies are explained below.

First level (Level-1) optimization: Generate classmap

How to run:

Execute command composer dump-autoload -o (-o is equivalent to --optimize)

Principle:

The essence of this command is to convert PSR-4/PSR- The rules of 0 are transformed into classmap rules. Because classmap contains the correspondence between all class names and class file paths, the loader no longer needs to search for files in the file system. The path to the class file can be found directly from the classmap.

Notes

It is recommended to enable opcache, which will greatly speed up the loading of classes.

php5.5 and later versions come with opcache by default.

This command does not take into account the situation when the target class is not found in the classmap. When the loader cannot find the target class, it will still go to the file system according to the rules of PSR-4/PSR-0. Search

Second level (Level-2/A) optimization: Authoritative (Authoritative) classmap

Execute command:

Execute commandcomposer dump-autoload -a (-a is equivalent to --classmap-authoritative)

Principle

Execute this command to hide The Level-1 command is also executed, that is, the classmap is also generated. The difference is that when the loader cannot find the target class in the classmap, it will not search in the file system (that is, it implicitly believes that the classmap is All legal classes, there will be no other classes unless called illegally)

Notes

If your project will generate classes at runtime, use this The optimization strategy will not find these newly generated classes.

Second level (Level-2/B) optimization: using APCu cache

Execute command:

Execute command composer dump-autoload --apcu

Principle:

Using this strategy requires the apcu extension to be installed.

apcu can be understood as a piece of memory and can be shared among multiple processes.

This strategy is to store the result found in the file system into the shared memory when the target class is not found in the classmap in Level-1. When searching next time, it can be directly retrieved from the memory. Return without having to search again in the file system.

In the production environment, this strategy is generally used together with Level-1. Execute composer dump-autoload -o --apcu. In this way, even if a new class is generated in the production environment, only the file system is needed. It can be cached after searching once, which makes up for the shortcomings of Level-2/A.

How to choose an optimization strategy?

You should choose a strategy based on the actual situation of your project. If your project does not generate class files when running and requires composer's autoload to load, then use Level-2/A. Otherwise, using Level-1 and Level-2/B is a better choice.

A few tips

  • Level-2 optimization is basically a supplement to Level-1 optimization, and Level-2/A is mainly a decision When the target class is not found in the classmap, whether to continue searching for it, Level-2/B mainly provides a caching mechanism. When the target class is not found in the classmap, it will cache the file path found in the file system. , speeding up subsequent searches.

  • When Level-2/A is executed, it means that if it cannot be found in the classmap, it will not continue to search. At this time, Level-2/B will not take effect.

  • No matter what the situation is, it is recommended to turn on opcache. This will greatly improve the class loading speed. I have visually measured the performance improvement by at least 10 times.

The above is the detailed content of Share composer autoload automatic loading performance optimization guide. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:dahouduan
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template