C的奇特字符串复制功能

|| 在阅读nginx代码时,我看到了以下功能:
#define ngx_cpymem(dst, src, n)   (((u_char *) memcpy(dst, src, n)) + (n))

static ngx_inline u_char *
ngx_copy(u_char *dst, u_char *src, size_t len)
{
    if (len < 17) {

        while (len) {
            *dst++ = *src++;
            len--;
        }

        return dst;

    } else {
        return ngx_cpymem(dst, src, len);
    }
}
这是一个简单的字符串复制功能。但是,为什么要测试字符串的长度并在长度> = 17时切换到memcpy呢?     
已邀请:
这是一种优化-对于很小的字符串,简单复制比调用系统(libc)复制函数要快。 带有“ 1”循环的简单复制对短字符串的运行相当快,而系统复制功能(通常)对长字符串具有优化。但是系统复制也进行了大量检查和设置。 实际上,在此代码之前有作者评论:nginx,/ src / core / ngx_string.h(搜索ngx_copy)
/*
 * the simple inline cycle copies the variable length strings up to 16
 * bytes faster than icc8 autodetecting _intel_fast_memcpy()
 */
另外,两行的上方是
#if ( __INTEL_COMPILER >= 800 )
因此,作者进行了测量,得出的结论是,ICC优化的内存复制经过长时间的CPU检查以选择最优化的内存复制变体。他发现手动复制16个字节比从ICC最快的memcpy代码快。 对于其他编译器,nginx确实直接使用
ngx_cpymem
(memcpy)
#define ngx_copy                  ngx_cpymem
作者对不同大小的of6进行了研究:
/*
 * gcc3, msvc, and icc7 compile memcpy() to the inline \"rep movs\".
 * gcc3 compiles memcpy(d, s, 4) to the inline \"mov\"es.
 * icc8 compile memcpy(d, s, 4) to the inline \"mov\"es or XMM moves.
 */
    

要回复问题请先登录注册