[PATCH] mbuf: optimize detach direct buffer

Morten Brørup mb at smartsharesystems.com
Sat Jan 3 18:34:54 CET 2026


doesn't write the fields that already have the required values. This
saves a memory store operation when all the fields already have the
required values.

This patch adds the same optimization to __rte_pktmbuf_free_direct(),
to improve the performance for freeing a direct buffer being detached from
a packet mbuf, i.e. saving a memory store operation when all the fields
(of the buffer being freed) already have the required values.

Signed-off-by: Morten Brørup <mb at smartsharesystems.com>
---
 lib/mbuf/rte_mbuf.h | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h
index 2004391f57..592af2388c 100644
--- a/lib/mbuf/rte_mbuf.h
+++ b/lib/mbuf/rte_mbuf.h
@@ -1334,17 +1334,23 @@ static inline void
 __rte_pktmbuf_free_direct(struct rte_mbuf *m)
 {
 	struct rte_mbuf *md;
+	bool refcnt_not_one;
 
 	RTE_ASSERT(RTE_MBUF_CLONED(m));
 
 	md = rte_mbuf_from_indirect(m);
 
-	if (rte_mbuf_refcnt_update(md, -1) == 0) {
-		md->next = NULL;
-		md->nb_segs = 1;
+	refcnt_not_one = unlikely(rte_mbuf_refcnt_read(md) != 1);
+	if (refcnt_not_one && __rte_mbuf_refcnt_update(md, -1) != 0)
+		return;
+
+	if (refcnt_not_one)
 		rte_mbuf_refcnt_set(md, 1);
-		rte_mbuf_raw_free(md);
-	}
+	if (md->nb_segs != 1)
+		md->nb_segs = 1;
+	if (md->next != NULL)
+		md->next = NULL;
+	rte_mbuf_raw_free(md);
 }
 
 /**
-- 
2.43.0



More information about the dev mailing list