summaryrefslogtreecommitdiffstats
path: root/arch/x86/lib/memcpy_32.c
diff options
context:
space:
mode:
authorMa Ling <ling.ma@intel.com>2010-06-29 03:24:25 +0800
committerH. Peter Anvin <hpa@linux.intel.com>2010-08-23 14:56:41 -0700
commit59daa706fbec745684702741b9f5373142dd9fdc (patch)
tree53c20211ef0bf1be250a31a98ae0d966dce49308 /arch/x86/lib/memcpy_32.c
parentfdf4289679fd41d76553ce224750e9737cd80eea (diff)
downloadlinux-59daa706fbec745684702741b9f5373142dd9fdc.tar.gz
linux-59daa706fbec745684702741b9f5373142dd9fdc.tar.bz2
linux-59daa706fbec745684702741b9f5373142dd9fdc.zip
x86, mem: Optimize memcpy by avoiding memory false dependece
All read operations after allocation stage can run speculatively, all write operation will run in program order, and if addresses are different read may run before older write operation, otherwise wait until write commit. However CPU don't check each address bit, so read could fail to recognize different address even they are in different page.For example if rsi is 0xf004, rdi is 0xe008, in following operation there will generate big performance latency. 1. movq (%rsi), %rax 2. movq %rax, (%rdi) 3. movq 8(%rsi), %rax 4. movq %rax, 8(%rdi) If %rsi and rdi were in really the same meory page, there are TRUE read-after-write dependence because instruction 2 write 0x008 and instruction 3 read 0x00c, the two address are overlap partially. Actually there are in different page and no any issues, but without checking each address bit CPU could think they are in the same page, and instruction 3 have to wait for instruction 2 to write data into cache from write buffer, then load data from cache, the cost time read spent is equal to mfence instruction. We may avoid it by tuning operation sequence as follow. 1. movq 8(%rsi), %rax 2. movq %rax, 8(%rdi) 3. movq (%rsi), %rax 4. movq %rax, (%rdi) Instruction 3 read 0x004, instruction 2 write address 0x010, no any dependence. At last on Core2 we gain 1.83x speedup compared with original instruction sequence. In this patch we first handle small size(less 20bytes), then jump to different copy mode. Based on our micro-benchmark small bytes from 1 to 127 bytes, we got up to 2X improvement, and up to 1.5X improvement for 1024 bytes on Corei7. (We use our micro-benchmark, and will do further test according to your requirment) Signed-off-by: Ma Ling <ling.ma@intel.com> LKML-Reference: <1277753065-18610-1-git-send-email-ling.ma@intel.com> Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Diffstat (limited to 'arch/x86/lib/memcpy_32.c')
-rw-r--r--arch/x86/lib/memcpy_32.c6
1 files changed, 2 insertions, 4 deletions
diff --git a/arch/x86/lib/memcpy_32.c b/arch/x86/lib/memcpy_32.c
index be424dfcf365..81130d477ee2 100644
--- a/arch/x86/lib/memcpy_32.c
+++ b/arch/x86/lib/memcpy_32.c
@@ -36,11 +36,9 @@ void *memmove(void *dest, const void *src, size_t n)
"1" (src),
"2" (dest)
:"memory");
-
} else {
-
- if((src + count) < dest)
- return memcpy(dest, src, count);
+ if((src + n) < dest)
+ return memcpy(dest, src, n);
else
__asm__ __volatile__(
"std\n\t"