zakarumych / alkahest

Fantastic serialization library
Other
157 stars 9 forks source link

what's advantage of alkahest over raw struct? #25

Closed ssh352 closed 3 months ago

ssh352 commented 3 months ago

alkahest is very fast. What's the advantage of using alkahest over repr(C) struct? thanks!

zakarumych commented 3 months ago

There are multiple advantages.

  1. You can have unbound sequences with slice formula and serialize Vec or similar types.
  2. [repr(C)] is not enough to serialize and deserialize a type. You also need zero padding bytes and all bit patters must represent valid value.

  3. Alkahest uses #[forbid(unsafe)] protecting from errors, casting pointer to raw bytes to #[repr(C)] would require unsafe code and failure to meet requirements will cause UB.
ssh352 commented 3 months ago

There are multiple advantages.

Thank you for your detailed response. It looks like alkahest is as fast as raw struct and has many advantages.

  1. You can have unbound sequences with slice formula and serialize Vec or similar types.
  2. [repr(C)] is not enough to serialize and deserialize a type. You also need zero padding bytes and all bit patters must represent valid value.

A C++ program sends and receives varieties of raw C struct(alignment 1, zero padding) to my Rust program over UDP.

originally I planned to

However I came across the very fast alkahest crate. Is this scenario a good use case of alkahest?

  1. Alkahest uses #[forbid(unsafe)] protecting from errors, casting pointer to raw bytes to #[repr(C)] would require unsafe code and failure to meet requirements will cause UB.
zakarumych commented 3 months ago

Ideally you'd have Alkahest on C++ side as well. But I didn't make C codegen yet.

In Alkahest format of the message is not the same as memory layout of #[repr(C)] struct, so if you send #[repr(C)] struct, you have to read into #[repr(C)] struct.

If possible, use bytemuck crate and derive Pod for your structs on Rust side. Then you will be able to safely convert bytes to those structs.