theAlias
theAlias

Reputation: 396

Visual Studio 2010 takes forever to link when using too many string literals

I'm using visual studio 2010 to build a small project which declares a vector and does push_back for close to 1500 records, which has 6 string member variables. An instance of Employee is created by passing string literals hard coded in the cpp file.

sample code:

struct Employee{

Employee(string && name, string && id, string && ssn, string && location, string && phone): name(move(name)), id(move(id)), ssn(move(ssn)), location(move(location)), phone(move(phone)){}

string name;
string id;
string ssn;
string location;
string phone;
};

int main (int argc, char *args[]){
vector<Employee> ec;
ec.push_back(Employee(string("Sam"), string("sam"), string("215559999"), string("Seattle"), string("2145482058")));
ec.push_back(Employee(string("Adams"), string("ada"), string("124558888"), string("Pittsburgh"), string("6545482058")));
/*push_back 1500 records*/
}

This code compiles and links fine in debug mode. In release mode, the code compiles just fine, but takes forever to link. Last 2 lines from output console is
1>Link:
1> Generating code

PS: Disabling the optimization (properties > C/C++ > Optimization), from a release default of "Maximize Speed /O2", resolves the issue, but that's not something i would want to consider unless there's really no other way.

EDIT: File IO is not something we are comfortable with due to the slight delay it adds to process, especially because this code will be kicked off in 100s of jobs at once. So given this is a 30x4000 static table, we narrowed down on having a in-memory table, and avoiding loading delays due to io. Hard-coded definitely makes the code look more like a text file, but we are looking for performance. So, is there any way for Visual studio to accept hard coding of string literals, and linking them fast. If not, i'll go back to my file io approach.

Upvotes: 3

Views: 1373

Answers (1)

MSalters
MSalters

Reputation: 180303

Considering your literals are all literals, I'd stick with just const char* members. All those constructors being called at startup, all calling strlen can't be efficient. This also eliminates 12.000 temporaries, which is going to be a relief for the compiler.

Also, call reserve on that vector.

[edit] A common technique to avoid calling strlen at startup is to pass the strings as const char (&str)[N] and use Template Argument Deduction to get the string length N. In this particular case, the compiler is already suffering (28 minutes) so adding templates may very well make it worse. You'd have to profile it with the actual compiler. In particular, you may want a wrapper to prevent Employee::Employee from being instantiated for every possible combination of string lengths.

template<size_t N> inline std::string make_string(const char (&str)[N])
{
   return std::string(static_cas<const char*>(str), N);
} 

Upvotes: 3

Related Questions