小编Sha*_*ank的帖子

执行存储在数据段中的x86指令会导致性能下降?

我有一个简单的程序,首先将一些本机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 …
Run Code Online (Sandbox Code Playgroud)

performance x86 caching alignment execution

5
推荐指数
1
解决办法
496
查看次数

标签 统计

alignment ×1

caching ×1

execution ×1

performance ×1

x86 ×1