Home  >  Article  >  Performance optimization, new features and upgrade guide brought by Composer 2.0 release

Performance optimization, new features and upgrade guide brought by Composer 2.0 release

藏色散人
藏色散人forward
2020-10-29 17:30:423623browse

Performance optimization, new features and upgrade guide brought by Composer 2.0 release

Composer 2.0 release brings performance optimizations, new features and upgrade guide

Composer released version 2.0 on Programmer’s Day yesterday. This version has been 8 years since the release of Composer 1.0 beta version. As a PHP package management tool, Composer has given everyone The elephant (PHP's logo is an elephant) has wings, bringing a new installation and management experience for extension packages (or dependency packages) to PHP project development, which not only reduces their maintenance costs, but also greatly enriches PHP's ecosystem.

This new version provides many new features, and more importantly, the performance has also been greatly improved. If you are interested, you can check the detailed upgrade details in the Composer GitHub repository. Here I will give you a brief introduction to the performance comparison of the new and old versions in popular PHP projects, the main new features and how to upgrade to Composer 2.0.

Performance Optimization

The new version overhauls almost all code, from the protocol used between Composer and packagist.org to dependency resolution Refactorings, including the use of curl to download files in parallel and optimization of constraint evaluation (i.e., version control of expansion packages), etc. These refactorings have greatly improved Composer 2.0 in terms of both speed and memory usage.

However, the actual performance of these improvements depends on the specific usage scenarios. Although official reports of 50% performance improvement have been obtained in some projects, it cannot be used as a basis to give exact figures that apply to all scenarios. But one thing is for sure, if you haven’t used Composer 2.0 yet, you will be surprised by your experience with the new version.

In addition, require/remove and some updates are much faster than before, because Composer will now only load the metadata corresponding to the modified expansion package.

The following is a comparison of the speed optimization of Composer 1 and Composer 2 in currently popular PHP projects:

Performance optimization, new features and upgrade guide brought by Composer 2.0 release

As you can see, for Laravel projects, the performance Improved by about four times. I personally experienced using the old and new versions to initialize a Laravel project, and there was indeed a significant speed improvement visible to the naked eye.

Overview of the main new features

We give a brief overview of some important updates in Composer 2.0:

Architectural adjustments

The way dependency updates are executed internally has been refactored. For you, you can now see more deterministic updates. After the updates are completed, the installation process will automatically execute concurrently, thus avoiding the need to just The process was terminated halfway through the installation due to network problems.

New runtime features

vendor/autoload.php adds a new platform detection step during initialization, mainly checking whether the current PHP version and the extension package version match, and if they do not match If so, initialization will fail.

In the Composer 2.0 project, you can see a new InstalledVersions class in the vendor/composer directory. It will be automatically loaded in each project and is valid at runtime. You can use it to check the runtime Extension packages valid in the project and their version numbers.

If your code depends on these new runtime features, you can add the "composer-runtime-api": "^2.0" dependency statement in the require configuration item of composer.json.

Error report optimization

Composer 2.0 optimizes the display of error reports when dependencies cannot be resolved. The error messages will now be shorter, clearer, and less repetitive.

Partial update with temporary constraints

Now you can run composer update vendor/package:1.0.* to upgrade the version of the specified extension package (such as vendor/package here) , it will not update composer.json, nor will it update the composer.lock file. If you want to add this temporary constraint and update all dependencies at the same time, you need to use the composer update --with vendor/package:1.0.* command.

Upgrade to Composer 2.0

Upgrading to Composer 2.0 is very simple, just run the following command:

composer self-update --2

Performance optimization, new features and upgrade guide brought by Composer 2.0 release

It should be noted that after the upgrade, some Composer 1.0 version plug-ins may not yet support Composer 2.0, and the new platform detection mechanism will detect whether the runtime PHP version and the expansion pack version match. These are all It may cause problems with the previously normal expansion package dependency resolution. You can use the composer self-update --rollback or composer self-update --1 command to roll back to the previous old version of Composer, or read the Composer upgrade guide to learn more. detail.

Statement:
This article is reproduced at:xueyuanjun.com. If there is any infringement, please contact admin@php.cn delete