msgpack / msgpack

MessagePack is an extremely efficient object serialization library. It's like JSON, but very fast and small.

Home Page:http://msgpack.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enable padding

pohlt opened this issue · comments

I have a use case where I send a few values and a chuck of bytes to JavaScript using msgpack. Most libraries (including msgpackr) try to unpack the data with as little copying as possible. Therefore, the chunk of bytes is unpacked as a Uint8Array which actually is just a view of the entire message with an offset (I could ask msgpackr to copy the data into a newly allocated array, but I try to avoid copying).

The chuck of bytes are actually 16-bit values, so I want convert it (still using the same original data) using new Uint16Array(msg.data.buffer, msg.data.byteOffset).

However, this doesn't work for my message, because the offset happens to be odd in this specific message and JS requires it to be divisible by 2 (as I'm asking for 16-bit values). I manually introduced new items in my dictionary where I tweaked the string lengths to shift the chunk of bytes to an appropriate alignment, but that feels wrong.

Does the msgpack format have something like a padding value (ideally a single byte) which could by used by packers to shift data to preferred byte alignments?

I know that msgpack is all about the smallest message size, but being able to add a single byte (or two or three) could save you a lot of copying on the receiver side.

No official padding support but you could misuse Nil (0xc0) for that purpose.
Be sure to document it well if any 3rd party is to be consuming or providing the content :-)

Wouldn't a Nil (0xc0) be interpreted as a regular none/null/None/nil value in an array or map?

How about 0xc1? To make it work, msgpack would need to specify that it has to be ignored independent of its context. Is that realistic?