From 6211de87c0660a5dfd21b5decc93a67236ec0984 Mon Sep 17 00:00:00 2001 From: Pisit Sawangvonganan Date: Thu, 18 Jul 2024 18:44:37 +0700 Subject: [PATCH] net: mqtt: improve decoder buffer handling Improve buffer handling logic to use local variables extensively. This change reduces the number of pointer dereferences, which leads to more efficient runtime and helps reduce the code size. Signed-off-by: Pisit Sawangvonganan --- subsys/net/lib/mqtt/mqtt_decoder.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/subsys/net/lib/mqtt/mqtt_decoder.c b/subsys/net/lib/mqtt/mqtt_decoder.c index ee86d44e184964..678546b2f111ec 100644 --- a/subsys/net/lib/mqtt/mqtt_decoder.c +++ b/subsys/net/lib/mqtt/mqtt_decoder.c @@ -29,13 +29,17 @@ LOG_MODULE_REGISTER(net_mqtt_dec, CONFIG_MQTT_LOG_LEVEL); */ static int unpack_uint8(struct buf_ctx *buf, uint8_t *val) { - NET_DBG(">> cur:%p, end:%p", (void *)buf->cur, (void *)buf->end); + uint8_t *cur = buf->cur; + uint8_t *end = buf->end; + + NET_DBG(">> cur:%p, end:%p", (void *)cur, (void *)end); - if ((buf->end - buf->cur) < sizeof(uint8_t)) { + if ((end - cur) < sizeof(uint8_t)) { return -EINVAL; } - *val = *(buf->cur++); + *val = cur[0]; + buf->cur = (cur + sizeof(uint8_t)); NET_DBG("<< val:%02x", *val); @@ -55,14 +59,17 @@ static int unpack_uint8(struct buf_ctx *buf, uint8_t *val) */ static int unpack_uint16(struct buf_ctx *buf, uint16_t *val) { - NET_DBG(">> cur:%p, end:%p", (void *)buf->cur, (void *)buf->end); + uint8_t *cur = buf->cur; + uint8_t *end = buf->end; + + NET_DBG(">> cur:%p, end:%p", (void *)cur, (void *)end); - if ((buf->end - buf->cur) < sizeof(uint16_t)) { + if ((end - cur) < sizeof(uint16_t)) { return -EINVAL; } - *val = *(buf->cur++) << 8; /* MSB */ - *val |= *(buf->cur++); /* LSB */ + *val = sys_get_be16(cur); + buf->cur = (cur + sizeof(uint16_t)); NET_DBG("<< val:%04x", *val);