search
HomeDevelopment Toolscomposercomposer autoloader optimization

composer autoloader optimization

Oct 29, 2019 pm 02:29 PM
composer

The following composer command uses the tutorial column to introduce to you about composer automatic loader optimization. I hope it will be helpful to friends in need!

composer autoloader optimization

Autoloader optimization

Normally, Composer’s autoloader runs relatively fast. However, due to the autoloading rules of PSR-4 and PSR-0, the file system needs to be checked before finally parsing a class. This will cause the speed of automatic loading to become quite slow, but in a development environment, this will be a very convenient way of loading, because when you create a new class, the loader will immediately discover and use the class without You are required to rebuild the autoloader configuration.

The problems caused by this kind of loading rule are really reflected in the production environment. In the production environment, you can easily rebuild the configuration before each deployment, and new configurations will not randomly appear between deployments. class, so you don't need it to check the file system all the time, you generally want the autoload to complete as quickly as possible.

For the above reasons, Composer provides some optimization strategies for autoloaders.

Note: You should not use any of the optimization strategies described in this article in a development environment, as this will cause various problems when you add or remove classes. In fact, the performance gain from applying these settings in a development environment is far outweighed by the problems it causes.

Optimization level 1: Class map generation

How to run it?

There are several options to enable this feature:

Set "optimize-autoloader": true in the configuration of composer.json

Use -o / - -optimize-autoloader calls installation or update

Use -o / --optimize to call dump-autoload

What does it do?

Class mapping generation essentially converts PSR-4/PSR-0 rules into class mapping rules. This makes everything a lot faster, since a known class map returns the path immediately, and Composer can guarantee the class is there, so no file system checks are needed.

In PHP 5.6, classmaps are also cached in opcache, which greatly improves initialization time. If you make sure opcache is enabled, the classmap should load immediately and the classes will load quickly.

Tradeoffs

There are no real tradeoffs with this approach. It should always be enabled in production.

The only problem is that it doesn't track autoloading (i.e. when it can't find a given class), so it's still possible that those rules that fall back to PSR-4 will cause filesystem checks to be slower . To solve this problem, there are two secondary optimization options that you can decide to enable if you perform a lot of class_exists checks for classes that do not exist in your project.

Optimization level 2/A: Authoritative class mapping

How to enable it?

There are several options to enable this feature:

Set "classmap-authoritative": true in the config key of composer.json

Use -a / --classmap-authoritative Call to install or update

Use -a / --classmap-authoritative to call dump-autoload

What does it do?

Enabling this option automatically enables level 1 class map optimization.

This option is simple, it says that if something is not found in the class diagram, then it does not exist and the autoloader should not try to look at the file system according to PSR-4 rules.

tradeoff

This option enables the autoloader to always return quickly. On the other hand, it also means that if the class is generated at runtime for some reason, autoloading is not allowed. If your project or any of its dependencies do this then you may run into "class not found" issues in production. Be careful to enable it.

Note: This cannot be used in conjunction with Level 2/B optimization. You have to choose one because they solve the same problem in different ways.

Optimization Level 2/B: APCu Cache

How to enable it?

There is an option to enable this feature:

Set "apcu-autoloader": true in the configuration key of composer.json

Use --apcu- Autoloader calls installation or update

Use --apcu to call dump-autoload

What does it do?

This option adds the APCu cache as a fallback to the class map. It does not automatically generate classmaps, so you still need to manually enable level 1 optimization if you wish.

Whether the class is found or not, this fact is always cached in APCu so it can be returned quickly on the next request.

Weigh-offs

This option requires APCu, which may or may not work for you. It also uses APCu memory for autoloading, but it is safe to use and does not cause classes not to be found, like the authoritative classmap optimization above.

Note: This cannot be used in conjunction with Level 2/A optimization. You have to choose one because they solve the same problem in different ways.

The above is the detailed content of composer autoloader optimization. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete
Composer: Streamlining PHP Project DevelopmentComposer: Streamlining PHP Project DevelopmentApr 15, 2025 am 12:08 AM

Composer simplifies PHP project development steps include: 1) adding dependencies, such as adding "laravel/framework":"^8.0" in composer.json; 2) automatically loading, generating an autoload.php file by defining the namespace and classpath; 3) optimizing performance, using composerdump-autoload-o and composerinstall--no-dev-optimize-autoloader commands. Through these steps, developers can be more efficient and avoid common mistakes.

Composer Credentials: What Makes Someone a Composer?Composer Credentials: What Makes Someone a Composer?Apr 14, 2025 am 12:13 AM

The key elements of becoming a composer include: 1. Mastering music theory, 2. Being creative and original, 3. Having technical skills, 4. Being able to express emotions and tell stories, 5. Understand cultural and historical background, 6. Accumulating practice and experience, these elements together constitute the composer's identity and ability.

The Path to Becoming a Composer: A Practical GuideThe Path to Becoming a Composer: A Practical GuideApr 13, 2025 am 12:11 AM

The steps to becoming a composer include: 1. Master the basic elements of music, such as notes, rhythm, harmony, and melody; 2. Select appropriate technical tools, such as AbletonLive; 3. Understand the process of composing, including inspiration acquisition, conception, writing, modification and improvement; 4. Start with simple melody creation and gradually try complex techniques such as harmony; 5. Solve common problems through debugging techniques, such as note selection and rhythm arrangement; 6. Apply performance optimization and best practices, such as using templates, version control, and collaboration.

Composer: The Key to Building Robust PHP ApplicationsComposer: The Key to Building Robust PHP ApplicationsApr 12, 2025 am 12:05 AM

Composer is a key tool for building robust PHP applications because it simplifies dependency management, improves development efficiency and code quality. 1) Composer defines project dependencies through composer.json file and automatically downloads and manages these dependencies. 2) It generates a composer.lock file to ensure that the dependency version is consistent and automatically loaded through vendor/autoload.php. 3) Examples of usage include basic usage such as adding log libraries, as well as advanced usage such as version constraints and environment variable management. 4) Common error debugging techniques include handling dependency conflicts and network problems. 5) Performance optimization suggestions include using composer.lock file and optimizing automatic loading.

Composer Expertise: What Makes Someone SkilledComposer Expertise: What Makes Someone SkilledApr 11, 2025 pm 12:41 PM

To become proficient when using Composer, you need to master the following skills: 1. Proficient in using composer.json and composer.lock files, 2. Understand how Composer works, 3. Master Composer's command line tools, 4. Understand basic and advanced usage, 5. Familiar with common errors and debugging techniques, 6. Optimize usage and follow best practices.

What is a composer doing?What is a composer doing?Apr 08, 2025 am 12:19 AM

Composer is a dependency management tool for PHP, used to declare, download and manage project dependencies. 1) Declare dependencies through composer.json file, 2) Install dependencies using composerinstall command, 3) parse the dependency tree and download it from Packagist, 4) generate the autoload.php file to simplify automatic loading, 5) optimize use includes using composerupdate--prefer-dist and adjusting the autoload configuration.

What is App composer?What is App composer?Apr 07, 2025 am 12:07 AM

AppComposer is a tool for building and managing applications. 1) It simplifies application development and improves efficiency by dragging and configuring predefined components. 2) Developers can define components, combine interfaces, define business logic, and ultimately render the application. 3) Support basic and advanced usage, such as task management and conditional rendering, helping to build flexible applications.

What is a composer used for?What is a composer used for?Apr 06, 2025 am 12:02 AM

Composer is a dependency management tool for PHP. The core steps of using Composer include: 1) Declare dependencies in composer.json, such as "stripe/stripe-php":"^7.0"; 2) Run composerinstall to download and configure dependencies; 3) Manage versions and autoloads through composer.lock and autoload.php. Composer simplifies dependency management and improves project efficiency and maintainability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool