I am using nanopb to do logging on an embedded system: my logging messages will be .proto messages.
Speed of encoding is the most important factor; I have plenty of RAM and flash.
QUESTION
Specific to nanopb, how do I minimize encoding time?
I know of all the C optimizations I could make: inline functions, set pb_encode functions in RAM instead of flash, etc
Would it make a difference if I use all fixed-size types in my .proto file?
The easy methods I'm aware of are:
PB_BUFFER_ONLYat compile time.PB_LITTLE_ENDIAN_8BIT. It should be detected automatically on most compilers, though.These could result in speed improvements up to 2x.
It is further possible to speed up encoding by programming direct calls to the encoding functions, instead of going through the message structure and descriptor loops. I expect this to increase encoding speed up to 5x. Some knowledge of protobuf encoding spec will be needed.
For example, for this message:
you could do:
While this results in hard-coding part of the message definition, the backward- and forward compatibility of protobuf messages works as usual. The tag numbers can be accessed by the
Message_field_tagmacros that are generated by nanopb generator. The field types are defined in the C code, but they are also embedded in the encoded messages so any mismatches will be detected on the decoding side.