New in Symfony 4.1: Faster serializer
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
supportsNormalization() method for each of them whenever a new object
is normalized/denormalized. In theory the result of
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
CacheableSupportsMethodInterface for those normalizers/denormalizers
that only use the type and the format in their
1 2 3 4 5 6
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.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.