getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, UBJSON, XML, YAML / msgpack.org[C++20]
https://rfl.getml.com
MIT License
1.13k stars 102 forks source link

Support for 0-copy tensors #207

Open colinator opened 1 month ago

colinator commented 1 month ago

Would it be possible to add support for tensor serialization/deserialization? i.e. things like xtensor or eigen? Ideally these would have a '0-copy' ability - you could deserialize the memory directly into the tensor... or maybe just a '0-copy bytestring'? Do byte strings work that way now? Then tensor support could be grafted independently then... right?

liuzicheng1987 commented 1 month ago

@colinator , is is possible to serialize and deserialize bytestrings using this library for formats that support this (msgpack, flexbuffers, CBOR, BSON):

https://github.com/getml/reflect-cpp/blob/main/docs/bytestring.md

You could just implement your own custom parser for your tensors:

https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md

In this case you would use using ReflType = rfl::Bytestring and then implement the to and from methods that would load the byte string from and to your tensors.

colinator commented 1 month ago

Thanks! I will give it a try.

colinator commented 4 weeks ago

Now I'm not so sure that byte strings (or deserialization/serialization in general) works with '0-copy'. Meaning, if I have some data 'in hand' from the network, for example - a sequence of bytes that represents a flex-buffer-encoded thing, I want to get a structure such that accessing a variable (or calling a function to get something) from the structure points into the flex buffer-encoded data, without copying the data. In this way I can get true '0-copy': I can turn some bytes from the network, for example, into a 'live' struct with no copying. Maybe I'm missing how to do it...