Skip to content
Permalink
Browse files

Speedup critical string handling in RideCache Save

  • Loading branch information
ericchristoffersen authored and amtriathlon committed Dec 1, 2019
1 parent c13b242 commit fa9a59d60ced1cd0baf0f1650ed3b026f3e5d13d
Showing with 50 additions and 10 deletions.
  1. +50 −10 src/Core/RideDB.y
@@ -426,6 +426,48 @@ static bool setup_mmp_durations()
}
static bool did_mmp_durations = setup_mmp_durations();

// Fast constructon of common string. In the future we'll achieve this with a single variadic template function.
QString ConstructNameNumberString(QString s0, QString name, QString s1, double num, QString s2)
{
QString numStr;
numStr.setNum(num, 'f', 5);

QString m;
m.reserve(s0.length() + name.length() + s1.length() + numStr.length() + s2.length());

m.append(s0);
m.append(name);
m.append(s1);
m.append(numStr);
m.append(s2);

return m;
}

// Fast constructon of common string. In the future we'll achieve this with a single variadic template function.
QString ConstructNameNumberNumberString(QString s0, QString name, QString s1, double num0, QString s2, double num1, QString s3)
{
QString num0Str;
num0Str.setNum(num0, 'f', 5);

QString num1Str;
num1Str.setNum(num1, 'f', 5);

QString m;
m.reserve(s0.length() + name.length() + s1.length() + num0Str.length() + s2.length() + num1Str.length() + s3.length());

m.append(s0);
m.append(name);
m.append(s1);
m.append(num0Str);
m.append(s2);
m.append(num1Str);
m.append(s3);

return m;
}


// save cache to disk
//
// if opendata is true then save in format for sending to the GC OpenData project
@@ -548,12 +590,11 @@ void RideCache::save(bool opendata, QString filename)
<< QString("%1").arg(item->stdvariances().value(index, 0.0f), 0, 'f', 5) <<"\"]";
} else if (item->counts()[index] == 0) {
// if count is 0 don't write it
stream << "\t\t\t\"" << name << "\":\"" << QString("%1").arg(item->metrics()[index], 0, 'f', 5) <<"\"";
stream << ConstructNameNumberString(QString("\t\t\t\""), name,
QString("\":\""), item->metrics()[index], QString("\""));
} else {

// count is not 1, so lets write it
stream << "\t\t\t\"" << name << "\":[\"" << QString("%1").arg(item->metrics()[index], 0, 'f', 5) <<"\",\""
<< QString("%1").arg(item->counts()[index], 0, 'f', 5) <<"\"]";
stream << ConstructNameNumberNumberString(QString("\t\t\t\""), name,
QString("\":[\""), item->metrics()[index], QString("\",\""), item->counts()[index], QString("\"]"));
}
}
}
@@ -782,12 +823,11 @@ void RideCache::save(bool opendata, QString filename)

// if count is 0 don't write it
} else if (interval->counts()[index] == 0) {
stream << "\t\t\t\t\"" << name << "\":\"" << QString("%1").arg(interval->metrics()[index], 0, 'f', 5) <<"\"";
stream << ConstructNameNumberString(QString("\t\t\t\""), name,
QString("\":\""), item->metrics()[index], QString("\""));
} else {

// count is not 1, so lets write it
stream << "\t\t\t\t\"" << name << "\":[\"" << QString("%1").arg(interval->metrics()[index], 0, 'f', 5) <<"\",\""
<< QString("%1").arg(interval->counts()[index], 0, 'f', 5) <<"\"]";
stream << ConstructNameNumberNumberString(QString("\t\t\t\""), name,
QString("\":[\""), item->metrics()[index], QString("\",\""), item->counts()[index], QString("\"]"));
}
}
}

0 comments on commit fa9a59d

Please sign in to comment.
You can’t perform that action at this time.