I'm doing some code review and I saw something like the following:
std::ifstream dataFile(filename);
if (dataFile.is_open())
{
std::deque<std::string> lines;
std::string line, line_batch;
while (std::getline(dataFile, line))
{
if (!line.empty())
lines.push_back(line);
}
dataFile.close();
for (size_t i = 0; i < lines.size();)
{
if (i < MAX_BATCH_SIZE)
{
line_batch += "{" + lines.at(i) + "},";
i++;
}
I'm thinking that having many small strings (1000 lines equating to around 3000 strings) that will be concatenated together, that it would be beneficial to calculate the final string size ahead of time, reserve it and then do a concatenation using string::append
as this would reduce fragmentation, reallocation and increase speed (the last one I don't think would be that big of a deal).
A read in line would, at a guess, be between 10-1000 characters. Max size of line_batch
should be under 20MB. The application can get pretty big during execution. Big enough to force us to move from a 32 to a 64 bit memory scheme. The amount of times that this will happen depends on the user's environment, but if I were to guess, maybe between 0 and 100 times in a session. Could be more.
I'm a little on the fence on this. Am I being too nit picky? Is the added complexity worth it? If it matters, we're compiling with VS2019, though I don't think we're using it as a fully compliant c++17 compiler.