mlot / nanopb

Automatically exported from code.google.com/p/nanopb
zlib License
0 stars 0 forks source link

Wasted memory due to the change of bytes field datatype #134

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I can see that the bytes field datatype was changed from "pointer-type" to 
"array-type" in the version nanopb-0.2.7.
What's the benefit of this modification?

Acaully it will cause the wasted memory in embedded system, because in its 
encode function, the size parameter is still from "pb_size_t size".

#define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; uint8_t bytes[n]; }
return pb_encode_string(stream, bytes->bytes, bytes->size);

That means if user want to encode one section of binary data, it will first 
copy it to bytes[n], and then encode it.
It is same if "uint8_t bytes[n]" change to "uint8_t *bytes" since it just 
record the beginning address of the data which will be encoded. And the size of 
this section data will be given to bytes->size.
So memory of "bytes[n]" will be wasted, and the larger of parameter "n", more 
wasted memory in embedded system.

Original issue reported on code.google.com by hellowoo...@gmail.com on 22 Oct 2014 at 8:56

GoogleCodeExporter commented 9 years ago
It makes the internal logic simpler and the code smaller and faster. So it is a 
tradeoff.

The case where the data is already in memory somewhere can be solved by using a 
callback function without copying the data.

See for example 
https://code.google.com/p/nanopb/source/browse/tests/callbacks/encode_callbacks.
c#9  the pb_encode_string() can also be used for bytes type.

Original comment by Petteri.Aimonen on 22 Oct 2014 at 9:25

GoogleCodeExporter commented 9 years ago
Reviewing the differences, I must admit that the savings from the change were 
not very great in code size or efficiency. But on the other hand, there is not 
sufficient reason to change it back again now.

Hopefully the callback method is not too cumbersome to use. Passing the size of 
the memory can be somewhat annoying, though, and may require making a custom 
structure for the purpose.

Original comment by Petteri.Aimonen on 22 Dec 2014 at 6:58

GoogleCodeExporter commented 9 years ago
Thank you for your reply.
I got it.
Happy new year! :)

Original comment by hellowoo...@gmail.com on 4 Jan 2015 at 8:20