mbuf: optimize segment prefree
PATCH 1/1 mbuf: optimize segment prefree
Eefactored rte_pktmbuf_prefree_seg() for both performance and readability.
With the optimized RTE_MBUF_DIRECT() macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.
--- lib/mbuf/rte_mbuf.h | 52 ++++++++++++++++------------------------ lib/mbuf/rte_mbuf_core.h | 8 +++++++ 2 files changed, 28 insertions(+), 32 deletions(-)
lib/mbuf/rte_mbuf.h +20 -32
@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h>
@@ -1423,44 +1424,31 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one && +__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +8 -0
@@ -715,6 +715,14 @@ struct rte_mbuf_ext_shared_info {#define RTE_MBUF_DIRECT (mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +/* GCC only optimizes single-bit MSB tests this way, so do it by hand with multi-bit. */ +#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) + 7] & \ + (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> 56))) +#endif +/** Uninitialized or unspecified port. */ #define RTE_MBUF_PORT_INVALID UINT16_MAX/** For backwards compatibility. */
Signature · 1 line
2.43.0
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 724
Quoted 8 lines · On Wed, 27 Aug 2025 21:35:34 +0000 Morten Brørup <[email protected]>…
lib/mbuf/rte_mbuf_core.h +7
-0+/* GCC only optimizes single-bit MSB tests this way, so do it by hand with multi-bit. */ +#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) + 7] & \ + (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> 56))) +#endif
Complex enough expression that I would prefer this be an inline function with some more temporary variables and more comments.
Like the magic 7 for mask??
Quoted 4 lines · Stephen Hemminger
Quoted 1 line · On Wed, 27 Aug 2025 21:35:34 +0000 Morten Brørup <[email protected]>…
Quoted 10 lines
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 724
Quoted 7 lines
lib/mbuf/rte_mbuf_core.h +7
-0+/* GCC only optimizes single-bit MSB tests this way, so do it by hand with multi-bit. */ +#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) + 7] & \ + (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> 56))) +#endif Complex enough expression that I would prefer this be an inline function with some more temporary variables and more comments.
Like the magic 7 for mask??
+1
--wathsala
Quoted 15 lines
From: Wathsala Vithanage [mailto:[email protected]]
Sent: Monday, 6 October 2025 19.46Quoted 5 lines · Stephen Hemminger
Quoted 2 lines · On Wed, 27 Aug 2025 21:35:34 +0000 Morten Brørup <[email protected]>…
+/* GCC only optimizes single-bit MSB tests this way, so do it by
hand with multi-bit. */
Quoted 4 lines
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 4 lines
lib/mbuf/rte_mbuf_core.h +4
-0+#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) +7] & \
Quoted 8 lines
Quoted 3 lines
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 724
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +2
-0+ (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> 56))) +#endif Complex enough expression that I would prefer this be an inline
function
Quoted 2 lines
with some more temporary variables and more comments. Like the magic 7 for mask??
+1
So, instead of overriding the macro definition in the GCC exception case, you prefer something like:
#if defined(RTE_TOOLCHAIN_GCC) && defined(RTE_ARCH_X86)
static __rte_always_inlineRTE_MBUF_DIRECT(const struct rte_mbuf * const mb)
{...}
#else
#define RTE_MBUF_DIRECT(mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL)))
#endif
which would give us a function with a macro-like (upper case) name.
How about I just add a more detailed description to this macro? After all, formally, it's an exception to the simple default macro.
PING for review.
Only received feedback from Stephen about the GCC optimized RTE_MBUF_DIRECT() macro being complex, and could be an inline function instead.
Venlig hilsen / Kind regards,
-Morten Brørup
Quoted 120 lines
-----Original Message-----
From: Morten Brørup [mailto:[email protected]]
Sent: Wednesday, 27 August 2025 23.36
To: [email protected]
Cc: Morten Brørup
Subject: [PATCH] mbuf: optimize segment prefreeEefactored
rte_pktmbuf_prefree_seg()for both performance and readability.With the optimized
RTE_MBUF_DIRECT()macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.Signed-off-by Morten Brørup <[email protected]>--- lib/mbuf/rte_mbuf.h | 52 ++++++++++++++++------------------------ lib/mbuf/rte_mbuf_core.h | 8 +++++++ 2 files changed, 28 insertions(+), 32 deletions(-)lib/mbuf/rte_mbuf.h +20
-32@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h> @@ -1423,44 +1424,31 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one && +__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +1
-0@@ -715,6 +715,14 @@ struct rte_mbuf_ext_shared_info {#define RTE_MBUF_DIRECT (mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +/* GCC only optimizes single-bit MSB tests this way, so do it by hand with multi-bit. */
lib/mbuf/rte_mbuf_core.h +4
-0+#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) +7] & \
lib/mbuf/rte_mbuf_core.h +3
-1+ (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> 56))) +#endif +/** Uninitialized or unspecified port. */ #define RTE_MBUF_PORT_INVALID UINT16_MAX/** For backwards compatibility. */ --2.43.0
PATCH v2 1/1 mbuf: optimize segment prefree
In reply to Morten BrørupRefactored rte_pktmbuf_prefree_seg() for both performance and readability.
With the optimized RTE_MBUF_DIRECT() macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.
---
v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimized RTE_MBUF_DIRECT() macro. (Stephen Hemminger)
* Added static_assert() to verify that the optimized RTE_MBUF_DIRECT() macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.
--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 27 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 32 deletions(-)
lib/mbuf/rte_mbuf.h +19 -32
@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h>
@@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +27 -0
@@ -715,6 +715,33 @@ struct rte_mbuf_ext_shared_info {#define RTE_MBUF_DIRECT (mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +/* Optimization for code size. +* GCC only optimizes single-bit MSB tests this way, so we do it by hand with multi-bit. +* +* The flags RTE_MBUF_F_INDIRECT and RTE_MBUF_F_EXTERNAL are both in the MSB of the +* 64-bit ol_flags field, so we only compare this one byte instead of all 64 bits. +* On little endian architecture, the MSB of a 64-bit integer is at byte offest 7. +* +* Note: Tested using GCC version 16.0.0 20251019 (experimental). +* +* Without this optimization, GCC generates 17 bytes of instructions: +* movabs rax,0x6000000000000000 // 10 bytes +* and rax,QWORD PTR [rdi+0x18] // 4 bytes +* sete al // 3 bytes +* With this optimization, GCC generates only 7 bytes of instructions: +* test BYTE PTR [rdi+0x1f],0x60 // 4 bytes +* sete al // 3 bytes +*/ +#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) + 7] & \ + (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 * 8)))) +static_assert (((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ) >> (7 *8 )) << (7 *8 ) == + (RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ), +"RTE_MBUF_F_INDIRECT and/or RTE_MBUF_F_EXTERNAL are not in MSB." ); +#endif +/** Uninitialized or unspecified port. */ #define RTE_MBUF_PORT_INVALID UINT16_MAX/** For backwards compatibility. */
Signature · 1 line
2.43.0
Quoted 137 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance and readability.With the optimized
RTE_MBUF_DIRECT()macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.Signed-off-by Morten Brørup <[email protected]>---
v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimizedRTE_MBUF_DIRECT()macro. (Stephen Hemminger)
* Addedstatic_assert()to verify that the optimizedRTE_MBUF_DIRECT()macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 27 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 32 deletions(-)lib/mbuf/rte_mbuf.h +19
-32@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h> @@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +3
-0@@ -715,6 +715,33 @@ struct rte_mbuf_ext_shared_info {#define RTE_MBUF_DIRECT (mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +/* Optimization for code size. +* GCC only optimizes single-bit MSB tests this way, so we do it by hand with multi-bit.
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare both in the MSB of the
+ * 64-bitol_flagsfield, so we only compare this one byte instead of all 64 bits.
+ * On little endian architecture, the MSB of a 64-bit integer is at byte offest 7.
+ *
+ * Note: Tested using GCC version 16.0.0 20251019 (experimental).
+ *
+ * Without this optimization, GCC generates 17 bytes of instructions:+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * With this optimization, GCC generates only 7 bytes of instructions: + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
lib/mbuf/rte_mbuf_core.h +4
-0+#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) + 7] & \ + (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *8))))
+static_assert(((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL) >> (7 * 8)) << (7 * 8) ==lib/mbuf/rte_mbuf_core.h +2
-0+ (RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ), + "RTE_MBUF_F_INDIRECT and/or RTE_MBUF_F_EXTERNAL are not inMSB.");
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +2
-1+#endif +/** Uninitialized or unspecified port. */ #define RTE_MBUF_PORT_INVALID UINT16_MAX/** For backwards compatibility. */ --
Quoted 1 line
2.43.0
Quoted 4 lines · Morten Brørup
Refactored
rte_pktmbuf_prefree_seg()for both performance and readability.With the optimized
RTE_MBUF_DIRECT()macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.
Learned a lot, thanks
Quoted 2 lines
Signed-off-by Morten Brørup <[email protected]>
Quoted 6 lines · Morten Brørup
Refactored
rte_pktmbuf_prefree_seg()for both performance and readability.With the optimized
RTE_MBUF_DIRECT()macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.Signed-off-by Morten Brørup <[email protected]>
Comments inline below.
Quoted 78 lines
---
v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimizedRTE_MBUF_DIRECT()macro. (Stephen Hemminger)
* Addedstatic_assert()to verify that the optimizedRTE_MBUF_DIRECT()macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 27 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 32 deletions(-)lib/mbuf/rte_mbuf.h +15
-29@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h> @@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 )
lib/mbuf/rte_mbuf.h static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) · line 1487
Quoted 10 lines
lib/mbuf/rte_mbuf.h +4
-3+ m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }
Nice refactor, much more readable, thanks.
Quoted 26 lines
lib/mbuf/rte_mbuf.h +0
-0/** lib/mbuf/rte_mbuf_core.h +17
-0@@ -715,6 +715,33 @@ struct rte_mbuf_ext_shared_info {#define RTE_MBUF_DIRECT (mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +/* Optimization for code size. +* GCC only optimizes single-bit MSB tests this way, so we do it by hand with multi-bit. +* +* The flags RTE_MBUF_F_INDIRECT and RTE_MBUF_F_EXTERNAL are both in the MSB of the +* 64-bit ol_flags field, so we only compare this one byte instead of all 64 bits. +* On little endian architecture, the MSB of a 64-bit integer is at byte offest 7. +* +* Note: Tested using GCC version 16.0.0 20251019 (experimental). +* +* Without this optimization, GCC generates 17 bytes of instructions: +* movabs rax,0x6000000000000000 // 10 bytes +* and rax,QWORD PTR [rdi+0x18] // 4 bytes +* sete al // 3 bytes +* With this optimization, GCC generates only 7 bytes of instructions: +* test BYTE PTR [rdi+0x1f],0x60 // 4 bytes +* sete al // 3 bytes
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 744
Quoted 10 lines
lib/mbuf/rte_mbuf_core.h +10
-0+*/ +#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) + 7] & \ + (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 * 8)))) +static_assert (((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ) >> (7 *8 )) << (7 *8 ) == + (RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ), +"RTE_MBUF_F_INDIRECT and/or RTE_MBUF_F_EXTERNAL are not in MSB." ); +#endif +
Couple of comments/thoughts/questions here.
* This looks like a compiler limitation that should be fixed in GCC. IF we put this optimization in, how will we know when/if we can remove it again in future? I'm not sure we want this hanging around forever.
* Can the static_assert - which just checks flags are in the MSB - be
* simplified to e.g.
"((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) << CHAR_BIT) == 0" or "__builtin_ctzll(...) > (7 * CHAR_BIT)"
* As in prev bullet, I tend to prefer use of CHAR_BIT over hard-coded 8.
* Is it necessary to limit this to just GCC and x86? If it leads to the best code on x86, why not include for all compilers? What about non-x86 LE platforms?
* Does the actual macro need to be that long and complex? If we simplify a bit, does the compiler go back to generating bad code? For example:
using "(mb->ol_flags >> 56) & ((RTE_MBUF_F_INDIRECT | ..) >> 56)"
* If the above is true, do we need to actually put this in in assembler to guarantee compiler generates good code in all situations?
Quoted 16 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 22 October 2025 11.08Quoted 1 line · Morten Brørup
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likely codepath
Quoted 1 line
now fits within one instruction cache line on x86-64 when built with
GCC.
Quoted 2 lines
Signed-off-by Morten Brørup <[email protected]>Reviewed-by Bruce Richardson <[email protected]>
[...]
Quoted 42 lines
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
#define
RTE_MBUF_DIRECT(mb)\lib/mbuf/rte_mbuf_core.h +3
-0(!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +/* Optimization for code size. +* GCC only optimizes single-bit MSB tests this way, so we do it by hand with multi-bit.
Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare both inthe MSB of the
Quoted 1 line
+ * 64-bit
ol_flagsfield, so we only compare this one byte insteadof all 64 bits.
Quoted 1 line
+ * On little endian architecture, the MSB of a 64-bit integer is at
byte offest 7.
Quoted 4 lines
+ *
+ * Note: Tested using GCC version 16.0.0 20251019 (experimental).
+ *
+ * Without this optimization, GCC generates 17 bytes ofinstructions:
Quoted 4 lines
+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes+ * With this optimization, GCC generates only 7 bytes of
instructions:
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
+ * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
lib/mbuf/rte_mbuf_core.h +3
-0+#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) +7] & \
Review
Quoted 1 line
(quoted diff) +1
-0+ (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *8))))
Quoted 1 line
+static_assert(((RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL) >> (7 *8)) << (7 * 8) ==
Review
Quoted 2 lines
(quoted diff) +2
-0+ (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), + "RTE_MBUF_F_INDIRECT and/or RTE_MBUF_F_EXTERNAL are not inMSB.");
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 744
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +2
-0+#endif +Couple of comments/thoughts/questions here.
Quoted 5 lines
* This looks like a compiler limitation that should be fixed in GCC. IF we
put this optimization in, how will we know when/if we can remove it
again
in future? I'm not sure we want this hanging around forever.
Agree.
There are plenty of hand crafted optimizations in DPDK, which are already obsolete; it seems no one has found a good way of identifying them. Including myself.
Quoted 6 lines
* Can the
static_assert- which just checks flags are in the MSB - be
* simplified to e.g.
"((RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL) <<CHAR_BIT) == 0" or "__builtin_ctzll(...)> (7 *CHAR_BIT)"
* As in prev bullet, I tend to prefer use ofCHAR_BITover hard-coded 8.
In v3, I have simplified both the static_assert and the optimized macro as you suggested on Slack, with some minor improvements.
Quoted 4 lines
* Is it necessary to limit this to just GCC and x86? If it leads to the best code on x86, why not include for all compilers? What about non-
x86
LE platforms?
I had already tested ARM64, where it didn't make a difference; now I have added a note about it. I also tested ARM32, which doesn't benefit either, but I didn't add a note about it. I also tested Loongarch (on Godbolt), which does benefit from it, so I added it.
Now, as I'm writing this email, Godbolt shows that RISC-V and POWER could also benefit. Maybe we should just replace the standard macro with the optimized macro. WDYT?
Quoted 4 lines
* Does the actual macro need to be that long and complex? If we simplify a
bit, does the compiler go back to generating bad code? For example:
using "(mb->ol_flags>> 56) & ((RTE_MBUF_F_INDIRECT| ..) >> 56)"
Simplified in v3.
Quoted 3 lines
* If the above is true, do we need to actually put this in in assembler to
guarantee compiler generates good code in all situations?
No need for assembler.
Quoted 86 lines · Morten Brørup
Quoted 16 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 22 October 2025 11.08Quoted 1 line · Morten Brørup
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likely codepath
Quoted 1 line
now fits within one instruction cache line on x86-64 when built with
GCC.
Quoted 2 lines
Signed-off-by Morten Brørup <[email protected]>Reviewed-by Bruce Richardson <[email protected]>[...]
Quoted 42 lines
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
#define
RTE_MBUF_DIRECT(mb)\lib/mbuf/rte_mbuf_core.h +3
-0(!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +/* Optimization for code size. +* GCC only optimizes single-bit MSB tests this way, so we do it by hand with multi-bit.
Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare both inthe MSB of the
Quoted 1 line
+ * 64-bit
ol_flagsfield, so we only compare this one byte insteadof all 64 bits.
Quoted 1 line
+ * On little endian architecture, the MSB of a 64-bit integer is at
byte offest 7.
Quoted 4 lines
+ *
+ * Note: Tested using GCC version 16.0.0 20251019 (experimental).
+ *
+ * Without this optimization, GCC generates 17 bytes ofinstructions:
Quoted 4 lines
+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes+ * With this optimization, GCC generates only 7 bytes of
instructions:
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
+ * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
lib/mbuf/rte_mbuf_core.h +3
-0+#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf, ol_flags) +7] & \
Review
Quoted 1 line
(quoted diff) +1
-0+ (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *8))))
Quoted 1 line
+static_assert(((RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL) >> (7 *8)) << (7 * 8) ==
Review
Quoted 2 lines
(quoted diff) +2
-0+ (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), + "RTE_MBUF_F_INDIRECT and/or RTE_MBUF_F_EXTERNAL are not inMSB.");
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 744
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +2
-0+#endif +Couple of comments/thoughts/questions here.
Quoted 5 lines
* This looks like a compiler limitation that should be fixed in GCC. IF we
put this optimization in, how will we know when/if we can remove it
again
in future? I'm not sure we want this hanging around forever.Agree.
There are plenty of hand crafted optimizations in DPDK, which are already obsolete; it seems no one has found a good way of identifying them. Including myself.Quoted 6 lines
* Can the
static_assert- which just checks flags are in the MSB - be
* simplified to e.g.
"((RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL) <<CHAR_BIT) == 0" or "__builtin_ctzll(...)> (7 *CHAR_BIT)"
* As in prev bullet, I tend to prefer use ofCHAR_BITover hard-coded 8.In v3, I have simplified both the
static_assertand the optimized macro as you suggested on Slack, with some minor improvements.Quoted 4 lines
* Is it necessary to limit this to just GCC and x86? If it leads to the best code on x86, why not include for all compilers? What about non-
x86
LE platforms?
Quoted 7 lines
I had already tested ARM64, where it didn't make a difference; now I have added a note about it. I also tested ARM32, which doesn't benefit either, but I didn't add a note about it. I also tested Loongarch (on Godbolt), which does benefit from it, so I added it.
Now, as I'm writing this email, Godbolt shows that RISC-V and POWER could also benefit. Maybe we should just replace the standard macro with the optimized macro. WDYT?
I think that's not a bad idea. At least everything would be consistent.
/Bruce
Quoted 115 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 22 October 2025 16.12Quoted 8 lines · Morten Brørup
Quoted 8 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 22 October 2025 11.08Quoted 1 line · Morten Brørup
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likelycode
Quoted 2 lines
Quoted 2 lines
path
Quoted 1 line
now fits within one instruction cache line on x86-64 when built
with
Quoted 11 lines
Quoted 6 lines
GCC.
Quoted 2 lines
Signed-off-by Morten Brørup <[email protected]>Reviewed-by Bruce Richardson <[email protected]>[...]
Quoted 2 lines
Quoted 2 lines
#define
RTE_MBUF_DIRECT(mb)\
(!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|
RTE_MBUF_F_EXTERNAL)))Quoted 4 lines
Quoted 4 lines
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 4 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#if defined(RTE_TOOLCHAIN_GCC ) && defined(RTE_ARCH_X86 ) +/* Optimization for code size. +* GCC only optimizes single-bit MSB tests this way, so we do it by
Quoted 3 lines
Quoted 3 lines
hand with multi-bit.
Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALareboth in
Quoted 2 lines
Quoted 2 lines
the MSB of the
Quoted 1 line
+ * 64-bit
ol_flagsfield, so we only compare this one byteinstead
Quoted 2 lines
Quoted 2 lines
of all 64 bits.
Quoted 1 line
+ * On little endian architecture, the MSB of a 64-bit integer is
at
Quoted 3 lines
Quoted 3 lines
byte offest 7.
Quoted 2 lines
+ *
+ * Note: Tested using GCC version 16.0.0 20251019(experimental).
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
+ *
+ * Without this optimization, GCC generates 17 bytes ofinstructions:
Quoted 4 lines
+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes+ * With this optimization, GCC generates only 7 bytes of
Quoted 7 lines
Quoted 7 lines
instructions:
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
+ * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
lib/mbuf/rte_mbuf_core.h +3
-0+#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) \ + (!(((const uint8_t *)(mb))[offsetof(struct rte_mbuf,
ol_flags) +Quoted 2 lines
Quoted 2 lines
7] & \
Review
Quoted 1 line
(quoted diff) +1
-0+ (uint8_t)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7*
Quoted 2 lines
Quoted 2 lines
8))))
Quoted 1 line
+static_assert(((RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL) >> (7*
Quoted 9 lines
Quoted 9 lines
8)) << (7 * 8) ==
Review
Quoted 2 lines
(quoted diff) +2
-0+ (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), + "RTE_MBUF_F_INDIRECT and/or RTE_MBUF_F_EXTERNAL are not inMSB.");
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 744
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +2
-0+#endif +Couple of comments/thoughts/questions here.
* This looks like a compiler limitation that should be fixed in
GCC. IF
Quoted 2 lines
Quoted 2 lines
we
put this optimization in, how will we know when/if we can removeit
Quoted 5 lines
Quoted 2 lines
again
in future? I'm not sure we want this hanging around forever.Agree.
There are plenty of hand crafted optimizations in DPDK, which arealready obsolete;
Quoted 1 line
it seems no one has found a good way of identifying them. Including
myself.
Quoted 2 lines
Quoted 2 lines
* Can the
static_assert- which just checks flags are in the MSB -be
Quoted 4 lines
Quoted 4 lines
* simplified to e.g.
"((RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL) <<CHAR_BIT) == 0" or "__builtin_ctzll(...)> (7 *CHAR_BIT)"
* As in prev bullet, I tend to prefer use ofCHAR_BITover hard-coded
Quoted 3 lines
Quoted 1 line
8.
In v3, I have simplified both the
static_assertand the optimizedmacro as you suggested on Slack,
Quoted 3 lines
with some minor improvements.
Quoted 1 line
* Is it necessary to limit this to just GCC and x86? If it leads to
the
Quoted 1 line
Quoted 1 line
best code on x86, why not include for all compilers? What about
non-
Quoted 4 lines
Quoted 2 lines
x86
LE platforms?I had already tested ARM64, where it didn't make a difference; now I
have added a note about it.
Quoted 1 line
I also tested ARM32, which doesn't benefit either, but I didn't add a
note about it.
Quoted 1 line
I also tested Loongarch (on Godbolt), which does benefit from it, so
Quoted 8 lines
I added it.
Quoted 2 lines
Now, as I'm writing this email, Godbolt shows that RISC-V and POWER
could also benefit.
Quoted 1 line
Maybe we should just replace the standard macro with the optimized
macro. WDYT?
Quoted 1 line
I think that's not a bad idea. At least everything would be consistent.
I'll post a v4. Then we can go back to v3 if it looks too weird.
Quoted 2 lines
/Bruce
PATCH v3 1/1 mbuf: optimize segment prefree
In reply to Morten BrørupRefactored rte_pktmbuf_prefree_seg() for both performance and readability.
With the optimized RTE_MBUF_DIRECT() macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.
---
v3:
* Rewrote the optimized RTE_MBUF_DIRECT() macro for readability; use numerical value instead of long names. (Bruce Richardson)
* Enabled the optimized RTE_MBUF_DIRECT() macro for Loongarch too. v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimized RTE_MBUF_DIRECT() macro. (Stephen Hemminger)
* Added static_assert() to verify that the optimized RTE_MBUF_DIRECT() macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.
--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 40 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 32 deletions(-)
lib/mbuf/rte_mbuf.h +19 -32
@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h>
@@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +40 -0
@@ -715,6 +715,46 @@ struct rte_mbuf_ext_shared_info {#define RTE_MBUF_DIRECT (mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +#if defined(RTE_TOOLCHAIN_GCC ) && (defined(RTE_ARCH_X86 ) || defined(RTE_ARCH_LOONGARCH )) +/* Optimization for code size. +* GCC only optimizes single-bit MSB tests this way, so we do it by hand with multi-bit. +* +* The flags RTE_MBUF_F_INDIRECT and RTE_MBUF_F_EXTERNAL are both in the MSB of the +* 64-bit ol_flags field, so we only compare this one byte instead of all 64 bits. +* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. +* +* Note: Tested on x86-64 using GCC version 16.0.0 20251019 (experimental). +* +* Without this optimization, GCC generates 17 bytes of instructions: +* movabs rax,0x6000000000000000 // 10 bytes +* and rax,QWORD PTR [rdi+0x18] // 4 bytes +* sete al // 3 bytes +* With this optimization, GCC generates only 7 bytes of instructions: +* test BYTE PTR [rdi+0x1f],0x60 // 4 bytes +* sete al // 3 bytes +* +* Note: Tested on loongarch using GCC version 15.2.0. +* +* Without this optimization, GCC generates 5 instructions: +* ld.d $a0, $a0, 24 +* move $t0, $zero +* lu52i.d $t0, $t0, 1536 +* and $a0, $a0, $t0 +* sltui $a0, $a0, 1 +* With this optimization, GCC generates only 3 instructions: +* ld.bu $a0, $a0, 31 +* andi $a0, $a0, 0x60 +* sltui $a0, $a0, 1 +* +* Note: Tested on ARM64, GCC generates 3 instructions both without and with the optimization, +* so no need for the optimization here. +*/ +static_assert ((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ) ==UINT64_C (0x60 ) << (7 *CHAR_BIT ), +"(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 at byte offset 7" ); +#undef RTE_MBUF_DIRECT +#define RTE_MBUF_DIRECT (mb) !(((const char *)(&(mb)->ol_flags))[7] & 0x60) +#endif +/** Uninitialized or unspecified port. */ #define RTE_MBUF_PORT_INVALID UINT16_MAX/** For backwards compatibility. */
Signature · 1 line
2.43.0
PATCH v4 1/1 mbuf: optimize segment prefree
In reply to Morten BrørupRefactored rte_pktmbuf_prefree_seg() for both performance and readability.
With the optimized RTE_MBUF_DIRECT() macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.
---
v4:
* Enabled the optimized RTE_MBUF_DIRECT() macro for GCC on all architectures.
v3:
* Rewrote the optimized RTE_MBUF_DIRECT() macro for readability; use numerical value instead of long names. (Bruce Richardson)
* Enabled the optimized RTE_MBUF_DIRECT() macro for Loongarch too. v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimized RTE_MBUF_DIRECT() macro. (Stephen Hemminger)
* Added static_assert() to verify that the optimized RTE_MBUF_DIRECT() macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.
--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 32 deletions(-)
lib/mbuf/rte_mbuf.h +19 -32
@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h>
@@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +47 -0
59 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 118 lines · Morten Brørup
Refactored
rte_pktmbuf_prefree_seg()for both performance and readability.With the optimized
RTE_MBUF_DIRECT()macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v4:
* Enabled the optimizedRTE_MBUF_DIRECT()macro for GCC on all architectures.
v3:
* Rewrote the optimizedRTE_MBUF_DIRECT()macro for readability; use numerical value instead of long names. (Bruce Richardson)
* Enabled the optimizedRTE_MBUF_DIRECT()macro for Loongarch too. v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimizedRTE_MBUF_DIRECT()macro. (Stephen Hemminger)
* Addedstatic_assert()to verify that the optimizedRTE_MBUF_DIRECT()macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 32 deletions(-)lib/mbuf/rte_mbuf.h +19
-32@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h> @@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/** lib/mbuf/rte_mbuf_core.h +1
-0@@ -706,6 +706,7 @@ struct rte_mbuf_ext_shared_info {*/ #define RTE_MBUF_HAS_EXTBUF (mb) ((mb)->ol_flags & RTE_MBUF_F_EXTERNAL) +#if !defined(RTE_TOOLCHAIN_GCC ) || defined __DOXYGEN__/** * Returns TRUE if given mbuf is direct, or FALSE otherwise. *
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 718
Quoted 4 lines
lib/mbuf/rte_mbuf_core.h +1
-0@@ -714,6 +715,52 @@ struct rte_mbuf_ext_shared_info {*/ #define RTE_MBUF_DIRECT (mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +#else /* RTE_TOOLCHAIN_GCC */
Do we need the non-gcc block?
Quoted 34 lines
lib/mbuf/rte_mbuf_core.h +34
-0+#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* Macro optimized for code size. +* GCC only optimizes single-bit MSB tests this way, so we do it by hand with multi-bit. +* +* The flags RTE_MBUF_F_INDIRECT and RTE_MBUF_F_EXTERNAL are both in the MSB of the +* 64-bit ol_flags field, so we only compare this one byte instead of all 64 bits. +* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. +* +* Note: Tested on x86-64 using GCC version 16.0.0 20251019 (experimental). +* +* Without this optimization, GCC generates 17 bytes of instructions: +* movabs rax,0x6000000000000000 // 10 bytes +* and rax,QWORD PTR [rdi+0x18] // 4 bytes +* sete al // 3 bytes +* With this optimization, GCC generates only 7 bytes of instructions: +* test BYTE PTR [rdi+0x1f],0x60 // 4 bytes +* sete al // 3 bytes +* +* Note: Tested on loongarch using GCC version 15.2.0. +* +* Without this optimization, GCC generates 5 instructions: +* ld.d $a0, $a0, 24 +* move $t0, $zero +* lu52i.d $t0, $t0, 1536 +* and $a0, $a0, $t0 +* sltui $a0, $a0, 1 +* With this optimization, GCC generates only 3 instructions: +* ld.bu $a0, $a0, 31 +* andi $a0, $a0, 0x60 +* sltui $a0, $a0, 1 +* +* Note: GCC also generates smaller code size with the optimized macro on many other architectures. +* +* Note: GCC generates the same code size as with the plain macro on ARM (64 and 32 bit).
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 762
Quoted 10 lines
lib/mbuf/rte_mbuf_core.h +10
-0+*/ +static_assert ((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ) ==UINT64_C (0x60 ) << (7 *CHAR_BIT ), +"(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 at byte offset 7" ); +#define RTE_MBUF_DIRECT (mb) !(((const char *)(&(mb)->ol_flags))[7] & 0x60) +#elif RTE_BYTE_ORDER ==RTE_BIG_ENDIAN +/* As described above; but on big endian architecture, the MSB is at byte offset 0. */ +static_assert ((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ) ==UINT64_C (0x60 ) << (7 *CHAR_BIT ), +"(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 at byte offset 0" ); +#define RTE_MBUF_DIRECT (mb) !(((const char *)(&(mb)->ol_flags))[0] & 0x60) +#endif /* RTE_BYTE_ORDER */
Minor nit, the static assert is common, so can be put out of the endianness check rather than duplicated. I'd also suggest putting the the comment outside it too, so that the endianness check only covers one line each. Also, in the comment, showing an example on one arch is probably enough, showing two sort of implies that you should show all, while if you show just one, you can clearly state its just one example.
BTW: if you want to remove the endian ifdefs, you can compute the offset as e.g.
#define OLF_MSB (7 * (RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN)) // 7 for LE, 0 for BE
#define RTE_MBUF_DIRECT(mb) !(((const char *)(&(mb)->ol_flags))[OLF_MSB] & 0x60)
Quoted 7 lines
lib/mbuf/rte_mbuf_core.h +1
-0+#endif /* RTE_TOOLCHAIN_GCC */ /** Uninitialized or unspecified port. */ #define RTE_MBUF_PORT_INVALID UINT16_MAXSignature · 1 line
2.43.0
Quoted 195 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 22 October 2025 17.03Quoted 1 line · Morten Brørup
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likely codepath
Quoted 1 line
now fits within one instruction cache line on x86-64 when built with
GCC.
Quoted 5 lines
Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>Quoted 12 lines
---
v4:
* Enabled the optimizedRTE_MBUF_DIRECT()macro for GCC on all architectures.
v3:
* Rewrote the optimizedRTE_MBUF_DIRECT()macro for readability; use numerical value instead of long names. (Bruce Richardson)
* Enabled the optimizedRTE_MBUF_DIRECT()macro for Loongarch too. v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimizedRTE_MBUF_DIRECT()macro.
Quoted 2 lines
(Stephen Hemminger)
* Addedstatic_assert()to verify that the optimized
RTE_MBUF_DIRECT()Quoted 1 line
macro is valid, specifically that the tested bits are in the MSB of
the
Quoted 3 lines
64-bit field.
--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++-------------------------
lib/mbuf/rte_mbuf.h static inline int
Quoted 16 lines
lib/mbuf/rte_mbuf_core.h | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 32 deletions(-)lib/mbuf/rte_mbuf.h +1
-0@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h> @@ -1458,44 +1459,30 @@ static inline int
__rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m)Quoted 67 lines
lib/mbuf/rte_mbuf.h +18
-32static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/** lib/mbuf/rte_mbuf_core.h +0
-0lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 707
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +0
-0@@ -706,6 +706,7 @@ struct rte_mbuf_ext_shared_info {*/ #define RTE_MBUF_HAS_EXTBUF (mb) ((mb)->ol_flags &
RTE_MBUF_F_EXTERNAL)lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 718
Quoted 10 lines
lib/mbuf/rte_mbuf_core.h +2
-0+#if !defined(RTE_TOOLCHAIN_GCC ) || defined __DOXYGEN__/** * Returns TRUE if given mbuf is direct, or FALSE otherwise. * @@ -714,6 +715,52 @@ struct rte_mbuf_ext_shared_info {*/ #define RTE_MBUF_DIRECT (mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +#else /* RTE_TOOLCHAIN_GCC */ Do we need the non-gcc block?
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 3 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* Macro optimized for code size. +* GCC only optimizes single-bit MSB tests this way, so we do it by hand with multi-bit.
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 723
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +2
-0+* +* The flags RTE_MBUF_F_INDIRECT and RTE_MBUF_F_EXTERNAL are both in the MSB of the
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 724
Quoted 1 line
lib/mbuf/rte_mbuf_core.h +1
-0+* 64-bit ol_flags field, so we only compare this one byte instead of all 64 bits.
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 725
Quoted 1 line
lib/mbuf/rte_mbuf_core.h +1
-0+* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7.
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 727
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +2
-0+* +* Note: Tested on x86-64 using GCC version 16.0.0 20251019 (experimental).
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 729
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +2
-0+* +* Without this optimization, GCC generates 17 bytes of instructions:
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 4 lines
lib/mbuf/rte_mbuf_core.h +4
-0+* movabs rax,0x6000000000000000 // 10 bytes +* and rax,QWORD PTR [rdi+0x18] // 4 bytes +* sete al // 3 bytes +* With this optimization, GCC generates only 7 bytes of instructions:
Quoted 7 lines
lib/mbuf/rte_mbuf_core.h +7
-0+* test BYTE PTR [rdi+0x1f],0x60 // 4 bytes +* sete al // 3 bytes +* +* Note: Tested on loongarch using GCC version 15.2.0. +* +* Without this optimization, GCC generates 5 instructions: +* ld.d $a0, $a0, 24 lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 10 lines
lib/mbuf/rte_mbuf_core.h +10
-0+* move $t0, $zero +* lu52i.d $t0, $t0, 1536 +* and $a0, $a0, $t0 +* sltui $a0, $a0, 1 +* With this optimization, GCC generates only 3 instructions: +* ld.bu $a0, $a0, 31 +* andi $a0, $a0, 0x60 +* sltui $a0, $a0, 1 +* +* Note: GCC also generates smaller code size with the optimized macro on many other architectures.
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 752
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +2
-0+* +* Note: GCC generates the same code size as with the plain macro on ARM (64 and 32 bit).
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 754
Quoted 2 lines
lib/mbuf/rte_mbuf_core.h +2
-0+*/ +static_assert((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) ==
UINT64_C(0x60)<< (7 *CHAR_BIT),lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 755
Quoted 1 line
lib/mbuf/rte_mbuf_core.h +1
-0+ "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 at byteoffset 7");
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 759
Quoted 4 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#define RTE_MBUF_DIRECT (mb) !(((const char *)(&(mb)- ol_flags))[7] & 0x60) +#elif RTE_BYTE_ORDER ==RTE_BIG_ENDIAN +/* As described above; but on big endian architecture, the MSB is at byte offset 0. */
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 760
Quoted 1 line
lib/mbuf/rte_mbuf_core.h +1
-0+static_assert((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) ==
UINT64_C(0x60)<< (7 *CHAR_BIT),lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 761
Quoted 1 line
lib/mbuf/rte_mbuf_core.h +1
-0+ "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 at byteoffset 0");
Quoted 7 lines
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 764
Quoted 3 lines
lib/mbuf/rte_mbuf_core.h +2
-0+#define RTE_MBUF_DIRECT (mb) !(((const char *)(&(mb)- ol_flags))[0] & 0x60) +#endif /* RTE_BYTE_ORDER */ Minor nit, the static assert is common, so can be put out of the endianness
check rather than duplicated.
I considered a generic common static_assert, but prefer individual ones with specific error texts (byte offset 7 or 0).
Quoted 2 lines
I'd also suggest putting the the comment
outside it too, so that the endianness check only covers one line each.
Good idea.
While doing this, I'll see how it looks with a common static_assert and individual ones.
Quoted 4 lines
Also, in the comment, showing an example on one arch is probably enough,
showing two sort of implies that you should show all, while if you show just one, you can clearly state its just one example.
Agree. Less is more.
Quoted 9 lines
BTW: if you want to remove the endian ifdefs, you can compute the offset
as e.g.#define
OLF_MSB(7 * (RTE_BYTE_ORDER==RTE_LITTLE_ENDIAN)) // 7 for LE, 0 for BE
#defineRTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-Quoted 1 line
lib/mbuf/rte_mbuf_core.h +0
-0ol_flags))[OLF_MSB] & 0x60)
I don't think it improves source code readability, so not going to do it.
Also, it's a header file, so the OLF_MSB would need to have some obscure name. Alternatively reside in rte_common.h with a generic name for similar purposes.
Quoted 9 lines
Quoted 9 lines
lib/mbuf/rte_mbuf_core.h +1
-0+#endif /* RTE_TOOLCHAIN_GCC */ /** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--
2.43.0
Will post a v5.
Quoted 11 lines
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 718
Quoted 9 lines
lib/mbuf/rte_mbuf_core.h +2
-0+#if !defined(RTE_TOOLCHAIN_GCC ) || defined __DOXYGEN__/** * Returns TRUE if given mbuf is direct, or FALSE otherwise. * @@ -714,6 +715,52 @@ struct rte_mbuf_ext_shared_info {*/ #define RTE_MBUF_DIRECT (mb) \ (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +#else /* RTE_TOOLCHAIN_GCC */ Do we need the non-gcc block?
I have now tested with a bunch of compilers on Godbolt, and it seems only clang doesn't need this optimization, so I'll remove the compiler check and make the optimized variant the only one.
PATCH v5 1/1 mbuf: optimize segment prefree
In reply to Morten BrørupRefactored rte_pktmbuf_prefree_seg() for both performance and readability.
With the optimized RTE_MBUF_DIRECT() macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.
---
v5:
* Removed the plain RTE_MBUF_DIRECT() macro, and only use the optimized variant. (Bruce Richardson)
Further testing on Godbolt confirmed that other compilers benefit from the optimized macro too.
* Shortened the description of the RTE_MBUF_DIRECT() macro, and only provide one example of code emitted by a compiler. (Bruce Richardson)
* Consolidated the static_assert() into one, covering both little and big endian.
This also reduces the amount of endian-conditional source code and improves readability.
(Bruce Richardson)
* Added comment about MSB meaning "most significant byte". v4:
* Enabled the optimized RTE_MBUF_DIRECT() macro for GCC on all architectures.
v3:
* Rewrote the optimized RTE_MBUF_DIRECT() macro for readability; use numerical value instead of long names. (Bruce Richardson)
* Enabled the optimized RTE_MBUF_DIRECT() macro for Loongarch too. v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimized RTE_MBUF_DIRECT() macro. (Stephen Hemminger)
* Added static_assert() to verify that the optimized RTE_MBUF_DIRECT() macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.
--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 33 +++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 35 deletions(-)
lib/mbuf/rte_mbuf.h +19 -32
@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h>
@@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +30 -3
@@ -711,9 +711,36 @@ struct rte_mbuf_ext_shared_info {* * If a mbuf embeds its own data after the rte_mbuf structure, this mbuf * can be defined as a direct mbuf. -*/ -#define RTE_MBUF_DIRECT (mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +* +* Note: Macro optimized for code size. +* +* The plain macro would be: +* #define RTE_MBUF_DIRECT(mb) \ +* (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +* +* The flags RTE_MBUF_F_INDIRECT and RTE_MBUF_F_EXTERNAL are both in the MSB (most significant +* byte) of the 64-bit ol_flags field, so we only compare this one byte instead of all 64 bits. +* +* E.g., GCC version 16.0.0 20251019 (experimental) generates the following code for x86-64. +* +* With the plain macro, 17 bytes of instructions: +* movabs rax,0x6000000000000000 // 10 bytes +* and rax,QWORD PTR [rdi+0x18] // 4 bytes +* sete al // 3 bytes +* With this optimized macro, only 7 bytes of instructions: +* test BYTE PTR [rdi+0x1f],0x60 // 4 bytes +* sete al // 3 bytes +*/ +#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. */ +#define RTE_MBUF_DIRECT (mb) !(((const char *)(&(mb)->ol_flags))[7] & 0x60) +#elif RTE_BYTE_ORDER ==RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is at byte offset 0. */ +#define RTE_MBUF_DIRECT (mb) !(((const char *)(&(mb)->ol_flags))[0] & 0x60) +#endif +/* Verify the optimization above. */ +static_assert ((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ) ==UINT64_C (0x60 ) << (7 *CHAR_BIT ), +"(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 at MSB" );/** Uninitialized or unspecified port. */ #define RTE_MBUF_PORT_INVALID UINT16_MAX
Signature · 1 line
2.43.0
Quoted 9 lines · Morten Brørup
Refactored
rte_pktmbuf_prefree_seg()for both performance and readability.With the optimized
RTE_MBUF_DIRECT()macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>
Quoted 14 lines
---
v5:
* Removed the plainRTE_MBUF_DIRECT()macro, and only use the optimized variant. (Bruce Richardson)
Further testing on Godbolt confirmed that other compilers benefit from the optimized macro too.
* Shortened the description of theRTE_MBUF_DIRECT()macro, and only provide one example of code emitted by a compiler. (Bruce Richardson)
* Consolidated thestatic_assert()into one, covering both little and big endian.
This also reduces the amount of endian-conditional source code and improves readability.
(Bruce Richardson)
* Added comment about MSB meaning "most significant byte".
LGTM now thanks!
Quoted 156 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance and readability.With the optimized
RTE_MBUF_DIRECT()macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v5:
* Removed the plainRTE_MBUF_DIRECT()macro, and only use the optimized variant. (Bruce Richardson)
Further testing on Godbolt confirmed that other compilers benefit from the optimized macro too.
* Shortened the description of theRTE_MBUF_DIRECT()macro, and only provide one example of code emitted by a compiler. (Bruce Richardson)
* Consolidated thestatic_assert()into one, covering both little and big endian.
This also reduces the amount of endian-conditional source code and improves readability.
(Bruce Richardson)
* Added comment about MSB meaning "most significant byte". v4:
* Enabled the optimizedRTE_MBUF_DIRECT()macro for GCC on all architectures.
v3:
* Rewrote the optimizedRTE_MBUF_DIRECT()macro for readability; use numerical value instead of long names. (Bruce Richardson)
* Enabled the optimizedRTE_MBUF_DIRECT()macro for Loongarch too. v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimizedRTE_MBUF_DIRECT()macro. (Stephen Hemminger)
* Addedstatic_assert()to verify that the optimizedRTE_MBUF_DIRECT()macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 33 +++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 35 deletions(-)lib/mbuf/rte_mbuf.h +19
-32@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h> @@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +8
-3@@ -711,9 +711,36 @@ struct rte_mbuf_ext_shared_info {* * If a mbuf embeds its own data after the rte_mbuf structure, this mbuf * can be defined as a direct mbuf. -*/ -#define RTE_MBUF_DIRECT (mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +* +* Note: Macro optimized for code size. +* +* The plain macro would be: +* #define RTE_MBUF_DIRECT(mb) \ +* (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +* +* The flags RTE_MBUF_F_INDIRECT and RTE_MBUF_F_EXTERNAL are both in the MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare this one byte instead of all 64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generates the following code for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * With this optimized macro, only 7 bytes of instructions: + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info { · line 739
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +6
-0+#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. */ +#define RTE_MBUF_DIRECT (mb) !(((const char *)(&(mb)->ol_flags))[7] & 0x60) +#elif RTE_BYTE_ORDER ==RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is at byte offset 0. */ +#define RTE_MBUF_DIRECT (mb) !(((const char *)(&(mb)->ol_flags))[0] & 0x60)
A stupid q: why then not simply do:
(mb->ol_flags >> 56) & 0x60
then?
Should help to all these LE/BE casts, etc.
Quoted 10 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#endif +/* Verify the optimization above. */ +static_assert((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) ==
UINT64_C(0x60)<< (7 *CHAR_BIT),lib/mbuf/rte_mbuf_core.h +1
-1+ "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 at MSB");/** Uninitialized or unspecified port. */ #define RTE_MBUF_PORT_INVALID UINT16_MAX --2.43.0
Quoted 40 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 10.51lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 8 lines
lib/mbuf/rte_mbuf_core.h +6
-2-#define RTE_MBUF_DIRECT (mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +* +* Note: Macro optimized for code size. +* +* The plain macro would be: +* #define RTE_MBUF_DIRECT(mb) \ +* (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare both inthe
Quoted 2 lines
MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare this onebyte instead of all
Quoted 3 lines
64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generates thefollowing code
Review
Quoted 12 lines
for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * With this optimized macro, only 7 bytes of instructions: + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
(quoted diff) +2
-0+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is atbyte offset 7. */
Review
Quoted 4 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[7] &0x60)(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is atbyte offset 0. */
Quoted 7 lines
Quoted 2 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[0] &0x60)A stupid q: why then not simply do:
(mb->ol_flags>> 56) &0x60
then?
Should help to all these LE/BE casts, etc.
GCC is too stupid for that too.
Playing around with Godbolt shows that
return !((char)(p[3] >> 56) & 0x60); becomes movzx eax,BYTE PTR [rdi+0x1f] // 4 bytes test al,0x60 // 2 bytes Instead of simply test BYTE PTR [rdi+0x1f],0x60 // 4 bytes
Good suggestion, though.
Quoted 12 lines
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#endif +/* Verify the optimization above. */ +static_assert((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) ==
UINT64_C(0x60)<< (7 *CHAR_BIT),(quoted diff) +1
-0+ "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 atMSB");
Quoted 5 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--
2.43.0
Quoted 52 lines
Quoted 42 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 10.51lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 8 lines
lib/mbuf/rte_mbuf_core.h +6
-2-#define RTE_MBUF_DIRECT (mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +* +* Note: Macro optimized for code size. +* +* The plain macro would be: +* #define RTE_MBUF_DIRECT(mb) \ +* (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare both inthe
Quoted 2 lines
MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare this onebyte instead of all
Quoted 3 lines
64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generates thefollowing code
Review
Quoted 12 lines
for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * With this optimized macro, only 7 bytes of instructions: + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
(quoted diff) +2
-0+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is atbyte offset 7. */
Review
Quoted 4 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[7] &0x60)(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is atbyte offset 0. */
Quoted 7 lines
Quoted 2 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[0] &0x60)A stupid q: why then not simply do:
(mb->ol_flags>> 56) &0x60
then?
Should help to all these LE/BE casts, etc.GCC is too stupid for that too.
Quoted 7 lines
Playing around with Godbolt shows that
return !((char)(p[3] >> 56) & 0x60); becomes movzx eax,BYTE PTR [rdi+0x1f] // 4 bytes test al,0x60 // 2 bytes Instead of simply test BYTE PTR [rdi+0x1f],0x60 // 4 bytes
And these 2 extra bytes in instructions, are that really that critical? My guess, we wouldn't notice any real diff here.
But if it really is, can I ask you to create a new define for 0x60, to avoid hardcoded constants in the code?
Might be something like
#define RTE_MBUF_F_INDIRECT_EXTERNAL_1B ...
or so.
Konstantin
Quoted 14 lines
Good suggestion, though.
Quoted 12 lines
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#endif +/* Verify the optimization above. */ +static_assert((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) ==
UINT64_C(0x60)<< (7 *CHAR_BIT),(quoted diff) +1
-0+ "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 atMSB");
Quoted 5 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--
2.43.0
Quoted 66 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 16.05Quoted 5 lines
Quoted 5 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 10.51Review
Quoted 2 lines
(quoted diff) +0
-2-#define RTE_MBUF_DIRECT(mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * #defineRTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|
RTE_MBUF_F_EXTERNAL)))Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALareboth in
Quoted 3 lines
Quoted 3 lines
the
Quoted 2 lines
MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare thisone
Quoted 4 lines
Quoted 4 lines
byte instead of all
Quoted 3 lines
64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generatesthe
Quoted 13 lines
Quoted 13 lines
following code
Review
Quoted 12 lines
for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * With this optimized macro, only 7 bytes of instructions: + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
(quoted diff) +2
-0+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer isat
Quoted 16 lines
Quoted 6 lines
byte offset 7. */
Review
Quoted 4 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[7] &0x60)(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is atbyte offset 0. */
Quoted 7 lines
Quoted 2 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[0] &0x60)A stupid q: why then not simply do:
(mb->ol_flags>> 56) &0x60
then?
Should help to all these LE/BE casts, etc.GCC is too stupid for that too.
Quoted 7 lines
Playing around with Godbolt shows that
return !((char)(p[3] >> 56) & 0x60); becomes movzx eax,BYTE PTR [rdi+0x1f] // 4 bytes test al,0x60 // 2 bytes Instead of simply test BYTE PTR [rdi+0x1f],0x60 // 4 bytes
Quoted 2 lines
And these 2 extra bytes in instructions, are that really that critical? My guess, we wouldn't notice any real diff here.
The optimized macro made the common code path of the refactored rte_pktmbuf_prefree_seg() fit into one cache line. IIRC, all 10 bytes saving were required for this.
Quoted 5 lines
But if it really is, can I ask you to create a new define for
0x60, to avoid hardcoded constants in the code?
Might be something like
#defineRTE_MBUF_F_INDIRECT_EXTERNAL_1B...
or so.
I started out using the field names, but Bruce suggested using 0x60 for readability, making the macros shorter, which IMO looks good.
I don't like adding special names just for this, so either we stick with 0x60 or go for "(char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 * CHAR_BIT))", something like this:
#ifdef __DOXYGEN__
#define RTE_MBUF_DIRECT(mb) \ !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET /* 7 or 0, depending on endianness */] & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 * CHAR_BIT))) #else /* !__DOXYGEN__ */ #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN /* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. */ #define RTE_MBUF_DIRECT(mb) \ !(((const char *)(&(mb)->ol_flags))[7] & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 * CHAR_BIT))) #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN /* On big endian architecture, the MSB of a 64-bit integer is at byte offset 0. */ #define RTE_MBUF_DIRECT(mb) \ !(((const char *)(&(mb)->ol_flags))[0] & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 * CHAR_BIT)))
#endif /* RTE_BYTE_ORDER */
#endif /* !__DOXYGEN__ */
/* Verify the optimization above. */
static_assert(((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) & (UINT64_C(0xFF) << (7 * CHAR_BIT))) ==
(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not at MSB");
Quoted 16 lines
Konstantin
Quoted 14 lines
Good suggestion, though.
Quoted 12 lines
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#endif +/* Verify the optimization above. */ +static_assert((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) ==
UINT64_C(0x60)<< (7 *CHAR_BIT),(quoted diff) +1
-0+ "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60 atMSB");
Quoted 5 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--
2.43.0
Quoted 68 lines
Quoted 68 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 16.05Quoted 5 lines
Quoted 5 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 10.51Review
Quoted 2 lines
(quoted diff) +0
-2-#define RTE_MBUF_DIRECT(mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * #defineRTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|
RTE_MBUF_F_EXTERNAL)))Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALareboth in
Quoted 3 lines
Quoted 3 lines
the
Quoted 2 lines
MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare thisone
Quoted 4 lines
Quoted 4 lines
byte instead of all
Quoted 3 lines
64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generatesthe
Quoted 13 lines
Quoted 13 lines
following code
Review
Quoted 12 lines
for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * With this optimized macro, only 7 bytes of instructions: + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
(quoted diff) +2
-0+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer isat
Quoted 23 lines
Quoted 6 lines
byte offset 7. */
Review
Quoted 4 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[7] &0x60)(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is atbyte offset 0. */
Quoted 7 lines
Quoted 2 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[0] &0x60)A stupid q: why then not simply do:
(mb->ol_flags>> 56) &0x60
then?
Should help to all these LE/BE casts, etc.GCC is too stupid for that too.
Playing around with Godbolt shows that
return !((char)(p[3] >> 56) & 0x60); becomes movzx eax,BYTE PTR [rdi+0x1f] // 4 bytes test al,0x60 // 2 bytes Instead of simply test BYTE PTR [rdi+0x1f],0x60 // 4 bytes
Quoted 6 lines
Quoted 2 lines
And these 2 extra bytes in instructions, are that really that critical? My guess, we wouldn't notice any real diff here.
The optimized macro made the common code path of the refactored
rte_pktmbuf_prefree_seg()fit into one cache line. IIRC, all 10 bytes saving were required for this.
I understand that. but is that change will provide a measurable impact, in terms of cycles/op or pps or so?
Quoted 6 lines
Quoted 5 lines
But if it really is, can I ask you to create a new define for
0x60, to avoid hardcoded constants in the code?
Might be something like
#defineRTE_MBUF_F_INDIRECT_EXTERNAL_1B...
or so.
Quoted 6 lines
I started out using the field names, but Bruce suggested using
0x60for readability, making the macros shorter, which IMO looks good.I don't like adding special names just for this, so either we stick with
0x60or go for "(char)((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL) >> (7 *CHAR_BIT))", something like this:
My vote would be to use the construction above.
Might be put it in a new macro for readability.
Konstantin
Quoted 44 lines
#ifdef
__DOXYGEN__#define RTE_MBUF_DIRECT(mb) \ !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET /* 7 or 0, depending on endianness */] & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))
#else /* !__DOXYGEN__ */
#ifRTE_BYTE_ORDER==RTE_LITTLE_ENDIAN
/* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. */#define RTE_MBUF_DIRECT(mb) \ !(((const char *)(&(mb)->ol_flags))[7] & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 * CHAR_BIT))) #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN /* On big endian architecture, the MSB of a 64-bit integer is at byte offset 0. */ #define RTE_MBUF_DIRECT(mb) \ !(((const char *)(&(mb)->ol_flags))[0] & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))
#endif /*RTE_BYTE_ORDER*/
#endif /* !__DOXYGEN__ */
/* Verify the optimization above. */ static_assert(((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL) & (UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not at MSB");Quoted 10 lines
Konstantin
Quoted 8 lines
Good suggestion, though.
Quoted 6 lines
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#endif +/* Verify the optimization above. */ +static_assert((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) ==
UINT64_C(0x60)<< (7 *CHAR_BIT),(quoted diff) +1
-0+ "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60at
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
MSB");
Quoted 5 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--
2.43.0
Quoted 75 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 17.27Quoted 15 lines
Quoted 15 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 16.05Quoted 5 lines
Quoted 5 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 10.51Review
Quoted 2 lines
(quoted diff) +0
-2-#define RTE_MBUF_DIRECT(mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * #defineRTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
RTE_MBUF_F_EXTERNAL)))Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALareboth in
Quoted 3 lines
Quoted 3 lines
the
Quoted 2 lines
MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only comparethis
Quoted 5 lines
Quoted 5 lines
one
Quoted 4 lines
Quoted 4 lines
byte instead of all
Quoted 3 lines
64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental)generates
Quoted 14 lines
Quoted 14 lines
the
Quoted 13 lines
Quoted 13 lines
following code
Review
Quoted 12 lines
for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * With this optimized macro, only 7 bytes of instructions: + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
(quoted diff) +2
-0+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bitinteger is
Quoted 6 lines
Quoted 6 lines
at
Quoted 5 lines
Quoted 5 lines
byte offset 7. */
Review
Quoted 4 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[7] &0x60)(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integeris at
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 8 lines
byte offset 0. */
Quoted 2 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[0] &0x60)A stupid q: why then not simply do:
(mb->ol_flags>> 56) &0x60
then?
Should help to all these LE/BE casts, etc.GCC is too stupid for that too.
Playing around with Godbolt shows that
return !((char)(p[3] >> 56) & 0x60); becomes movzx eax,BYTE PTR [rdi+0x1f] // 4 bytes test al,0x60 // 2 bytes Instead of simply test BYTE PTR [rdi+0x1f],0x60 // 4 bytesQuoted 1 line
Quoted 1 line
And these 2 extra bytes in instructions, are that really that
critical?
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
My guess, we wouldn't notice any real diff here.
The optimized macro made the common code path of the refactored
rte_pktmbuf_prefree_seg()fit into one cache line. IIRC, all 10 bytes saving were required for this.I understand that. but is that change will provide a measurable impact, in terms of cycles/op or pps or so?
L1 instruction cache is important; reducing code size of a per-packet function might have an effect in some cases. I don't have other metrics than code size for this optimization.
I just tested to see if I recalled correctly, and here's the generated code with the two different macros:
#define RTE_MBUF_DIRECT(mb) !(((const char *)(&(mb)->ol_flags))[7] & 0x60)
0000000000000670 <review_rte_pktmbuf_prefree_seg>:
670: f3 0f 1e fa endbr64
674: 41 57 push %r15
676: 41 56 push %r14
678: 41 55 push %r13
67a: 41 54 push %r12
67c: 55 push %rbp
67d: 53 push %rbx
67e: 48 89 fb mov %rdi,%rbx
681: 48 83 ec 18 sub $0x18,%rsp
685: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax
68c: 00 00
68e: 48 89 44 24 08 mov %rax,0x8(%rsp)
693: 31 c0 xor %eax,%eax
695: 0f b7 6f 12 movzwl 0x12(%rdi),%ebp
699: 66 83 fd 01 cmp $0x1,%bp
69d: 75 51 jne 6f0 <review_rte_pktmbuf_prefree_seg+0x80>
** Look here {
69f: f6 47 1f 60 testb $0x60,0x1f(%rdi)
6a3: 75 6b jne 710 <review_rte_pktmbuf_prefree_seg+0xa0>
** }
6a5: 66 83 7b 14 01 cmpw $0x1,0x14(%rbx)
6aa: 74 09 je 6b5 <review_rte_pktmbuf_prefree_seg+0x45>
6ac: b8 01 00 00 00 mov $0x1,%eax
6b1: 66 89 43 14 mov %ax,0x14(%rbx)
6b5: 48 83 7b 40 00 cmpq $0x0,0x40(%rbx)
6ba: 74 08 je 6c4 <review_rte_pktmbuf_prefree_seg+0x54>
6bc: 48 c7 43 40 00 00 00 movq $0x0,0x40(%rbx)
6c3: 00
6c4: 48 89 d8 mov %rbx,%rax
6c7: 48 8b 54 24 08 mov 0x8(%rsp),%rdx
6cc: 64 48 2b 14 25 28 00 sub %fs:0x28,%rdx
6d3: 00 00
6d5: 0f 85 3a 02 00 00 jne 915 <review_rte_pktmbuf_prefree_seg+0x2a5>
6db: 48 83 c4 18 add $0x18,%rsp
6df: 5b pop %rbx
6e0: 5d pop %rbp
6e1: 41 5c pop %r12
6e3: 41 5d pop %r13
6e5: 41 5e pop %r14
6e7: 41 5f pop %r15
6e9: c3 ret#define RTE_MBUF_DIRECT(mb) \ !((char)((mb)->ol_flags >> (7 * CHAR_BIT)) & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 * CHAR_BIT)))
0000000000000690 <review_rte_pktmbuf_prefree_seg>:
690: f3 0f 1e fa endbr64
694: 41 57 push %r15
696: 41 56 push %r14
698: 41 55 push %r13
69a: 41 54 push %r12
69c: 55 push %rbp
69d: 53 push %rbx
69e: 48 89 fb mov %rdi,%rbx
6a1: 48 83 ec 18 sub $0x18,%rsp
6a5: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax
6ac: 00 00
6ae: 48 89 44 24 08 mov %rax,0x8(%rsp)
6b3: 31 c0 xor %eax,%eax
6b5: 0f b7 6f 12 movzwl 0x12(%rdi),%ebp
6b9: 66 83 fd 01 cmp $0x1,%bp
6bd: 75 59 jne 718 <review_rte_pktmbuf_prefree_seg+0x88>
** Look here {
6bf: 48 8b 47 18 mov 0x18(%rdi),%rax
6c3: 48 89 c2 mov %rax,%rdx
6c6: 48 c1 ea 38 shr $0x38,%rdx
6ca: 83 e2 60 and $0x60,%edx
6cd: 75 71 jne 740 <review_rte_pktmbuf_prefree_seg+0xb0>
* }
6cf: 66 83 7b 14 01 cmpw $0x1,0x14(%rbx)
6d4: 74 09 je 6df <review_rte_pktmbuf_prefree_seg+0x4f>
6d6: b8 01 00 00 00 mov $0x1,%eax
6db: 66 89 43 14 mov %ax,0x14(%rbx)
6df: 48 83 7b 40 00 cmpq $0x0,0x40(%rbx)
6e4: 74 08 je 6ee <review_rte_pktmbuf_prefree_seg+0x5e>
6e6: 48 c7 43 40 00 00 00 movq $0x0,0x40(%rbx)
6ed: 00
6ee: 48 89 d8 mov %rbx,%rax
6f1: 48 8b 54 24 08 mov 0x8(%rsp),%rdx
6f6: 64 48 2b 14 25 28 00 sub %fs:0x28,%rdx
6fd: 00 00
6ff: 0f 85 50 02 00 00 jne 955 <review_rte_pktmbuf_prefree_seg+0x2c5>
705: 48 83 c4 18 add $0x18,%rsp
709: 5b pop %rbx
70a: 5d pop %rbp
70b: 41 5c pop %r12
70d: 41 5d pop %r13
70f: 41 5e pop %r14
711: 41 5f pop %r15
713: c3 retQuoted 11 lines
Quoted 8 lines
Quoted 6 lines
But if it really is, can I ask you to create a new define for
0x60, to avoid hardcoded constants in the code?
Might be something like
#defineRTE_MBUF_F_INDIRECT_EXTERNAL_1B...
or so.I started out using the field names, but Bruce suggested using
0x60for
Quoted 2 lines
readability, making the macros shorter, which IMO looks good.
Quoted 8 lines
Quoted 1 line
I don't like adding special names just for this, so either we stick
with
0x60or go forQuoted 2 lines
"(char)((RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL) >> (7 *CHAR_BIT))", something like this:My vote would be to use the construction above.
Might be put it in a new macro for readability.
Konstantin
The optimization requires casting ol_flags as a byte array and then reading the MSB; otherwise GCC and some other compilers are too stupid to perform the optimization. So, should I post a v7 with the code proposed below (to get rid of the 0x60 numerical value)?
Quoted 48 lines
Quoted 4 lines
#ifdef
__DOXYGEN__#define RTE_MBUF_DIRECT(mb) \ !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET /* 7 or 0,depending on
Quoted 6 lines
endianness */] & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))
#else /* !__DOXYGEN__ */
#ifRTE_BYTE_ORDER==RTE_LITTLE_ENDIAN
/* On little endian architecture, the MSB of a 64-bit integer is atbyte offset 7. */
Quoted 6 lines
#define RTE_MBUF_DIRECT(mb) \ !(((const char *)(&(mb)->ol_flags))[7] & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))
#elifRTE_BYTE_ORDER==RTE_BIG_ENDIAN
/* On big endian architecture, the MSB of a 64-bit integer is at byteoffset 0. */
Quoted 29 lines
#define RTE_MBUF_DIRECT(mb) \ !(((const char *)(&(mb)->ol_flags))[0] & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))
#endif /*RTE_BYTE_ORDER*/
#endif /* !__DOXYGEN__ */
/* Verify the optimization above. */ static_assert(((RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL) & (UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not at MSB");Quoted 10 lines
Konstantin
Quoted 8 lines
Good suggestion, though.
Quoted 6 lines
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#endif +/* Verify the optimization above. */ +static_assert((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) ==
UINT64_C(0x60)<< (7 *CHAR_BIT),(quoted diff) +1
-0+ "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not 0x60at
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
MSB");
Quoted 5 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--
2.43.0
Quoted 200 lines · Morten Brørup
Quoted 75 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 17.27Quoted 15 lines
Quoted 15 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 16.05Quoted 5 lines
Quoted 5 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 10.51Review
Quoted 2 lines
(quoted diff) +0
-2-#define RTE_MBUF_DIRECT(mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * #defineRTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
RTE_MBUF_F_EXTERNAL)))Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALareboth in
Quoted 3 lines
Quoted 3 lines
the
Quoted 2 lines
MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only comparethis
Quoted 5 lines
Quoted 5 lines
one
Quoted 4 lines
Quoted 4 lines
byte instead of all
Quoted 3 lines
64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental)generates
Quoted 14 lines
Quoted 14 lines
the
Quoted 13 lines
Quoted 13 lines
following code
Review
Quoted 12 lines
for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * With this optimized macro, only 7 bytes of instructions: + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
(quoted diff) +2
-0+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bitinteger is
Quoted 6 lines
Quoted 6 lines
at
Quoted 5 lines
Quoted 5 lines
byte offset 7. */
Review
Quoted 4 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[7] &0x60)(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integeris at
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 8 lines
byte offset 0. */
Quoted 2 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[0] &0x60)A stupid q: why then not simply do:
(mb->ol_flags>> 56) &0x60
then?
Should help to all these LE/BE casts, etc.GCC is too stupid for that too.
Playing around with Godbolt shows that
return !((char)(p[3] >> 56) & 0x60); becomes movzx eax,BYTE PTR [rdi+0x1f] // 4 bytes test al,0x60 // 2 bytes Instead of simply test BYTE PTR [rdi+0x1f],0x60 // 4 bytesQuoted 1 line
Quoted 1 line
And these 2 extra bytes in instructions, are that really that
critical?
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
My guess, we wouldn't notice any real diff here.
The optimized macro made the common code path of the refactored
rte_pktmbuf_prefree_seg()fit into one cache line. IIRC, all 10 bytes saving were required for this.I understand that. but is that change will provide a measurable impact, in terms of cycles/op or pps or so?
L1 instruction cache is important; reducing code size of a per-packet function might have an effect in some cases. I don't have other metrics than code size for this optimization.
I just tested to see if I recalled correctly, and here's the generated code with the two different macros:
#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)->ol_flags))[7] &0x60)0000000000000670 <
review_rte_pktmbuf_prefree_seg>:670: f3 0f 1e fa endbr64 674: 41 57 push %r15 676: 41 56 push %r14 678: 41 55 push %r13 67a: 41 54 push %r12 67c: 55 push %rbp 67d: 53 push %rbx 67e: 48 89 fb mov %rdi,%rbx 681: 48 83 ec 18 sub $0x18,%rsp 685: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax 68c: 00 00 68e: 48 89 44 24 08 mov %rax,0x8(%rsp) 693: 31 c0 xor %eax,%eax 695: 0f b7 6f 12 movzwl 0x12(%rdi),%ebp 699: 66 83 fd 01 cmp $0x1,%bp 69d: 75 51 jne 6f0 <review_rte_pktmbuf_prefree_seg+0x80> ** Look here { 69f: f6 47 1f 60 testb $0x60,0x1f(%rdi) 6a3: 75 6b jne 710 <review_rte_pktmbuf_prefree_seg+0xa0> ** } 6a5: 66 83 7b 14 01 cmpw $0x1,0x14(%rbx) 6aa: 74 09 je 6b5 <review_rte_pktmbuf_prefree_seg+0x45> 6ac: b8 01 00 00 00 mov $0x1,%eax 6b1: 66 89 43 14 mov %ax,0x14(%rbx) 6b5: 48 83 7b 40 00 cmpq $0x0,0x40(%rbx) 6ba: 74 08 je 6c4 <review_rte_pktmbuf_prefree_seg+0x54> 6bc: 48 c7 43 40 00 00 00 movq $0x0,0x40(%rbx) 6c3: 00 6c4: 48 89 d8 mov %rbx,%rax 6c7: 48 8b 54 24 08 mov 0x8(%rsp),%rdx 6cc: 64 48 2b 14 25 28 00 sub %fs:0x28,%rdx 6d3: 00 00 6d5: 0f 85 3a 02 00 00 jne 915 <review_rte_pktmbuf_prefree_seg+0x2a5> 6db: 48 83 c4 18 add $0x18,%rsp 6df: 5b pop %rbx 6e0: 5d pop %rbp 6e1: 41 5c pop %r12 6e3: 41 5d pop %r13 6e5: 41 5e pop %r14 6e7: 41 5f pop %r15 6e9: c3 ret#define RTE_MBUF_DIRECT(mb) \ !((char)((mb)->ol_flags >> (7 * CHAR_BIT)) & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 * CHAR_BIT)))0000000000000690 <
review_rte_pktmbuf_prefree_seg>:690: f3 0f 1e fa endbr64 694: 41 57 push %r15 696: 41 56 push %r14 698: 41 55 push %r13 69a: 41 54 push %r12 69c: 55 push %rbp 69d: 53 push %rbx 69e: 48 89 fb mov %rdi,%rbx 6a1: 48 83 ec 18 sub $0x18,%rsp 6a5: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax 6ac: 00 00 6ae: 48 89 44 24 08 mov %rax,0x8(%rsp) 6b3: 31 c0 xor %eax,%eax 6b5: 0f b7 6f 12 movzwl 0x12(%rdi),%ebp 6b9: 66 83 fd 01 cmp $0x1,%bp 6bd: 75 59 jne 718 <review_rte_pktmbuf_prefree_seg+0x88> ** Look here { 6bf: 48 8b 47 18 mov 0x18(%rdi),%rax 6c3: 48 89 c2 mov %rax,%rdx 6c6: 48 c1 ea 38 shr $0x38,%rdx 6ca: 83 e2 60 and $0x60,%edx 6cd: 75 71 jne 740 <review_rte_pktmbuf_prefree_seg+0xb0> * } 6cf: 66 83 7b 14 01 cmpw $0x1,0x14(%rbx) 6d4: 74 09 je 6df <review_rte_pktmbuf_prefree_seg+0x4f> 6d6: b8 01 00 00 00 mov $0x1,%eax 6db: 66 89 43 14 mov %ax,0x14(%rbx) 6df: 48 83 7b 40 00 cmpq $0x0,0x40(%rbx) 6e4: 74 08 je 6ee <review_rte_pktmbuf_prefree_seg+0x5e> 6e6: 48 c7 43 40 00 00 00 movq $0x0,0x40(%rbx) 6ed: 00 6ee: 48 89 d8 mov %rbx,%rax 6f1: 48 8b 54 24 08 mov 0x8(%rsp),%rdx 6f6: 64 48 2b 14 25 28 00 sub %fs:0x28,%rdx 6fd: 00 00 6ff: 0f 85 50 02 00 00 jne 955 <review_rte_pktmbuf_prefree_seg+0x2c5> 705: 48 83 c4 18 add $0x18,%rsp 709: 5b pop %rbx 70a: 5d pop %rbp 70b: 41 5c pop %r12 70d: 41 5d pop %r13 70f: 41 5e pop %r14 711: 41 5f pop %r15 713: c3 retQuoted 16 lines
Quoted 8 lines
Quoted 6 lines
But if it really is, can I ask you to create a new define for
0x60, to avoid hardcoded constants in the code?
Might be something like
#defineRTE_MBUF_F_INDIRECT_EXTERNAL_1B...
or so.I started out using the field names, but Bruce suggested using
0x60for
Quoted 3 lines
readability, making the macros shorter, which IMO looks good.
I don't like adding special names just for this, so either we stick
with
0x60or go forQuoted 2 lines
"(char)((RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL) >> (7 *CHAR_BIT))", something like this:
Quoted 7 lines
Quoted 3 lines
My vote would be to use the construction above.
Might be put it in a new macro for readability.
KonstantinThe optimization requires casting
ol_flagsas a byte array and then reading the MSB; otherwise GCC and some other compilers are too stupid to perform the optimization. So, should I post a v7 with the code proposed below (to get rid of the0x60numerical value)?
While I'm not going to massively complain about removing it, I think using the numeric value is absolutely fine because we check its validity using a static_assert, which also serves to document where the constant comes from.
/Bruce
Quoted 237 lines
From: Bruce Richardson [mailto:[email protected]]
Sent: Thursday, 23 October 2025 18.04Quoted 7 lines · Morten Brørup
Quoted 7 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 17.27Quoted 4 lines
Quoted 4 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Thursday, 23 October 2025 16.05Quoted 1 line
Quoted 1 line
From: Konstantin Ananyev
[mailto:[email protected]]
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 4 lines
Quoted 4 lines
Sent: Thursday, 23 October 2025 10.51
Review
Quoted 2 lines
(quoted diff) +0
-2-#define RTE_MBUF_DIRECT(mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * #defineRTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
RTE_MBUF_F_EXTERNAL)))Quoted 2 lines
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
both in
Quoted 3 lines
Quoted 3 lines
the
Quoted 2 lines
MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we onlycompare
Quoted 8 lines
Quoted 8 lines
this
Quoted 5 lines
Quoted 5 lines
one
Quoted 4 lines
Quoted 4 lines
byte instead of all
Quoted 3 lines
64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental)generates
Quoted 1 line
Quoted 1 line
the
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
following code
Quoted 7 lines
for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes+ * With this optimized macro, only 7 bytes of
instructions:
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Review
Quoted 5 lines
+ * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ */
(quoted diff) +2
-0+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bitinteger is
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
at
Quoted 5 lines
Quoted 5 lines
byte offset 7. */
Review
Quoted 4 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[7] &0x60)(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bitinteger
Quoted 21 lines
Quoted 21 lines
is at
Quoted 20 lines
Quoted 20 lines
Quoted 11 lines
Quoted 8 lines
byte offset 0. */
Quoted 2 lines
+#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[0] &0x60)A stupid q: why then not simply do:
(mb->ol_flags>> 56) &0x60
then?
Should help to all these LE/BE casts, etc.GCC is too stupid for that too.
Quoted 7 lines
Playing around with Godbolt shows that
return !((char)(p[3] >> 56) & 0x60); becomes movzx eax,BYTE PTR [rdi+0x1f] // 4 bytes test al,0x60 // 2 bytes Instead of simply test BYTE PTR [rdi+0x1f],0x60 // 4 bytesAnd these 2 extra bytes in instructions, are that really that
Quoted 8 lines
Quoted 8 lines
critical?
Quoted 5 lines
Quoted 1 line
My guess, we wouldn't notice any real diff here.
The optimized macro made the common code path of the refactored
rte_pktmbuf_prefree_seg()fit into one cache line. IIRC, all 10 bytes saving were required for this.I understand that. but is that change will provide a measurable
impact,
Quoted 3 lines
Quoted 1 line
in terms of cycles/op or pps or so?
L1 instruction cache is important; reducing code size of a per-packet
function might have an effect in some cases.
Quoted 3 lines
I don't have other metrics than code size for this optimization.
I just tested to see if I recalled correctly, and here's the
generated code with the two different macros:
Quoted 5 lines
#define
RTE_MBUF_DIRECT(mb)!(((const char *)(&(mb)-
ol_flags))[7] &0x60)0000000000000670 <
review_rte_pktmbuf_prefree_seg>:Quoted 16 lines
670: f3 0f 1e fa endbr64 674: 41 57 push %r15 676: 41 56 push %r14 678: 41 55 push %r13 67a: 41 54 push %r12 67c: 55 push %rbp 67d: 53 push %rbx 67e: 48 89 fb mov %rdi,%rbx 681: 48 83 ec 18 sub $0x18,%rsp 685: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax 68c: 00 00 68e: 48 89 44 24 08 mov %rax,0x8(%rsp) 693: 31 c0 xor %eax,%eax 695: 0f b7 6f 12 movzwl 0x12(%rdi),%ebp 699: 66 83 fd 01 cmp $0x1,%bp 69d: 75 51 jne 6f0<
review_rte_pktmbuf_prefree_seg+0x80>Quoted 3 lines
** Look here {
69f: f6 47 1f 60 testb $0x60,0x1f(%rdi) 6a3: 75 6b jne 710<
review_rte_pktmbuf_prefree_seg+0xa0>Quoted 3 lines
** }
6a5: 66 83 7b 14 01 cmpw $0x1,0x14(%rbx) 6aa: 74 09 je 6b5<
review_rte_pktmbuf_prefree_seg+0x45>Quoted 4 lines
6ac: b8 01 00 00 00 mov $0x1,%eax 6b1: 66 89 43 14 mov %ax,0x14(%rbx) 6b5: 48 83 7b 40 00 cmpq $0x0,0x40(%rbx) 6ba: 74 08 je 6c4<
review_rte_pktmbuf_prefree_seg+0x54>Quoted 7 lines
6bc: 48 c7 43 40 00 00 00 movq $0x0,0x40(%rbx) 6c3: 00 6c4: 48 89 d8 mov %rbx,%rax 6c7: 48 8b 54 24 08 mov 0x8(%rsp),%rdx 6cc: 64 48 2b 14 25 28 00 sub %fs:0x28,%rdx 6d3: 00 00 6d5: 0f 85 3a 02 00 00 jne 915<
review_rte_pktmbuf_prefree_seg+0x2a5>Quoted 9 lines
6db: 48 83 c4 18 add $0x18,%rsp 6df: 5b pop %rbx 6e0: 5d pop %rbp 6e1: 41 5c pop %r12 6e3: 41 5d pop %r13 6e5: 41 5e pop %r14 6e7: 41 5f pop %r15 6e9: c3 retQuoted 3 lines
#define RTE_MBUF_DIRECT(mb) \ !((char)((mb)->ol_flags >> (7 * CHAR_BIT)) & \ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))Quoted 18 lines
0000000000000690 <
review_rte_pktmbuf_prefree_seg>:690: f3 0f 1e fa endbr64 694: 41 57 push %r15 696: 41 56 push %r14 698: 41 55 push %r13 69a: 41 54 push %r12 69c: 55 push %rbp 69d: 53 push %rbx 69e: 48 89 fb mov %rdi,%rbx 6a1: 48 83 ec 18 sub $0x18,%rsp 6a5: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax 6ac: 00 00 6ae: 48 89 44 24 08 mov %rax,0x8(%rsp) 6b3: 31 c0 xor %eax,%eax 6b5: 0f b7 6f 12 movzwl 0x12(%rdi),%ebp 6b9: 66 83 fd 01 cmp $0x1,%bp 6bd: 75 59 jne 718<
review_rte_pktmbuf_prefree_seg+0x88>Quoted 6 lines
** Look here { 6bf: 48 8b 47 18 mov 0x18(%rdi),%rax 6c3: 48 89 c2 mov %rax,%rdx 6c6: 48 c1 ea 38 shr $0x38,%rdx 6ca: 83 e2 60 and $0x60,%edx 6cd: 75 71 jne 740<
review_rte_pktmbuf_prefree_seg+0xb0>Quoted 3 lines
* }
6cf: 66 83 7b 14 01 cmpw $0x1,0x14(%rbx) 6d4: 74 09 je 6df<
review_rte_pktmbuf_prefree_seg+0x4f>Quoted 4 lines
6d6: b8 01 00 00 00 mov $0x1,%eax 6db: 66 89 43 14 mov %ax,0x14(%rbx) 6df: 48 83 7b 40 00 cmpq $0x0,0x40(%rbx) 6e4: 74 08 je 6ee<
review_rte_pktmbuf_prefree_seg+0x5e>Quoted 7 lines
6e6: 48 c7 43 40 00 00 00 movq $0x0,0x40(%rbx) 6ed: 00 6ee: 48 89 d8 mov %rbx,%rax 6f1: 48 8b 54 24 08 mov 0x8(%rsp),%rdx 6f6: 64 48 2b 14 25 28 00 sub %fs:0x28,%rdx 6fd: 00 00 6ff: 0f 85 50 02 00 00 jne 955<
review_rte_pktmbuf_prefree_seg+0x2c5>Quoted 11 lines
705: 48 83 c4 18 add $0x18,%rsp 709: 5b pop %rbx 70a: 5d pop %rbp 70b: 41 5c pop %r12 70d: 41 5d pop %r13 70f: 41 5e pop %r14 711: 41 5f pop %r15 713: c3 retQuoted 2 lines
Quoted 2 lines
Quoted 2 lines
But if it really is, can I ask you to create a new define for
0x60,Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
to avoid hardcoded constants in the code?
Might be something like
#defineRTE_MBUF_F_INDIRECT_EXTERNAL_1B...
or so.I started out using the field names, but Bruce suggested using
0x60Quoted 4 lines
Quoted 4 lines
for
Quoted 3 lines
readability, making the macros shorter, which IMO looks good.
I don't like adding special names just for this, so either we
stick
Quoted 9 lines
Quoted 7 lines
with
0x60or go forQuoted 2 lines
"(char)((RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL) >> (7 *CHAR_BIT))", something like this:My vote would be to use the construction above.
Might be put it in a new macro for readability.
KonstantinThe optimization requires casting
ol_flagsas a byte array and thenreading the MSB; otherwise GCC and some other compilers are too stupid to perform the optimization.
Quoted 1 line
So, should I post a v7 with the code proposed below (to get rid of
the
0x60numerical value)?Quoted 1 line
Quoted 8 lines
While I'm not going to massively complain about removing it, I think using
the numeric value is absolutely fine because we check its validity using astatic_assert, which also serves to document where the constant comes from./Bruce
I have posted a v7 patch, so you can all see how it looks with long names vs. numerical value.
I just want the optimization, and have no strong preference of one or the other.
If none of you have any strong opinions, let's let @Thomas choose. :-)
PATCH v6 1/1 mbuf: optimize segment prefree
In reply to Morten BrørupRefactored rte_pktmbuf_prefree_seg() for both performance and readability.
With the optimized RTE_MBUF_DIRECT() macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.
---
v6:
* Fixed Doxygen error in RTE_MBUF_DIRECT() macro description. (Thomas Monjalon)
* Added specific RTE_MBUF_DIRECT() macro implementation for API documentation purposes, using flag names instead of numerical value.
v5:
* Removed the plain RTE_MBUF_DIRECT() macro, and only use the optimized variant. (Bruce Richardson)
Further testing on Godbolt confirmed that other compilers benefit from the optimized macro too.
* Shortened the description of the RTE_MBUF_DIRECT() macro, and only provide one example of code emitted by a compiler. (Bruce Richardson)
* Consolidated the static_assert() into one, covering both little and big endian.
This also reduces the amount of endian-conditional source code and improves readability.
(Bruce Richardson)
* Added comment about MSB meaning "most significant byte".
v4:
* Enabled the optimized RTE_MBUF_DIRECT() macro for GCC on all architectures.
v3:
* Rewrote the optimized RTE_MBUF_DIRECT() macro for readability; use numerical value instead of long names. (Bruce Richardson)
* Enabled the optimized RTE_MBUF_DIRECT() macro for Loongarch too. v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimized RTE_MBUF_DIRECT() macro. (Stephen Hemminger)
* Added static_assert() to verify that the optimized RTE_MBUF_DIRECT() macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.
--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 43 +++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 34 deletions(-)
lib/mbuf/rte_mbuf.h +19 -32
@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h>
@@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +41 -2
50 lines not on this page · raw message
Signature · 1 line
2.43.0
Thomas Monjalon
Applied23/10/2025 14:48, Morten Brørup:
Quoted 9 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance and readability.With the optimized
RTE_MBUF_DIRECT()macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>
Applied, thanks.
Note: v7 is not chosen because long names make this difficult macro even more complicated to read.
PATCH v7 1/1 mbuf: optimize segment prefree
In reply to Morten BrørupRefactored rte_pktmbuf_prefree_seg() for both performance and readability.
With the optimized RTE_MBUF_DIRECT() macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.
---
v7:
* Go back to long names instead of numerical value in RTE_MBUF_DIRECT() macro.
(Konstantin Ananyev)
* Updated static_assert() accordingly.
v6:
* Fixed Doxygen error in RTE_MBUF_DIRECT() macro description. (Thomas Monjalon)
* Added specific RTE_MBUF_DIRECT() macro implementation for API documentation purposes, using flag names instead of numerical value.
v5:
* Removed the plain RTE_MBUF_DIRECT() macro, and only use the optimized variant. (Bruce Richardson)
Further testing on Godbolt confirmed that other compilers benefit from the optimized macro too.
* Shortened the description of the RTE_MBUF_DIRECT() macro, and only provide one example of code emitted by a compiler. (Bruce Richardson)
* Consolidated the static_assert() into one, covering both little and big endian.
This also reduces the amount of endian-conditional source code and improves readability.
(Bruce Richardson)
* Added comment about MSB meaning "most significant byte". v4:
* Enabled the optimized RTE_MBUF_DIRECT() macro for GCC on all architectures.
v3:
* Rewrote the optimized RTE_MBUF_DIRECT() macro for readability; use numerical value instead of long names. (Bruce Richardson)
* Enabled the optimized RTE_MBUF_DIRECT() macro for Loongarch too. v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimized RTE_MBUF_DIRECT() macro. (Stephen Hemminger)
* Added static_assert() to verify that the optimized RTE_MBUF_DIRECT() macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.
--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 48 +++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 34 deletions(-)
lib/mbuf/rte_mbuf.h +19 -32
@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h>
@@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +46 -2
55 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 197 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance and readability.With the optimized
RTE_MBUF_DIRECT()macro, the common likely code path now fits within one instruction cache line on x86-64 when built with GCC.Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v7:
* Go back to long names instead of numerical value inRTE_MBUF_DIRECT()macro.
(Konstantin Ananyev)
* Updatedstatic_assert()accordingly.
v6:
* Fixed Doxygen error inRTE_MBUF_DIRECT()macro description. (Thomas Monjalon)
* Added specificRTE_MBUF_DIRECT()macro implementation for API documentation purposes, using flag names instead of numerical value.
v5:
* Removed the plainRTE_MBUF_DIRECT()macro, and only use the optimized variant. (Bruce Richardson)
Further testing on Godbolt confirmed that other compilers benefit from the optimized macro too.
* Shortened the description of theRTE_MBUF_DIRECT()macro, and only provide one example of code emitted by a compiler. (Bruce Richardson)
* Consolidated thestatic_assert()into one, covering both little and big endian.
This also reduces the amount of endian-conditional source code and improves readability.
(Bruce Richardson)
* Added comment about MSB meaning "most significant byte". v4:
* Enabled the optimizedRTE_MBUF_DIRECT()macro for GCC on all architectures.
v3:
* Rewrote the optimizedRTE_MBUF_DIRECT()macro for readability; use numerical value instead of long names. (Bruce Richardson)
* Enabled the optimizedRTE_MBUF_DIRECT()macro for Loongarch too. v2:
* Fixed typo in commit description.
* Fixed indentation.
* Added detailed description to the optimizedRTE_MBUF_DIRECT()macro. (Stephen Hemminger)
* Addedstatic_assert()to verify that the optimizedRTE_MBUF_DIRECT()macro is valid, specifically that the tested bits are in the MSB of the 64-bit field.--- lib/mbuf/rte_mbuf.h | 51 +++++++++++++++------------------------- lib/mbuf/rte_mbuf_core.h | 48 +++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 34 deletions(-)lib/mbuf/rte_mbuf.h +19
-32@@ -31,6 +31,7 @@* http://www.kohala.com/start/tcpipiv2.html */ +#include <stdbool.h> #include <stdint.h> #include <rte_common.h> @@ -1458,44 +1459,30 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg (struct rte_mbuf *m) { -__rte_mbuf_sanity_check (m,0 ); - -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +bool refcnt_not_one; -return m; +__rte_mbuf_sanity_check (m,0 ); - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { + refcnt_not_one =unlikely (rte_mbuf_refcnt_read (m) !=1 ); +if (refcnt_not_one &&__rte_mbuf_refcnt_update (m,-1 ) !=0 ) +return NULL ; -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } +if (unlikely (!RTE_MBUF_DIRECT (m))) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) +return NULL ; + } -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (refcnt_not_one)rte_mbuf_refcnt_set (m,1 ); +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +if (m->next !=NULL ) + m->next =NULL ; -return m; - } -return NULL ; +return m; }/**
lib/mbuf/rte_mbuf_core.h +10
-1@@ -711,9 +711,53 @@ struct rte_mbuf_ext_shared_info {* * If a mbuf embeds its own data after the rte_mbuf structure, this mbuf * can be defined as a direct mbuf. -*/ +* +* Note: Macro optimized for code size. +* +* The plain macro would be: +* \code{.c} +* #define RTE_MBUF_DIRECT(mb) \ +* (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL))) +* \endcode +* +* The flags RTE_MBUF_F_INDIRECT and RTE_MBUF_F_EXTERNAL are both in the MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare this one byte instead of all 64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generates the following code for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:
+ * \code+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * \endcode + * With this optimized macro, only 7 bytes of instructions: + * \code + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ * \endcode
+ */lib/mbuf/rte_mbuf_core.h +3
-0+#ifdef __DOXYGEN__ +#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET/* 7 or 0, depending on endianness */] & \
lib/mbuf/rte_mbuf_core.h +1
-0+ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +6
-0+#else /* !__DOXYGEN__ */ +#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. */ +#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[7] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +2
-1+#elif RTE_BYTE_ORDER ==RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is at byte offset 0. */ #define RTE_MBUF_DIRECT (mb) \ - (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))lib/mbuf/rte_mbuf_core.h +2
-0+ !(((const char *)(&(mb)->ol_flags))[0] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +4
-0+#endif /* RTE_BYTE_ORDER */ +#endif /* !__DOXYGEN__ */ +/* Verify the optimization above. */ +static_assert(((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) &(
UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +2
-1+ (RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ), + "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not at MSB");/** Uninitialized or unspecified port. */ #define RTE_MBUF_PORT_INVALID UINT16_MAX --
LGTM, thanks for refactoring.
Quoted 1 line
2.43.0
Quoted 23 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Friday, 24 October 2025 10.20Quoted 2 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likely codepath
Quoted 1 line
now fits within one instruction cache line on x86-64 when built with
GCC.
Quoted 8 lines
Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v7:
* Go back to long names instead of numerical value in
RTE_MBUF_DIRECT()Quoted 3 lines
macro.
(Konstantin Ananyev)
* Updatedstatic_assert()accordingly.
[...]
Quoted 69 lines
Quoted 2 lines
*
* If a mbuf embeds its own data after therte_mbufstructure, thismbuf
Quoted 7 lines
* can be defined as a direct mbuf.
- */
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * \code{.c}Quoted 8 lines
+ * #define
RTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL)))
+ * \endcode
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare both in the MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare this onebyte instead of
Quoted 3 lines
all 64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generates thefollowing
Quoted 14 lines
code for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:
+ * \code+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * \endcode + * With this optimized macro, only 7 bytes of instructions: + * \code + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ * \endcode
+ */lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 3 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#ifdef __DOXYGEN__ +#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET/* 7 or 0, depending on
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
endianness */] & \
(quoted diff) +1
-0+ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +3
-0+#else /* !__DOXYGEN__ */ +#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. */
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[7] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is atbyte offset 0. */
Quoted 14 lines
#define
RTE_MBUF_DIRECT(mb)\(quoted diff) +0
-1- (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))(quoted diff) +2
-0+ !(((const char *)(&(mb)->ol_flags))[0] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +4
-0+#endif /* RTE_BYTE_ORDER */ +#endif /* !__DOXYGEN__ */ +/* Verify the optimization above. */ +static_assert(((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) &(
UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==lib/mbuf/rte_mbuf_core.h +2
-0+ (RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ), + "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not at MSB");
Quoted 5 lines
Quoted 3 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--LGTM, thanks for refactoring.
Thank you for reviewing, Konstantin.
I had no preference for v7 or v6, but Bruce and Thomas preferred v6, so v6 was applied.
Quoted 99 lines
Quoted 21 lines
Quoted 3 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likely codepath
Quoted 1 line
now fits within one instruction cache line on x86-64 when built with
GCC.
Quoted 8 lines
Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v7:
* Go back to long names instead of numerical value in
RTE_MBUF_DIRECT()Quoted 3 lines
macro.
(Konstantin Ananyev)
* Updatedstatic_assert()accordingly.[...]
Quoted 69 lines
Quoted 2 lines
*
* If a mbuf embeds its own data after therte_mbufstructure, thismbuf
Quoted 7 lines
* can be defined as a direct mbuf.
- */
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * \code{.c}Quoted 8 lines
+ * #define
RTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL)))
+ * \endcode
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare both in the MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare this onebyte instead of
Quoted 3 lines
all 64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generates thefollowing
Quoted 14 lines
code for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:
+ * \code+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * \endcode + * With this optimized macro, only 7 bytes of instructions: + * \code + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ * \endcode
+ */lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 3 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#ifdef __DOXYGEN__ +#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET/* 7 or 0, depending on
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
endianness */] & \
(quoted diff) +1
-0+ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +3
-0+#else /* !__DOXYGEN__ */ +#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. */
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[7] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is atbyte offset 0. */
Quoted 14 lines
#define
RTE_MBUF_DIRECT(mb)\(quoted diff) +0
-1- (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))(quoted diff) +2
-0+ !(((const char *)(&(mb)->ol_flags))[0] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +4
-0+#endif /* RTE_BYTE_ORDER */ +#endif /* !__DOXYGEN__ */ +/* Verify the optimization above. */ +static_assert(((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) &(
UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==lib/mbuf/rte_mbuf_core.h +2
-0+ (RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ), + "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not at MSB");Quoted 5 lines
Quoted 3 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--LGTM, thanks for refactoring.
Quoted 4 lines
Thank you for reviewing, Konstantin.
I had no preference for v7 or v6, but Bruce and Thomas preferred v6, so v6 was applied.
Yes, I saw Thomas email, after I sent my reply already. Looks like I was late with my vote.
My preference still would be to avoid hard-coded constants in the code, but seems that it is just me.
Konstantin
24/10/2025 11:21, Konstantin Ananyev:
Quoted 100 lines
Quoted 100 lines
Quoted 20 lines
Quoted 2 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likely codepath
Quoted 1 line
now fits within one instruction cache line on x86-64 when built with
GCC.
Quoted 8 lines
Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v7:
* Go back to long names instead of numerical value in
RTE_MBUF_DIRECT()Quoted 3 lines
macro.
(Konstantin Ananyev)
* Updatedstatic_assert()accordingly.[...]
Quoted 69 lines
Quoted 2 lines
*
* If a mbuf embeds its own data after therte_mbufstructure, thismbuf
Quoted 7 lines
* can be defined as a direct mbuf.
- */
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * \code{.c}Quoted 8 lines
+ * #define
RTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL)))
+ * \endcode
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare both in the MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare this onebyte instead of
Quoted 3 lines
all 64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generates thefollowing
Quoted 14 lines
code for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:
+ * \code+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * \endcode + * With this optimized macro, only 7 bytes of instructions: + * \code + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ * \endcode
+ */lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 3 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#ifdef __DOXYGEN__ +#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET/* 7 or 0, depending on
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
endianness */] & \
(quoted diff) +1
-0+ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +3
-0+#else /* !__DOXYGEN__ */ +#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. */
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[7] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is atbyte offset 0. */
Quoted 14 lines
#define
RTE_MBUF_DIRECT(mb)\(quoted diff) +0
-1- (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))(quoted diff) +2
-0+ !(((const char *)(&(mb)->ol_flags))[0] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +4
-0+#endif /* RTE_BYTE_ORDER */ +#endif /* !__DOXYGEN__ */ +/* Verify the optimization above. */ +static_assert(((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) &(
UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==lib/mbuf/rte_mbuf_core.h +2
-0+ (RTE_MBUF_F_INDIRECT |RTE_MBUF_F_EXTERNAL ), + "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not at MSB");Quoted 5 lines
Quoted 3 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--LGTM, thanks for refactoring.
Thank you for reviewing, Konstantin.
Quoted 7 lines
Quoted 2 lines
I had no preference for v7 or v6, but Bruce and Thomas preferred v6, so v6 was applied.
Yes, I saw Thomas email, after I sent my reply already. Looks like I was late with my vote.
My preference still would be to avoid hard-coded constants in the code, but seems that it is just me.
Me too I want to avoid hardcoded constants.
But in this case, it is very well documented,
and there is a trade-off with length and reading.
The comment starts with
* The plain macro would be:
* \code{.c}
* #define RTE_MBUF_DIRECT(mb) \
* (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL)))
* \endcode
so I believe it is very clear already.
Quoted 107 lines
Quoted 28 lines
Quoted 28 lines
Quoted 22 lines
Quoted 4 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likely codepath
Quoted 1 line
now fits within one instruction cache line on x86-64 when built with
GCC.
Quoted 8 lines
Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v7:
* Go back to long names instead of numerical value in
RTE_MBUF_DIRECT()Quoted 3 lines
macro.
(Konstantin Ananyev)
* Updatedstatic_assert()accordingly.[...]
Quoted 3 lines
Quoted 2 lines
*
* If a mbuf embeds its own data after therte_mbufstructure, thismbuf
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
* can be defined as a direct mbuf.
- */
+ *
+ * Note: Macro optimized for code size.
+ *Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
+ * The plain macro would be:
+ * \code{.c}
+ * #defineRTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL)))
+ * \endcode
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALare bothin
Quoted 39 lines
Quoted 39 lines
Quoted 39 lines
Quoted 2 lines
the MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only compare this onebyte instead of
Quoted 3 lines
all 64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental) generates thefollowing
Quoted 14 lines
code for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:
+ * \code+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * \endcode + * With this optimized macro, only 7 bytes of instructions: + * \code + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ * \endcode
+ */lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 3 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#ifdef __DOXYGEN__ +#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET/* 7 or 0, depending on
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
endianness */] & \
(quoted diff) +1
-0+ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +3
-0+#else /* !__DOXYGEN__ */ +#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is at byte offset 7. */
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[7] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integer is atbyte offset 0. */
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
#define
RTE_MBUF_DIRECT(mb)\(quoted diff) +0
-1- (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))(quoted diff) +2
-0+ !(((const char *)(&(mb)->ol_flags))[0] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +4
-0+#endif /* RTE_BYTE_ORDER */ +#endif /* !__DOXYGEN__ */ +/* Verify the optimization above. */ +static_assert(((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) &(
UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==(quoted diff) +2
-0+ (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), + "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not atMSB");
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--LGTM, thanks for refactoring.
Thank you for reviewing, Konstantin.
I had no preference for v7 or v6, but Bruce and Thomas preferred v6, so v6 was applied.
Quoted 8 lines
Quoted 4 lines
Yes, I saw Thomas email, after I sent my reply already. Looks like I was late with my vote.
My preference still would be to avoid hard-coded constants in the code, but seems that it is just me.Me too I want to avoid hardcoded constants.
But in this case, it is very well documented,
and there is a trade-off with length and reading.
If we allow hard-coded constants in one place,
then it would be harder for us to disallow them in other places. If we allow them everywhere - code will become a mess pretty soon. Anyway, the changes are already merged, so probably not point to keep arguing on that subject.
Quoted 10 lines
The comment starts with
* The plain macro would be:
* \code{.c}
* #defineRTE_MBUF_DIRECT(mb)\
* (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL)))
* \endcodeso I believe it is very clear already.
Quoted 123 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Saturday, 25 October 2025 12.25Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likelycode
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
path
Quoted 1 line
now fits within one instruction cache line on x86-64 when
built with
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
GCC.
Quoted 8 lines
Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v7:
* Go back to long names instead of numerical value in
RTE_MBUF_DIRECT()Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
macro.
(Konstantin Ananyev)
* Updatedstatic_assert()accordingly.[...]
Quoted 2 lines
Quoted 2 lines
*
* If a mbuf embeds its own data after therte_mbufstructure, this
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
mbuf
Quoted 13 lines
* can be defined as a direct mbuf.
- */
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * \code{.c}
+ * #defineRTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL)))
+ * \endcode
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALareboth
Quoted 3 lines
in
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
the MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only comparethis one
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
byte instead of
Quoted 3 lines
all 64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental)generates the
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
following
Quoted 14 lines
code for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:
+ * \code+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * \endcode + * With this optimized macro, only 7 bytes of instructions: + * \code + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ * \endcode
+ */lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 3 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#ifdef __DOXYGEN__ +#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET/* 7 or 0, depending on
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h struct rte_mbuf_ext_shared_info {
Quoted 6 lines
endianness */] & \
(quoted diff) +1
-0+ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +3
-0+#else /* !__DOXYGEN__ */ +#if RTE_BYTE_ORDER ==RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bit integer is at
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
byte offset 7. */
Review
Quoted 6 lines
lib/mbuf/rte_mbuf_core.h +3
-0+#define RTE_MBUF_DIRECT (mb) \ + !(((const char *)(&(mb)->ol_flags))[7] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integeris at
Quoted 25 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
byte offset 0. */
Review
Quoted 13 lines
#define
RTE_MBUF_DIRECT(mb)\(quoted diff) +0
-1- (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))(quoted diff) +2
-0+ !(((const char *)(&(mb)->ol_flags))[0] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))lib/mbuf/rte_mbuf_core.h +4
-0+#endif /* RTE_BYTE_ORDER */ +#endif /* !__DOXYGEN__ */ +/* Verify the optimization above. */ +static_assert(((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) &(
UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==(quoted diff) +2
-0+ (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), + "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not atMSB");
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--LGTM, thanks for refactoring.
Thank you for reviewing, Konstantin.
I had no preference for v7 or v6, but Bruce and Thomas preferred
v6, so v6 was
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
applied.
Yes, I saw Thomas email, after I sent my reply already. Looks like I was late with my vote.
My preference still would be to avoid hard-coded constants in thecode,
Quoted 2 lines
Quoted 1 line
but seems that it is just me.
Quoted 7 lines
Quoted 3 lines
Me too I want to avoid hardcoded constants.
But in this case, it is very well documented,
and there is a trade-off with length and reading.If we allow hard-coded constants in one place,
then it would be harder for us to disallow them in other places. If we allow them everywhere - code will become a mess pretty soon.
Vector code has plenty of hardcoded constants. E.g.:
https://elixir.bootlin.com/dpdk/v25.07/source/drivers/net/intel/i40e/i40e_rxtx_vec_avx512.c#L154
So we already do allow it.
However, I think uses of numerical constants (like 0x60 here) should always be accompanied by a static_assert(), so a change of the underlying value will be caught at build time.
Quoted 2 lines
Anyway, the changes are already merged, so probably not point to keep arguing on that subject.
I normally have a strong preference for descriptive names over numbers, but in this case, I was in doubt. A majority of reviewers voted that using 0x60 made the code easier to read, so let's stick with that.
Quoted 12 lines
Quoted 7 lines
The comment starts with
* The plain macro would be:
* \code{.c}
* #defineRTE_MBUF_DIRECT(mb)\
* (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|
RTE_MBUF_F_EXTERNAL)))Quoted 4 lines
* \endcode
so I believe it is very clear already.
Quoted 129 lines
Quoted 129 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Saturday, 25 October 2025 12.25Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likelycode
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
path
Quoted 1 line
now fits within one instruction cache line on x86-64 when
built with
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
GCC.
Quoted 8 lines
Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v7:
* Go back to long names instead of numerical value in
RTE_MBUF_DIRECT()Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
macro.
(Konstantin Ananyev)
* Updatedstatic_assert()accordingly.[...]
Quoted 2 lines
Quoted 2 lines
*
* If a mbuf embeds its own data after therte_mbufstructure, this
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
mbuf
Quoted 13 lines
* can be defined as a direct mbuf.
- */
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * \code{.c}
+ * #defineRTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL)))
+ * \endcode
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALareboth
Quoted 3 lines
in
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
the MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only comparethis one
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
byte instead of
Quoted 3 lines
all 64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental)generates the
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
following
Quoted 14 lines
code for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:
+ * \code+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * \endcode + * With this optimized macro, only 7 bytes of instructions: + * \code + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ * \endcode
+ */Review
Quoted 3 lines
(quoted diff) +3
-0+#ifdef __DOXYGEN__ +#define RTE_MBUF_DIRECT(mb) \ + !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET /* 7 or 0,depending on
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Review
Quoted 6 lines
endianness */] & \
(quoted diff) +1
-0+ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +3
-0+#else /* !__DOXYGEN__ */ +#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bitinteger is at
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
byte offset 7. */
Review
Quoted 6 lines
(quoted diff) +3
-0+#define RTE_MBUF_DIRECT(mb) \ + !(((const char *)(&(mb)->ol_flags))[7] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integeris at
Quoted 25 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
byte offset 0. */
Review
Quoted 13 lines
#define
RTE_MBUF_DIRECT(mb)\(quoted diff) +0
-1- (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))(quoted diff) +2
-0+ !(((const char *)(&(mb)->ol_flags))[0] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +4
-0+#endif /* RTE_BYTE_ORDER */ +#endif /* !__DOXYGEN__ */ +/* Verify the optimization above. */ +static_assert(((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) &(
UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==(quoted diff) +2
-0+ (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), + "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not atMSB");
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--LGTM, thanks for refactoring.
Thank you for reviewing, Konstantin.
I had no preference for v7 or v6, but Bruce and Thomas preferred
v6, so v6 was
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
applied.
Yes, I saw Thomas email, after I sent my reply already. Looks like I was late with my vote.
My preference still would be to avoid hard-coded constants in thecode,
Quoted 5 lines
Quoted 1 line
but seems that it is just me.
Me too I want to avoid hardcoded constants.
But in this case, it is very well documented,
and there is a trade-off with length and reading.
Quoted 8 lines
Quoted 3 lines
If we allow hard-coded constants in one place,
then it would be harder for us to disallow them in other places. If we allow them everywhere - code will become a mess pretty soon.Vector code has plenty of hardcoded constants. E.g.:
https://elixir.bootlin.com/dpdk/v25.07/source/drivers/net/intel/i40e/i40e_rxtx_vec
_avx512.c#L154
So we already do allow it.
It is a SIMD code for specific architecture (avx512) and specific driver. mbuf.h is a core header in our core library - I expect the code cleanness requirements to be higher for it.
Quoted 3 lines
However, I think uses of numerical constants (like
0x60here) should always be accompanied by astatic_assert(), so a change of the underlying value will be caught at build time.
Yes, they should be accompanied with static assert - completely agree with you here. Though even with static asserts using them in uncontrolled way will make code a mess. Macros were invented for good purpose.
Quoted 19 lines
Quoted 2 lines
Anyway, the changes are already merged, so probably not point to keep arguing on that subject.
I normally have a strong preference for descriptive names over numbers, but in this case, I was in doubt. A majority of reviewers voted that using
0x60made the code easier to read, so let's stick with that.Quoted 12 lines
Quoted 7 lines
The comment starts with
* The plain macro would be:
* \code{.c}
* #defineRTE_MBUF_DIRECT(mb)\
* (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|
RTE_MBUF_F_EXTERNAL)))Quoted 4 lines
* \endcode
so I believe it is very clear already.
27/10/2025 09:54, Konstantin Ananyev:
Quoted 131 lines
Quoted 131 lines
Quoted 130 lines
From: Konstantin Ananyev [mailto:[email protected]]
Sent: Saturday, 25 October 2025 12.25Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Refactored
rte_pktmbuf_prefree_seg()for both performance andreadability.
Quoted 2 lines
With the optimized
RTE_MBUF_DIRECT()macro, the common likelycode
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
path
Quoted 1 line
now fits within one instruction cache line on x86-64 when
built with
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
GCC.
Quoted 8 lines
Signed-off-by Morten Brørup <[email protected]>Acked-by Chengwen Feng <[email protected]>Reviewed-by Bruce Richardson <[email protected]>---
v7:
* Go back to long names instead of numerical value in
RTE_MBUF_DIRECT()Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
macro.
(Konstantin Ananyev)
* Updatedstatic_assert()accordingly.[...]
Quoted 2 lines
Quoted 2 lines
*
* If a mbuf embeds its own data after therte_mbufstructure, this
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
mbuf
Quoted 13 lines
* can be defined as a direct mbuf.
- */
+ *
+ * Note: Macro optimized for code size.
+ *
+ * The plain macro would be:
+ * \code{.c}
+ * #defineRTE_MBUF_DIRECT(mb)\
+ * (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL)))
+ * \endcode
+ *
+ * The flagsRTE_MBUF_F_INDIRECTandRTE_MBUF_F_EXTERNALareboth
Quoted 3 lines
in
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
the MSB (most significant
+ * byte) of the 64-bitol_flagsfield, so we only comparethis one
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
byte instead of
Quoted 3 lines
all 64 bits.
+ *
+ * E.g., GCC version 16.0.0 20251019 (experimental)generates the
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
following
Quoted 14 lines
code for x86-64.
+ *
+ * With the plain macro, 17 bytes of instructions:
+ * \code+ * movabs rax,0x6000000000000000 // 10 bytes + * and rax,QWORD PTR [rdi+0x18] // 4 bytes + * sete al // 3 bytes + * \endcode + * With this optimized macro, only 7 bytes of instructions: + * \code + * test BYTE PTR [rdi+0x1f],0x60 // 4 bytes + * sete al // 3 bytes+ * \endcode
+ */Review
Quoted 3 lines
(quoted diff) +3
-0+#ifdef __DOXYGEN__ +#define RTE_MBUF_DIRECT(mb) \ + !(((const char *)(&(mb)->ol_flags))[MSB_OFFSET /* 7 or 0,depending on
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Review
Quoted 6 lines
endianness */] & \
(quoted diff) +1
-0+ (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +3
-0+#else /* !__DOXYGEN__ */ +#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +/* On little endian architecture, the MSB of a 64-bitinteger is at
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
byte offset 7. */
Review
Quoted 6 lines
(quoted diff) +3
-0+#define RTE_MBUF_DIRECT(mb) \ + !(((const char *)(&(mb)->ol_flags))[7] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +2
-0+#elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN +/* On big endian architecture, the MSB of a 64-bit integeris at
Quoted 25 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
byte offset 0. */
Review
Quoted 13 lines
#define
RTE_MBUF_DIRECT(mb)\(quoted diff) +0
-1- (!((mb)->ol_flags & (RTE_MBUF_F_INDIRECT |
RTE_MBUF_F_EXTERNAL)))(quoted diff) +2
-0+ !(((const char *)(&(mb)->ol_flags))[0] & \ + (char)((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) >> (7 *
CHAR_BIT)))(quoted diff) +4
-0+#endif /* RTE_BYTE_ORDER */ +#endif /* !__DOXYGEN__ */ +/* Verify the optimization above. */ +static_assert(((RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) &(
UINT64_C(0xFF)<< (7 *CHAR_BIT))) ==(quoted diff) +2
-0+ (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL), + "(RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL) is not atMSB");
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
/** Uninitialized or unspecified port. */
#defineRTE_MBUF_PORT_INVALIDUINT16_MAX
--LGTM, thanks for refactoring.
Thank you for reviewing, Konstantin.
I had no preference for v7 or v6, but Bruce and Thomas preferred
v6, so v6 was
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
applied.
Yes, I saw Thomas email, after I sent my reply already. Looks like I was late with my vote.
My preference still would be to avoid hard-coded constants in thecode,
Quoted 5 lines
Quoted 1 line
but seems that it is just me.
Me too I want to avoid hardcoded constants.
But in this case, it is very well documented,
and there is a trade-off with length and reading.If we allow hard-coded constants in one place,
then it would be harder for us to disallow them in other places. If we allow them everywhere - code will become a mess pretty soon.
Quoted 8 lines
Quoted 4 lines
Vector code has plenty of hardcoded constants. E.g.:
https://elixir.bootlin.com/dpdk/v25.07/source/drivers/net/intel/i40e/i40e_rxtx_vec
_avx512.c#L154
So we already do allow it.It is a SIMD code for specific architecture (avx512) and specific driver.
mbuf.his a core header in our core library - I expect the code cleanness requirements to be higher for it.
Feel free to propose a patch, and I'll apply it if several reviewers like it.