Performance improvement: replace memcpy with loop.
In the pb_istream_from_buffer and pb_ostream_from_buffer, memcpy was used to transfer values to the buffer. For the common case of count = 1-10 bytes, a simple loop is faster.
This commit is contained in:
@@ -39,11 +39,14 @@ static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
|
|||||||
static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
|
static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
|
||||||
{
|
{
|
||||||
uint8_t *source = (uint8_t*)stream->state;
|
uint8_t *source = (uint8_t*)stream->state;
|
||||||
|
stream->state = source + count;
|
||||||
|
|
||||||
if (buf != NULL)
|
if (buf != NULL)
|
||||||
memcpy(buf, source, count);
|
{
|
||||||
|
while (count--)
|
||||||
|
*buf++ = *source++;
|
||||||
|
}
|
||||||
|
|
||||||
stream->state = source + count;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,8 +37,11 @@ static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = {
|
|||||||
static bool checkreturn buf_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
|
static bool checkreturn buf_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
|
||||||
{
|
{
|
||||||
uint8_t *dest = (uint8_t*)stream->state;
|
uint8_t *dest = (uint8_t*)stream->state;
|
||||||
memcpy(dest, buf, count);
|
|
||||||
stream->state = dest + count;
|
stream->state = dest + count;
|
||||||
|
|
||||||
|
while (count--)
|
||||||
|
*dest++ = *buf++;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user