laurencelundblade / QCBOR

Comprehensive, powerful, commercial-quality CBOR encoder/ decoder that is still suited for small devices.
Other
181 stars 47 forks source link

Provide an encoder interface for known-size arrays and maps #241

Open BrianSipos opened 2 months ago

BrianSipos commented 2 months ago

The current interface with paired functions like QCBOREncode_OpenArray() and QCBOREncode_CloseArray() use a late insertion of the array head during close when the size of the array is known by the encoder. This requires the use of memory insertion and memmove() though.

In cases where the size of an definite-length array or map is known by the application before opening it, could there be a public function to write the size immediately at open?

Something like

void QCBOREncode_OpenArrayDefiniteLength(QCBOREncodeContext *pMe, size_t length);

This wouldn't be a change in capability of the encoder, just one of efficiency if the length is known at time of opening. The existing bookkeeping of number of items actually encoded could still be checked against this definite length so that an error occurs at CloseArray if the actual differed from the open length. Because it's not a difference of capability this is just a wishlist request.

laurencelundblade commented 2 months ago

Hi Brian, it's a good idea and happy to have it on the list. May take a while to get to it.

Do you have any specific issues with the memmove? For example, is it a performance issue with really large CBOR structures?

It could also be a part of make the object code of the encoder smaller.