In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the composer autoloader optimization method, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
Optimization of automatic loader
In general, Composer's autoloader runs relatively fast. However, because the autoload rule of PSR-4 and PSR-0 is to check the file system before finally parsing a class. This will cause the automatic loading speed to become quite slow, but in the development environment, this can be a very convenient way to load, because when you create a new class, the loader will immediately discover and use the class. There is no need for you to rebuild the configuration of the autoloader.
The problems caused by this loading rule are really reflected in the production environment, where you can easily rebuild the configuration before each deployment, and new classes do not appear randomly between deployments, so you don't need it to check the file system all the time, you usually want automatic loading to be completed as quickly as possible.
For the above reasons, Composer provides some optimization strategies for autoloader.
Note: you should not use any of the optimization strategies described in this article in a development environment, as this can lead to problems when you add or remove classes. In fact, the performance improvement caused by the application of these settings in the development environment is far from worthwhile compared with the problems it brings.
Optimization level 1: class mapping generation
How do you run it?
There are several options to enable this feature:
Set "optimize-autoloader": true in the configuration of composer.json
Install or update using the-o /-- optimize-autoloader call
Call dump-autoload with-o /-- optimize
What does it do?
Class mapping generation is essentially a transformation of PSR-4/PSR-0 rules into class mapping rules. This makes everything much faster because the known class map returns the path immediately, while Composer guarantees that the class is there, so there is no need for file system checks.
In PHP 5.6 +, class maps are also cached in opcache, which greatly improves initialization time. If you make sure that opcache is enabled, the class map should be loaded immediately and the class should load quickly.
Tradeoff
There is no real tradeoff in this approach. It should always be enabled in production.
The only problem is that it doesn't track automatic loading (that is, when it can't find a given class), so rules that fall back to PSR-4 can still cause file system checks to slow down. To solve this problem, there are two secondary optimization options that you can decide to enable if you perform a large number of class_exists checks on classes that do not exist in the project.
Optimization level 2CMA: authoritative class mapping
How do you turn it on?
There are several options to enable this feature:
Set "classmap-authoritative": true in the config key of composer.json
Install or update using the-a /-- classmap-authoritative call
Call dump-autoload with-a /-- classmap-authoritative
What does it do?
Enabling this option automatically enables level 1 class mapping optimization.
This option is simple. It says that if you can't find something in the class diagram, it doesn't exist, and the automounter should not try to view the file system according to PSR-4 rules.
Tradeoff
This option enables the automatic loader to always return quickly. On the other hand, it also means that automatic loading is not allowed if classes are generated at run time for some reason. If your project or any dependencies do this, you may encounter a "class not found" problem in production. Use it carefully.
Note: this cannot be used in conjunction with level 2max B optimization. You must choose one because they solve the same problem in different ways.
Optimization level 2max B: APCu cache
How do you turn it on?
There is an option to enable this feature:
Set "apcu-autoloader": true in the configuration key of composer.json
Use the-- apcu-autoloader call to install or update
Use-- apcu to call dump-autoload
What does it do?
This option adds the APCu cache as a backup to the class map. It does not automatically generate class mappings, so you still need to manually enable level 1 optimization if you prefer.
Whether the class is found or not, this fact is always cached in the APCu, so it can be returned quickly on the next request.
Tradeoff
This option requires APCu, which may or may not be useful to you. It also uses APCu memory for automatic loading, but it is safe to use and does not cause classes to be found, such as the authoritative class mapping optimization above.
Note: this cannot be used in conjunction with level 2gamma An optimization. You must choose one because they solve the same problem in different ways.
Thank you for reading this article carefully. I hope the article "the optimization method of composer autoloader" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.