Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support skipping default values in MessagePack serialization #912

Open
vkaverin opened this issue Oct 3, 2023 · 0 comments
Open

Support skipping default values in MessagePack serialization #912

vkaverin opened this issue Oct 3, 2023 · 0 comments

Comments

@vkaverin
Copy link

vkaverin commented Oct 3, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant