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). Previously 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.