Kévin Dunglas Nicolas Grekas
Contributed by Kévin Dunglas and Nicolas Grekas in #27049 and #27105

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.

Published in #Living on the edge