Hi. I have a simple program that first writes some native x86 instructions into a declared buffer, and then sets a function pointer to this buffer and makes a call. I'm noticing a severe performance penalty, however, when that buffer is allocated on the stack (as opposed to on the heap, or even in the globals data area). I verified that the start of the instruction sequence in the data buffer is on a 16-byte boundary (I'm assuming that's what the cpu requires (or wants) it to be). I don't know why it would make a difference where I execute my instructions in the process from, but in the program below, "GOOD" executes in 4 seconds on my dual-core workstation, and "BAD" takes 6 minutes or so. Is there some kind of alignment/i-cache/prediction issue going on here?? My evaluation license to VTune just ended, so I can't even run an analysis on this :(. Thanks.
#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);
}
The results from running this are:
$ cl -DGOOD ne3.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 11.00.7022 for 80x86
Copyright (C) Microsoft Corp 1984-1997. All rights reserved.
ne3.cpp
Microsoft (R) 32-Bit Incremental Linker Version 5.10.7303
Copyright (C) Microsoft Corp 1992-1997. All rights reserved.
/out:ne3.exe
ne3.obj
$ time ./ne3
Addr of heap buf: 410eb0
real 0m 4.33s
user 0m 4.31s
sys 0m 0.01s
$
$
$ cl ne3.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 11.00.7022 for 80x86
Copyright (C) Microsoft Corp 1984-1997. All rights reserved.
ne3.cpp
Microsoft (R) 32-Bit Incremental Linker Version 5.10.7303
Copyright (C) Microsoft Corp 1992-1997. All rights reserved.
/out:ne3.exe
ne3.obj
$ time ./ne3
Addr of local buf: 12feb0
real 6m41.19s
user 6m40.46s
sys 0m 0.03s
$
Thanks.
- Shasank