【发布时间】:2010-12-08 22:00:24
【问题描述】:
我有一个简单的程序,它首先将一些本机 x86 指令写入声明的缓冲区,然后设置一个指向该缓冲区的函数指针并进行调用。但是,当在堆栈上分配缓冲区时(而不是在堆上,甚至在全局数据区域中),我注意到严重的性能损失。我验证了数据缓冲区中指令序列的开始位于 16 字节边界上(我假设这是 cpu 需要(或希望)它是什么)。我不知道为什么在我的过程中执行指令会有所不同,但在下面的程序中,“GOOD”在我的双核工作站上执行 4 秒,“BAD”需要 6 分钟左右.这里是否存在某种对齐/i-cache/预测问题?我对 VTune 的评估许可刚刚结束,所以我什至无法对此进行分析:(。谢谢。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int (*funcPtrType)(int, int);
int foo(int a, int b) { return a + b; }
void main()
{
// Instructions in buf are identical to what the compiler generated for "foo".
char buf[201] = {0x55,
0x8b, 0xec,
0x8b, 0x45, 0x08,
0x03, 0x45, 0x0c,
0x5D,
0xc3
};
int i;
funcPtrType ptr;
#ifdef GOOD
char* heapBuf = (char*)malloc(200);
printf("Addr of heap buf: %x\n", &heapBuf[0]);
memcpy(heapBuf, buf, 200);
ptr = (funcPtrType)(&heapBuf[0]);
#else // BAD
printf("Addr of local buf: %x\n", &buf[0]);
ptr = (funcPtrType)(&buf[0]);
#endif
for (i=0; i < 1000000000; i++)
ptr(1,2);
}
运行结果如下:
$ cl -DGOOD ne3.cpp
Microsoft (R) 32 位 C/C++ 优化编译器版本 11.00.7022 用于 80x86
版权所有 (C) Microsoft Corp 1984-1997。版权所有。
ne3.cpp
Microsoft (R) 32 位增量链接器版本 5.10.7303
版权所有 (C) Microsoft Corp 1992-1997。版权所有。
/out:ne3.exe
ne3.obj
$ 时间 ./ne3
堆缓冲区地址:410eb0
真正的 0m 4.33s
用户 0m 4.31s
系统 0m 0.01s
$
$
$ cl ne3.cpp
Microsoft (R) 32 位 C/C++ 优化编译器版本 11.00.7022 用于 80x86
版权所有 (C) Microsoft Corp 1984-1997。版权所有。
ne3.cpp
Microsoft (R) 32 位增量链接器版本 5.10.7303
版权所有 (C) Microsoft Corp 1992-1997。版权所有。
/out:ne3.exe
ne3.obj
$ 时间 ./ne3
本地 buf 地址:12feb0
真正的 6m41.19s
用户 6m40.46s
系统 0m 0.03s
$
谢谢。
- 沙桑克
【问题讨论】:
-
顺便说一句,还要检查生成的代码。虽然看起来不太可能,但谁真正知道编译器是否只在一个路径 中进行了优化。 . . 和通过几个循环的单步操作。 “这是唯一确定的方法。”
标签: performance caching x86 alignment execution