VC 对 memcpy 的优化
在很多编译器中,memcpy 是一个 intrinsic 函数,也就是说,这个函数是由编译器实现的。它比 inline 函数更容易被编译时优化。编译器可以根据 memcpy 的参数是常量还是变量做出多种版本,达到最佳的性能。这一点,用 inline 或者 template 的技巧都无法办到。
我们看看 VC 对 memcpy 的优化。(所用版本 VC6)
void foo(void *d,const void *s)
{
memcpy(d,s,1);
}
选性能最优化,生成汇编代码可以看到。代码被优化成:
mov eax, DWORD PTR _s$[esp-4]
mov edx, DWORD PTR _d$[esp-4]
mov cl, BYTE PTR [eax]
mov BYTE PTR [edx], cl
只是一个字节拷贝,用 cl 寄存器 mov 完成的。
把 1 改成 4 后:
mov eax, DWORD PTR _s$[esp-4]
mov edx, DWORD PTR _d$[esp-4]
mov ecx, DWORD PTR [eax]
mov DWORD PTR [edx], ecx
就变成了一条最普通的 mov 指令。
如果是 8 个字节:
mov eax, DWORD PTR _s$[esp-4]
mov ecx, DWORD PTR _d$[esp-4]
mov edx, DWORD PTR [eax]
mov DWORD PTR [ecx], edx
mov eax, DWORD PTR [eax+4]
mov DWORD PTR [ecx+4], eax
就是两条 mov 指令。
直到长度是常量 19 还是用 mov 完成的:
mov eax, DWORD PTR _s$[esp-4]
mov ecx, DWORD PTR _d$[esp-4]
mov edx, DWORD PTR [eax]
mov DWORD PTR [ecx], edx
mov edx, DWORD PTR [eax+4]
mov DWORD PTR [ecx+4], edx
mov edx, DWORD PTR [eax+8]
mov DWORD PTR [ecx+8], edx
mov edx, DWORD PTR [eax+12]
mov DWORD PTR [ecx+12], edx
mov dx, WORD PTR [eax+16]
mov WORD PTR [ecx+16], dx
mov al, BYTE PTR [eax+18]
mov BYTE PTR [ecx+18], al
长度达到 20 后,就转变成了使用 rep movsd
push esi
mov esi, DWORD PTR _s$[esp]
push edi
mov edi, DWORD PTR _d$[esp+4]
mov ecx, 5<strong>
rep movsd</strong>
pop edi
pop esi
如果长度并非 4 的整数倍的话,比如复制 23 个字节:
push esi
mov esi, DWORD PTR _s$[esp]
push edi
mov edi, DWORD PTR _d$[esp+4]
mov ecx, 5
rep movsd<strong>
movsw
movsb</strong>
pop edi
pop esi
编译器会在后面插入 movsw 和 movsb 。
现在我们来看看,memcpy 的长度是变量的情况:
void foo(void *d,const void *s,size_t size)
{
memcpy(d,s,size);
}
这次编译器直接调用了 rep movsd
mov ecx, DWORD PTR _size$[esp-4]
push esi
mov esi, DWORD PTR _s$[esp]
mov eax, ecx
push edi
mov edi, DWORD PTR _d$[esp+4]
shr ecx, 2
rep movsd
mov ecx, eax
and ecx, 3
rep movsb
pop edi
pop esi
因为我们并不知道 size 是否是 4 的整数倍,所以尾巴上用 and ecx,3 / repmovsb 来处理了一下。
那么我们能否通知编译器,需要 memcpy 的数据块长度是 4 的倍数呢?答案是可以的。看看编译器怎么编译 memcpy(d,s,size*4);
mov ecx, DWORD PTR _size$[esp-4]
push esi
mov esi, DWORD PTR _s$[esp]
push edi
mov edi, DWORD PTR _d$[esp+4]
rep movsd
pop edi
pop esi
非常简洁,不是吗?
Comments
Posted by: 发哈卡 | (45) December 11, 2011 06:22 PM
Posted by: 发哈卡 | (44) December 11, 2011 06:20 PM
Posted by: 免费小说网 | (43) June 24, 2011 12:16 AM
Posted by: JackH | (42) September 17, 2009 10:43 PM
Posted by: yongzhi.wu | (41) September 20, 2007 02:13 PM
Posted by: tigerdx8 | (40) September 6, 2007 05:23 PM
Posted by: xxx | (39) August 1, 2007 12:22 AM
Posted by: 大刘 | (38) December 11, 2006 06:21 PM
Posted by: woada | (37) December 6, 2006 04:42 PM
Posted by: neverSeen | (36) October 26, 2006 10:11 AM
Posted by: flying | (35) October 24, 2006 05:17 PM
Posted by: Anonymous | (34) April 8, 2006 10:25 AM
Posted by: Cloud | (33) April 4, 2006 12:36 PM
Posted by: Anonymous | (32) April 3, 2006 06:18 PM
Posted by: Bennie | (31) April 3, 2006 03:54 PM
Posted by: Cloud | (30) April 3, 2006 03:31 PM
Posted by: Bennie | (29) April 3, 2006 02:17 PM
Posted by: coder | (28) April 3, 2006 01:49 PM
Posted by: Cloud | (27) April 3, 2006 01:24 PM
Posted by: coder | (26) April 3, 2006 09:47 AM
Posted by: Bennie | (25) April 3, 2006 08:36 AM
Posted by: Cloud | (24) April 3, 2006 12:24 AM
Posted by: Anonymous | (23) April 2, 2006 10:49 PM
Posted by: Bennie | (22) April 2, 2006 09:48 PM
Posted by: Bennie | (21) April 2, 2006 09:38 PM
Posted by: Cloud | (20) April 2, 2006 05:26 PM
Posted by: Bennie | (19) April 1, 2006 10:34 PM
Posted by: Atry | (18) April 1, 2006 05:56 PM
Posted by: euclid | (17) April 1, 2006 09:46 AM
Posted by: Anonymous | (16) April 1, 2006 09:19 AM
Posted by: Cloud | (15) April 1, 2006 02:31 AM
Posted by: Cloud | (14) April 1, 2006 02:04 AM
Posted by: Cloud | (13) April 1, 2006 01:42 AM
Posted by: Anonymous | (12) March 31, 2006 04:59 PM
Posted by: Cloud | (11) March 31, 2006 04:03 PM
Posted by: Cloud | (10) March 31, 2006 01:24 PM
Posted by: Anonymous | (9) March 31, 2006 09:19 AM
Posted by: Cloud | (8) March 31, 2006 12:02 AM
Posted by: Anonymous | (7) March 30, 2006 11:38 PM
Posted by: Cloud | (6) March 27, 2006 03:15 PM
Posted by: Anonymous | (5) March 27, 2006 02:39 PM
Posted by: Cloud | (4) November 29, 2005 11:37 PM
Posted by: ihha | (3) November 29, 2005 12:10 PM
Posted by: study | (2) November 7, 2005 11:23 AM
Posted by: myan | (1) November 1, 2005 11:07 AM