Open tnovotny opened 6 years ago
You are right. In other words:
std::vector<unsigned char> data = getBytes();
ubjson::Value value;
value["data"] = data;
should be the "way" for writing binary types. Good!. My problem is reading it. The current way this library stores array is in form of:
//! A pattern type alias, i.e (std::unique_ptr<Value>)
using Uptr = std::unique_ptr<Value>;
//! An alias used to internally represent \ref Type "Array" types
using ArrayType = std::vector<Uptr>;
Which is a "holly grail" of a performance sucker. :-( . Prior to C++ 17 simply doing std::vector<Value>
would have been Undefined Behavior. Knowing such indirection exists, Making an array
of small items would have serious performance implications. However, Digging into the common implementations of loosely typed languages (JavaScript and Python) where objects of various types can be mixed in a single array, there are unavoidable performance implications.
As for reading strongly typed UBJSON array into C++, that wouldn't be much of a performance concern, however, for our Value
class, it would incur the extra cost of more "types" to check for at run-time. Something we can probably live with, and its much better than the overhead of having each array item allocated by new
.
I will need to significantly improve the library to store strongly typed arrays efficiently. Thank you for bringing this issue up.
The support for homogeneous arrays is missing. see
StreamWriter<StreamType>::append_array
I think you made this difficult because you got the binary type wrong. I.E there is not one binary type, but all the strong typed arrays are different binary types. One should be able to write something like:
and that should serialize into
Note the
[$][I]
. The type of the array should be kept as passed in so that it is received that way.