WXF is a fully documented binary encoding for Wolfram Language expressions. WRI has already published a Python encoder/decoder for this format. ExternalEvaluate
also uses it.
Has anyone developed a WXF encoder for C++?
Unlike in Python, in C++ there's no standard set of data structures to use to represent a WL expression. Therefore I expect that eventually there will be several C++ WXF libraries, backed by different data structures and intended for different uses.
The specific use case I have in mind here is returning complex results from LibraryFunctions that cannot be simply represented as an MTensor. The typical solution for this is using MathLink. I suspect that encoding those results to WXF, transferring them as a ByteArray, then decoding with BinaryDeserialize
will be faster, in some cases much faster, than using MathLink. This suspicion is based on noticing that sometimes, even encoding to JSON and transferring as a string can be faster than using MathLink.
Answer
We have developed a C library to support BinarySerialize / BinaryDeserialize. I confirm that we plan to open-source it as well.
The library is built around a struct, called WXFExpr, that can represent any WXF types. It decodes a stream of WXF bytes into a stream of WXFExpr, which can be seen as an intermediary representation, ensuring the encoding is consistent with WXF specifications.
It's then up to the caller of the library to turn these WXFExpr into something meaningful. That's exactly what the Wolfram Kernel does for BinaryDeserialize.
Similarly during serialization the library turns WXFExpr into a consistent stream of WXF bytes. The caller fills the WXFExpr with relevant data.
Comments
Post a Comment