Why does this static array cause an extremely long "generating code" step in release build?
I recently added a static lookup array to one of my classes, like:
// MyClass.h
class MyClass
{
static MyClass const s_fromIndex[1500];
public:
MyClass(size_t arg1, size_t arg2);
static MyClass fromIndex(size_t index) { return s_fromIndex[index]; }
}
// MyClass.cpp
MyClass const MyClass::s_fromIndex[1500] =
{
MyClass(0, 1), MyClass(0, 2), MyClass(0, 3), MyClass(0, 4),
...
};
The reason for the array is that the calculation from index to the two parameters is quite expensive (despite the looks of it in the sample code). Since index is limited to 1500, I figured I'd return objects from a lookup array instead.
The class resides in a static library project. Soon, I noticed that whenever I used MyClass in my executable project, the "generating code" step during building would suddenly take extremely long (2-3 minutes). Prev开发者_C百科iously this step would complete pretty much instantly. Debug build still completes very quickly.
When I take out the static array everything builds smoothly again.
Strangely enough, I cannot seem to reproduce the problem in a new clean project with just the above code, so there might be some other things going on.
Do you have any idea why the static array causes an extremely long 'generating code' step in release build? What is the compiler/linker doing in this step anyway?
Edit: I've currently worked around the issue by using a static vector and filling it at runtime. I'm still curious as to what might cause the extremely long generating code step. Furthermore, I'd like to add that in reality MyClass is larger than the sample code I've posted. Specifically, it has one (and only one) private member. The type of this member is a templated class. Maybe that has something to do with it.
Do you have different optimizations turned on in the different builds?
I noticed this once too with a quick & dirty app with a really large static array initialization & release builds -- the debug ver built quickly, release would take FOREVER. I eventually gave up and loaded the data at runtime vs. compile time, and never identified the specific optimization setting that was causing the issue.
精彩评论