nanopb / nanopb

Protocol Buffers with small code size
https://jpa.kapsi.fi/nanopb/
zlib License
4.32k stars 851 forks source link

Improve memory allocation performance #571

Open PetteriAimonen opened 4 years ago

PetteriAimonen commented 4 years ago

Currently nanopb relies on realloc() to efficiently increase size of allocated arrays, without extra copying.

For some allocators this assumption is incorrect. Most notably newlib-nano implements realloc() as malloc() + memcpy() + free().

The logic in pb_decode.c should be reworked so that it does something smart about this. If decoding from a buffer, one could look ahead to see how many items there are in array. If decoding from stream, some kind of preallocation logic should be applied.

PetteriAimonen commented 4 years ago

This might actually be quite important, considering how newlib-nano also has poor realloc() performance.