# \[Future\] Zero-cost serialization constraint

**URL:** <https://forum.opencyphal.org/t/future-zero-cost-serialization-constraint/469>\
**Category:** Development & Maintenance\
**Created:** [May 5, 2019, 5:10pm UTC](https://forum.opencyphal.org/t/future-zero-cost-serialization-constraint/469 "2019-05-05T17:10:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavel.kirienko](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/pavel.kirienko/32/9_2.png) [@pavel.kirienko](https://forum.opencyphal.org/u/pavel.kirienko)\
**Post date:** [May 5, 2019, 5:10pm UTC](https://forum.opencyphal.org/t/future-zero-cost-serialization-constraint/469/1 "2019-05-05T17:10:16Z")

</div>

Perhaps at some point in the future we should consider adding a new directive that would instruct the DSDL processing tool that the definition where the directive is located should adhere to a _conventional layout_. It could be called `@zerocost`.

A data schema is of a “conventional layout” if:

- It is not a tagged union.
- Each field is of a standard bit length (8, 16, 32, 64).
- The offset of each field is an integer multiple of the size of the field. In other words, in a data schema that is of a conventional layout the natural alignment requirement of each field is satisfied. Example:

```python
@zerocost
uint64 foo
int8 bar
uint8 baz
float16 boo
float32[9] zoo

```

The idea of conventional layout schemas is that on a typical little-endian machine their native memory layout will match their serialized representation, enabling zero-cost serialization via naive `memcpy()`. One can define zero-cost types without any explicit support from DSDL, of course; the only reason for having the new directive is to let the DSDL front-end catch data types that claim to be zero-cost while not actually being so. Example:

```python
@zerocost
uint64 foo
int8 bar
float16 boo # Oops! Bad alignment - not zero-cost.
uint8 baz
float32[9] zoo

```

The described feature does not seem to be compatible with variable-length entities, though, meaning that anything that contains a variable-length array is not zero-cost.

---

<div class="post-metadata">

**Author:** ![Zarkopafilis](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/zarkopafilis/32/228_2.png) [@Zarkopafilis](https://forum.opencyphal.org/u/Zarkopafilis)\
**Post date:** [May 5, 2019, 9:50pm UTC](https://forum.opencyphal.org/t/future-zero-cost-serialization-constraint/469/2 "2019-05-05T21:50:37Z")

</div>

And maybe some dsdl compiler auto detection which provides hints on possible zero cost initialization (much later).
