Test-Label: ai-code-review
Test-Status: SUCCESS
https://dpdk.org/patch/163800
_AI Code Review_
=== Patch Review: series.patch (via Claude) ===
Review date: 2026-05-08
Reviewing commit: eal/x86: optimize memcpy of small sizes
Author: Morten Brorup <mb at smartsharesystems.com>
Subject: [PATCH v10] eal/x86: optimize memcpy of small sizes
This patch optimizes the x86 memcpy implementation by consolidating the
handling of copies up to 64 bytes into a common location before alignment
checks, adds __rte_restrict qualifiers, and implements the missing
rte_mov48() function.
> diff --git a/lib/eal/x86/include/rte_memcpy.h b/lib/eal/x86/include/rte_memcpy.h
> index 46d34b8081..d6dfac19aa 100644
> --- a/lib/eal/x86/include/rte_memcpy.h
> +++ b/lib/eal/x86/include/rte_memcpy.h
[ ... ]
> @@ -22,6 +22,7 @@
> extern "C" {
> #endif
>
> +/* Workaround for drivers using elems[1] instead of elems[] for flexible arrays. */
> #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 100000)
> #pragma GCC diagnostic push
> #pragma GCC diagnostic ignored "-Wstringop-overflow"
[ ... ]
> @@ -40,9 +41,6 @@ extern "C" {
> /**
> * Copy bytes from one location to another. The locations must not overlap.
> *
> - * @note This is implemented as a macro, so it's address should not be taken
> - * and care is needed as parameter expressions may be evaluated multiple times.
> - *
The removed documentation note is outdated since rte_memcpy() is now an
inline function rather than a macro. Does the current implementation still
evaluate parameter expressions multiple times in __rte_constant() checks,
or has this been fully addressed?
[ ... ]
> @@ -53,15 +51,15 @@ extern "C" {
> * Pointer to the destination data.
> */
> static __rte_always_inline void *
> -rte_memcpy(void *dst, const void *src, size_t n);
> +rte_memcpy(void *__rte_restrict dst, const void *__rte_restrict src, size_t n);
[ ... ]
> @@ -132,12 +130,29 @@ rte_mov32(uint8_t *dst, const uint8_t *src)
> #endif
> }
>
> +/**
> + * Copy 48 bytes from one location to another,
> + * locations must not overlap.
> + */
> +static __rte_always_inline void
> +rte_mov48(uint8_t *__rte_restrict dst, const uint8_t *__rte_restrict src)
> +{
> +#if defined RTE_MEMCPY_AVX
> + rte_mov32((uint8_t *)dst, (const uint8_t *)src);
> + rte_mov16((uint8_t *)dst + 32, (const uint8_t *)src + 32);
> +#else /* SSE implementation */
> + rte_mov16((uint8_t *)dst + 0 * 16, (const uint8_t *)src + 0 * 16);
> + rte_mov16((uint8_t *)dst + 1 * 16, (const uint8_t *)src + 1 * 16);
> + rte_mov16((uint8_t *)dst + 2 * 16, (const uint8_t *)src + 2 * 16);
> +#endif
> +}
The casts in rte_mov48() are redundant since dst and src are already
uint8_t pointers.
[ ... ]
> @@ -232,45 +247,22 @@ rte_mov512blocks(uint8_t *dst, const uint8_t *src, size_t n)
> }
> }
>
> +/**
> + * Copy bytes from one location to another,
> + * locations must not overlap.
> + * Use with n > 64.
> + */
> static __rte_always_inline void *
> -rte_memcpy_generic(void *dst, const void *src, size_t n)
> +rte_memcpy_generic_more_than_64(void *__rte_restrict dst, const void *__rte_restrict src,
> + size_t n)
> {
> void *ret = dst;
> size_t dstofss;
> size_t bits;
>
> - /**
> - * Copy less than 16 bytes
> - */
> - if (n < 16) {
> - return rte_mov15_or_less(dst, src, n);
> - }
> -
> /**
> * Fast way when copy size doesn't exceed 512 bytes
> */
> - if (__rte_constant(n) && n == 32) {
> - rte_mov32((uint8_t *)dst, (const uint8_t *)src);
> - return ret;
> - }
> - if (n <= 32) {
> - rte_mov16((uint8_t *)dst, (const uint8_t *)src);
> - if (__rte_constant(n) && n == 16)
> - return ret; /* avoid (harmless) duplicate copy */
> - rte_mov16((uint8_t *)dst - 16 + n,
> - (const uint8_t *)src - 16 + n);
> - return ret;
> - }
> - if (__rte_constant(n) && n == 64) {
> - rte_mov64((uint8_t *)dst, (const uint8_t *)src);
> - return ret;
> - }
> - if (n <= 64) {
> - rte_mov32((uint8_t *)dst, (const uint8_t *)src);
> - rte_mov32((uint8_t *)dst - 32 + n,
> - (const uint8_t *)src - 32 + n);
> - return ret;
> - }
Does the removal of these size checks from rte_memcpy_generic_more_than_64()
ensure that callers never pass n <= 64, or should defensive programming
include an assertion or comment documenting this assumption?
[ ... ]
> @@ -696,44 +654,17 @@ rte_memcpy_generic(void *dst, const void *src, size_t n)
>
> #endif /* __AVX512F__ */
>
> +/**
> + * Copy bytes from one vector register size aligned location to another,
> + * locations must not overlap.
> + * Use with n > 64.
> + */
> static __rte_always_inline void *
> -rte_memcpy_aligned(void *dst, const void *src, size_t n)
> +rte_memcpy_aligned_more_than_64(void *__rte_restrict dst, const void *__rte_restrict src,
> + size_t n)
> {
> void *ret = dst;
>
> - /* Copy size < 16 bytes */
> - if (n < 16) {
> - return rte_mov15_or_less(dst, src, n);
> - }
> -
> - /* Copy 16 <= size <= 32 bytes */
> - if (__rte_constant(n) && n == 32) {
> - rte_mov32((uint8_t *)dst, (const uint8_t *)src);
> - return ret;
> - }
> - if (n <= 32) {
> - rte_mov16((uint8_t *)dst, (const uint8_t *)src);
> - if (__rte_constant(n) && n == 16)
> - return ret; /* avoid (harmless) duplicate copy */
> - rte_mov16((uint8_t *)dst - 16 + n,
> - (const uint8_t *)src - 16 + n);
> -
> - return ret;
> - }
> -
> - /* Copy 32 < size <= 64 bytes */
> - if (__rte_constant(n) && n == 64) {
> - rte_mov64((uint8_t *)dst, (const uint8_t *)src);
> - return ret;
> - }
> - if (n <= 64) {
> - rte_mov32((uint8_t *)dst, (const uint8_t *)src);
> - rte_mov32((uint8_t *)dst - 32 + n,
> - (const uint8_t *)src - 32 + n);
> -
> - return ret;
> - }
> -
> /* Copy 64 bytes blocks */
> for (; n > 64; n -= 64) {
> rte_mov64((uint8_t *)dst, (const uint8_t *)src);
[ ... ]
> @@ -749,12 +680,45 @@ rte_memcpy_aligned(void *dst, const void *src, size_t n)
> }
>
> static __rte_always_inline void *
> -rte_memcpy(void *dst, const void *src, size_t n)
> +rte_memcpy(void *__rte_restrict dst, const void *__rte_restrict src, size_t n)
> {
> + /* Fast way when copy size doesn't exceed 64 bytes. */
> + if (n < 16)
> + return rte_mov15_or_less(dst, src, n);
> + if (n <= 32) {
> + if (__rte_constant(n) && n == 32) {
> + rte_mov32((uint8_t *)dst, (const uint8_t *)src);
> + return dst;
> + }
> + rte_mov16((uint8_t *)dst, (const uint8_t *)src);
> + if (__rte_constant(n) && n == 16)
> + return dst; /* avoid (harmless) duplicate copy */
> + rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
> + return dst;
> + }
> + if (n <= 64) {
> + if (__rte_constant(n) && n == 64) {
> + rte_mov64((uint8_t *)dst, (const uint8_t *)src);
> + return dst;
> + }
> +#if defined RTE_MEMCPY_AVX
> + rte_mov32((uint8_t *)dst, (const uint8_t *)src);
> + rte_mov32((uint8_t *)dst - 32 + n, (const uint8_t *)src - 32 + n);
> +#else /* SSE implementation */
> + rte_mov16((uint8_t *)dst + 0 * 16, (const uint8_t *)src + 0 * 16);
> + rte_mov16((uint8_t *)dst + 1 * 16, (const uint8_t *)src + 1 * 16);
> + if (n > 48)
> + rte_mov16((uint8_t *)dst + 2 * 16, (const uint8_t *)src + 2 * 16);
> + rte_mov16((uint8_t *)dst - 16 + n, (const uint8_t *)src - 16 + n);
> +#endif
> + return dst;
> + }
> +
> + /* Implementation for size > 64 bytes depends on alignment with vector register size. */
> if (!(((uintptr_t)dst | (uintptr_t)src) & ALIGNMENT_MASK))
> - return rte_memcpy_aligned(dst, src, n);
> + return rte_memcpy_aligned_more_than_64(dst, src, n);
> else
> - return rte_memcpy_generic(dst, src, n);
> + return rte_memcpy_generic_more_than_64(dst, src, n);
Does the conditional in rte_memcpy() for n <= 64 bypass the alignment
check correctly for both aligned and unaligned pointers? Is there a
performance benefit to checking alignment for small copies?
> }
>
> #undef ALIGNMENT_MASK