kphp icon indicating copy to clipboard operation
kphp copied to clipboard

Support skipping default values in MessagePack serialization

Open vkaverin opened this issue 2 years ago • 0 comments

Skipping default values can significantly reduce serialized message size:

_main();

function _main() {
  $with_nulls = new FooWithNulls(42);
  $without_nulls = new FooWithoutNulls(42);
  echo 'With default values: ' . strlen(instance_serialize($with_nulls)) . PHP_EOL;
  echo 'Without default values: ' . strlen(instance_serialize($without_nulls)) . PHP_EOL;
}


/** @kphp-serializable */
final class FooWithNulls {

  /** @kphp-serialized-field 1 */
  private int $x;

  /** @kphp-serialized-field 2 */
  private ?int $a = null;

  /** @kphp-serialized-field 3 */
  private ?int $b = null;

  /** @kphp-serialized-field 4 */
  private ?int $c = null;

  /** @kphp-serialized-field 5 */
  private ?int $d = null;

  /** @kphp-serialized-field 6 */
  private ?int $e = null;

  public function __construct(int $x) {
    $this->x = $x;
  }
}

/** @kphp-serializable */
final class FooWithoutNulls {
  
  /** @kphp-serialized-field 1 */
  private int $x;

  public function __construct(int $x) {
    $this->x = $x;
  }
}

Output:

With default values: 13
Without default values: 3

This should be configurable just like in JsonEncoder.

vkaverin avatar Oct 03 '23 09:10 vkaverin