Using Composer Patches to fix Performance in Third-Party PHP Libraries such as Symfony ErrorHandler
Every so often, a careful profiling analysis of your application detects the bottleneck in third-party code and there is little remedy for you to override or change that code. In this blog post I want to present a solution to fixing performance problems in third-party libraries by using the composer patches plugin.
I use the the Symfony ErrorHandler as an example. Another example could be patching a performance bottleneck in Doctrine DBAL array parameter processing for an older version with the code from a newer version.
For certain applications that frequently trigger Symfony deprecations, this may result in a significant loss of response time, because it treats deprecations as a “strong error” that should not be silenced. For instance, in this Tideways trace from a Shopware store, a total of 18794 deprecations result in an additional response time of 699 ms:
An obvious first step would be to fix the code that triggers the deprecations, but here the Shopware core is triggering this deprecation itself. Shopware core is third party code that we cannot just change, especially when installed via Composer. The original code is installed over our changes on every Composer installation or update.
An issue to request a change in this policy to treat deprecations as errors that can never be silenced has been open for a while now. As usual, it’s not as easy for the developers of a framework to find a generic solution that works for them and everyone.
We can intervene here by using the excellent cweagans/composer-patches library, which allows patching vendor code as a subsequent step after installing or updating dependencies. Install the library into your project: composer require cweagans/composer-patches:2.0.0-beta2
Then we create a new file “patches/symfony_error_handler.patch
” and add the following patch for symfony/error-handler:
I would always store the patch locally and avoid using the remote patch feature of this plugin for security reasons.
And modify the composer.json to include a reference to this patch like this:
After this change, you can apply the patch by calling “composer patches-repatch
”:
From this point forward, the patch will be applied during every composer installation or update if the error-handler package changes.
Please be aware that this change effectively disables Symfony deprecation functionality unless the Symfony application is in debug mode.
An AI would never be able to dive this deep into technical topics surrounding PHP performance! Follow us on LinkedIn or X and subscribe to our newsletter to get the latest posts.
Let’s dive in and find out what is causing performance bottlenecks! Without Tideways, you’re likely to fish in murky waters attempting to figure it out. Try our free trial today and be enlightened.