I wanted to create a

faster Serializer

Mathias Arlaud

@matarld

Mathias Arlaud

@matarld
mtarld
@bakslahHQ

Thanks!

Mathias Arlaud

@matarld

@matarld

Figure 1. A regular AFUP Day conference room

@AFUP_Lyon

Wow!

This is

so nice

Figure 2. A conference room full of Mathias

Hello?

@matarld

@AFUP_Lyon

Figure 3. Where to find me during conferences

@matarld

@AFUP_Lyon

Try $i++

instead

Figure 4. Last two years of my free time
$i++

@matarld

@AFUP_Lyon

Figure 5. Expected vs actual project trajectory

a faster serializer

@matarld

@AFUP_Lyon

Representing data structures in a format that can be sent or persisted in order to be reconstructed later

Binary, textual

Construction pattern

Databases, flat files, APIs

Anywhere, interoperable

Serialization?

@matarld

@AFUP_Lyon

https://symfony.com/doc/current/components/serializer.html

Symfony's serializer

@matarld

@AFUP_Lyon

Symfony's serializer

{
  "name":"Axel",
  "color":"Red",
  "age":2
}
array(3) {
  ["name"]=> string(4) "Axel"
  ["color"]=> string(3) "Red"
  ["age"]=> int(2)
}
object(Cat)#1 (3) {
  ["name"]=> string(4) "Axel"
  ["color"]=> string(3) "Red"
  ["age"]=> int(2)
}

@matarld

@AFUP_Lyon

 Metadata is data

Metadata

, but about data

@matarld

@AFUP_Lyon

data

metadata

"thing"
"animal"
"cat"
"flying cat"

Metadata

@matarld

@AFUP_Lyon

data

/** @temlate T of string */
final class Rain {
  /** @return list<T> */
  public function content(): array {...}
}

/** @var Rain<'cat'|'dog'> $*/
$englishRain = new Rain();

$fallingStuff = $englishRain->content();

type

no idea
array of something
list of something
list of cats and dogs

Possible JSON

[
  [0.7]
]
{
  "c": 1
}

true

{
  "a": 1
}
[
  3.14
]
[
  "pi"
]
[
  true
]
[
  3.14
]
[
  "dog"
]
[
  "cat"
]
[
  "dog"
]
[
  "dog"
]

The Type metadata

@matarld

@AFUP_Lyon

interface PropertyTypeExtractorInterface
{
    /**
     * @return Type[]|null
     */
    public function getTypes(...): ?array;
}

The Type metadata

@matarld

@AFUP_Lyon

interface PropertyTypeExtractorInterface
{
    /**
     * @return Type[]|null
     */
    public function getTypes(...): ?array;
}

only properties

no union/intersection difference

no list/dict difference

no generics

...

The Type limitation

@matarld

@AFUP_Lyon

Figure 6. Forum PHP 2023 unequivocal evidence

@matarld

@AFUP_Lyon

TypeInfo component

@matarld

@AFUP_Lyon

ft. @Korbeil_

TypeInfo cool stuff

/**
 * @template T of Animal
 */
final class FlyingAnimal
{
  public function __construct(
    public object $type,
    private int $speed,
  ) {
  }
  
  /**
   * @return class-string<T>
   */
  public function getTypeClass(): string
  {
    return $this->type::class;
  }
  
  public function setSpeed(int $speed): void
  {
  	$this->speed = $speed;
  }
}

PropertyInfo

TypeInfo

@matarld

@AFUP_Lyon

TypeInfo cool stuff

@matarld

@AFUP_Lyon

TypeInfo cool stuff

$resolver = TypeResolver::create();

$resolver->resolve($reflectionClass->getProperty('isFlying'));
$resolver->resolve('list<int>');

@matarld

@AFUP_Lyon

TypeInfo cool stuff

Type::nullable(Type::list(Type::int()));

Type::intersection(
  Type::object(\Stringable::class),
  Type::object(\Traversable::class),
);

Type::generic(
  Type::object(Collection::class),
  Type::object(User::class),
);

@matarld

@AFUP_Lyon

Contribute to TypeInfo!

@matarld

@AFUP_Lyon

Design & BC limitations

AbstractNormalizer

allows attributes

instantiates objects

AbstractObjectNormalizer

normalization

denormalization

validation

ObjectNormalizer

extracts attributes

BC policy

@matarld

@AFUP_Lyon

🐌

Reflection is slow

Improved over PHP versions, but still...

Lot of caching to improve things

The Reflection limitation

@matarld

@AFUP_Lyon

The Memory limitation

Huge collection of objects

Super big array

array

@matarld

@AFUP_Lyon

Json streaming

Huge collection of objects

array

@matarld

@AFUP_Lyon

Figure 7. API Platform conference 2023 evidence

@matarld

@AFUP_Lyon

JsonEncoder component

@matarld

@AFUP_Lyon

JsonEncoder component

PHP

metadata

Cat::class
class Cat
{
  public string $name;
  public bool $flying;
}

// ...

$this->jsonEncoder->encode($cat);

Possible JSON

{
  "name": "any_string",
  "flying: true|false
}

@matarld

@AFUP_Lyon

Encoder

Possible JSON

[  
  {
    "name": "any_string",
    "flying: true|false
  },
  {
    "name": "any_string",
    "flying: true|false
  }
]
return static function ($cats, $stream, $config) {
  $stream->write('[');
  $prefix_0 = '';
  foreach ($cats as $cat) {
    $stream->write($prefix_0);
    $stream->write('{"name":');
    $stream->write(json_encode($cat->name));
    $stream->write(',"flying":');
    $stream->write($cat->flying ? 'true' : 'false');
    $stream->write('}');
    $prefix_0 = ',';
  }
  $stream->write(']');
};

JsonEncoder component

@matarld

@AFUP_Lyon

I want to serialize a cat!

Does an encoder exist?

    /\_____/\
   /  o   o  \
  ( ==  ^  == )
   )         (
  (           )
 ( (  )   (  ) )
(__(__)___(__)__)

Encode the cat

Yep 👌

Compute the cat JSON shape

Generate and store the encoder

No

JsonEncoder component

@matarld

@AFUP_Lyon

Read only needed

JSON part lazily

JsonEncoder pros and cons

Blazing fast!

Flat memory usage

Stream ready

Generics ready

Simple API

Edit data on-the-fly

   __
o-''|\_____/)
 \_/|_)     )
    \  __  /
    (_/ (_/  

@matarld

@AFUP_Lyon

composer req jolicode/automapper

Mapper

   __
o-''|\_____/)
 \_/|_)     )
    \  __  /
    (_/ (_/  

AutoMapper

@matarld

@AFUP_Lyon

use AutoMapper\Attribute as Mapper;

final class Cat
{
  #[MapTo(
    target: Dog::class,
    property: 'barkVolume',
  )]
  public int $meowVolume;
}

final class Dog
{
  public int $barkVolume;
}

Renaming properties

@matarld

@AFUP_Lyon

barkVolume
meowVolume
use AutoMapper\Attribute as Mapper;

final class Cat
{
  #[MapTo(
    target: Dog::class,
    transformer: [self::class, 'toDogAge'],
  )]
  public int $age;
  
  public static function toDogAge(int $value, Source $source, array $context): int
  {
    return floor($value * 1.3);
  }
}

Updating values

@matarld

@AFUP_Lyon

final class Cat
{
  #[MapTo(
    target: Dog::class,
    transformer: CatToDogAgeTransformer::class,
  )]
  public int $age;
}

class CatToDogAgeTransformer implements PropertyTransformerInterface
{
  public function __construct(
    private AgeConverter $ageConverter,
  ) {}

  public function transform(int $value, object|array $source, array $context): mixed
  {
    return $this->ageConverter->fromCatToDog($value);
  }
}

@matarld

@AFUP_Lyon

Updating values

Benchmark

Deserialization

Serializer

JsonEncoder

JsonEncoder + Automapper

@matarld

@AFUP_Lyon

JsonEncoder

Serializer

JsonEncoder + Automapper

Serialization

Thanks (again)!

Mathias Arlaud

@matarld

👍

👎

I want to convert a cat to a dog!

Does a mapper exist?

   __
o-''|\_____/)
 \_/|_)     )
    \  __  /
    (_/ (_/  

Convert the cat

Yep 👌

Fetch the metadata

Generate and store the mapper

No

@matarld

@AFUP_Lyon

Performances

use Symfony\Component\Serializer\SerializerInterface;

final readonly class MyService
{
  public function __construct(
    private SerializerInterface $serializer,
  ) {
  }
}

The Serializer

@matarld

@AFUP_Lyon

The TurboSerializer

// config/bundles.php

return [
  // ...
  Mtarld\JsonEncoderBundle\JsonEncoderBundle::class => ['all' => true],
  AutoMapper\Bundle\AutoMapperBundle::class => ['all' => true],
  TurboSerializer\TurboSerializerBundle::class => ['all' => true],
];
composer require korbeil/turbo-serializer

@matarld

@AFUP_Lyon

The TurboSerializer

final readonly class ApiController
{
  public function __construct(
    private SerializerInterface $turboSerializer,
  ) {
  }

  #[Route('/api/cat-but-dogs')]
  public function get(): Response
  {
    $cats = $this->callCats();
    
    return new Response($this->turboSerializer->serialize(
      $cats, 
      'json',
      [Serializer::NORMALIZED_TYPE => Type::list(Type::object(Dog::class))]
    ));
  }
}

@matarld

@AFUP_Lyon

[AFUP Day] Rendre le Serializer plus rapide

By Mathias Arlaud

[AFUP Day] Rendre le Serializer plus rapide

  • 639