New in Symfony 4.1: Faster serializer
May 18, 2018 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
The Serializer component will be much faster in Symfony 4.1, improving the performance of your applications up to 40%. Best of all, you don't have to make any changes in your code to make it faster. Just upgrade to Symfony 4.1 when it is released at the end of this month.
If you have used the Symfony serializer, you probably know that it works by normalizing/denormalizing PHP objects into arrays and then encoding/decoding that array into the desired format (JSON, XML, etc.)
Applications can define lots of normalizers/denormalizers and Symfony must call
the supportsNormalization()
method for each of them whenever a new object
is normalized/denormalized. In theory the result of supportsNormalization()
can depend on multiple factors. In practice most normalizers only depend on
the type and format and that information is easily cacheable.
That's the trick used to improve the Serializer performance. We've introduced a
new CacheableSupportsMethodInterface
for those normalizers/denormalizers
that only use the type and the format in their supports*()
methods:
1 2 3 4 5 6
namespace Symfony\Component\Serializer\Normalizer;
interface CacheableSupportsMethodInterface
{
public function hasCacheableSupportsMethod(): bool;
}
We've already implemented this interface in all the built-in normalizers, so you don't have to change your code. If you have created your own normalizers, check if they can be cached in the same way and implement the interface if needed.
According to our own benchmarks, this change can make simple apps which use few normalizers up to 10% faster. Complex applications with lots of normalizers, such as API Platform apps, can be up to 40% faster.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
- you don't have to make any change in your code -> you don't have to make any changes in your code
- when it releases at the end of this month -> when it is released at the end of the month
- Symfony must call to the supportsNormalization() method of each of them -> Symfony must call the supportsNormalization() method for each of them
- Api Platform -> API Platform