New in Symfony 4.1: Faster serializer

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.

Comments

Great!!
Up to 40% faster overall response time or 40% faster in time spent serializing?
@Dave the benchmark is for the entire application, not just the serializing part. You can find the details here: https://github.com/symfony/symfony/pull/27049#issuecomment-384995241
This article needs proofreading. There are multiple problems:

- 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
@Teoh thanks for proofreading the article. Everything should be fixed now.
Great news ! Thanks for you work guys.
Nice!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.