I have a function which prepares a data buffer and then sends it via an externally provided API function which looks like:
send(uint8_t* data_buf, uint32_t length)
In my particular case, my code is always sending exactly 8 bytes and the first 7 bytes are always the same (I can’t change this fact; it’s some sort of message header).
Because I am in an limited, embedded environment, I would like to optimize the size and performance of my code, or at least choose the best tradeoff of the two.
Currently, I see two options:
- Create a global array. Initialize the first 7 bytes one time and then
just overwrite the last byte before sending the array. - Create a local array, write all 8 bytes and then send it.
Are there any better solutions than the two mentioned above?
Even on an embedded system, you can count on having more than 8 bytes of cache. Hence performance really doesn’t matter.
databuf[8]will be fully in cache in both cases. The code size for the first case might be smaller by 2-3 instructions.(There’s often another issue: code/flash size and RAM size are distinct constraints, when you can XIP).