In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example of composer autoload automatic loading performance optimization, which is very detailed and has a certain reference value. Friends who are interested must finish it!
The autoload mechanism provided by composer makes it very convenient for us to organize code and introduce new class libraries, but it also degrades the performance of the project.
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 look up the corresponding class file location in the file system, which results in a lot of performance loss. Of course, this is useful when we develop, so that the new class files we add can take effect immediately. But in production mode, we want to find these class files as quickly as possible and load them.
So composer provides several optimization strategies, which are described below.
Level 1 (Level-1) optimization: generate classmap
How to run:
Execute the command composer dump-autoload-o (- o is equivalent to-- optimize)
Principle:
The essence of this command is to convert the rules of PSR-4/PSR-0 into the rules of classmap, because classmap contains the correspondence between all class names and class file paths, so the loader no longer needs to look for files in the file system. You can find the path to the class file directly from classmap.
Matters needing attention
It is recommended to turn on opcache, which will greatly speed up the loading of classes.
Opcache is included by default in future versions of php5.5.
This command does not take into account the situation when the target class is not found in classmap. When the loader cannot find the target class, it will still look in the file system according to the rules of PSR-4/PSR-0.
Level 2 (Level-2/A) optimization: Authoritative (Authoritative) classmap
Execute the command:
Execute the command composer dump-autoload-a (- an is equivalent to-- classmap-authoritative)
Principle
The implied execution of this command also executes the command of Level-1, that is, classmap is also generated. The difference is that when the loader cannot find the target class in classmap, it will not look for it in the file system (that is, it implicitly thinks that classmap is all legal classes, and there will be no other classes, except for illegal calls).
Matters needing attention
If your project generates classes at runtime, you will not be able to find these newly generated classes using this optimization strategy.
Level 2 (Level-2/B) optimization: using APCu cache
Execute the command:
Execute the command composer dump-autoload-- apcu
Principle:
Using this strategy requires the installation of the apcu extension.
Apcu can be understood as a piece of memory and can be shared among multiple processes.
The purpose of this strategy is to store the results found in the file system in shared memory when the target class is not found in classmap in Level-1, and it can be returned directly from memory the next time it is searched, instead of going to the file system to look for it again.
In a production environment, this strategy is generally used with Level-1 to execute composer dump-autoload-o-apcu, so that even if a new class is generated in the production environment, it can be cached only once in the file system, making up for the shortcomings of Level-2/A.
How to choose the optimization strategy?
To choose the strategy according to the actual situation of your project, if your project will not generate class files at run time and need composer's autoload to load, then use Level-2/A, otherwise using Level-1 and Level-2/B is a better choice.
A few hints
The optimization of Level-2 is basically a supplement to the optimization of Level-1. Level-2/A mainly decides whether to continue to find the target class in classmap. Level-2/B mainly provides a caching mechanism to cache the file path found in the file system when it cannot be found in classmap to speed up subsequent lookups.
When the Level-2/An is executed, it means that the search will not continue if it is not found in the classmap, and the Level-2/B will not take effect at this time.
In either case, it is recommended to turn on opcache, which will greatly improve the class loading speed, and I can see a performance improvement of at least 10 times.
The above is all the content of the article "composer autoload Auto-load performance Optimization example". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.