Performance: Removed unnecessary std::string::substr in high-traffic code

This commit is contained in:
Paul Beckingham 2015-11-01 16:10:49 -05:00
parent 25b7f42810
commit b5c4bf0a6d
4 changed files with 37 additions and 39 deletions

View file

@ -140,8 +140,8 @@ Color::Color (const std::string& spec)
}
// greyN/grayN, where 0 <= N <= 23.
else if (word.substr (0, 4) == "grey" ||
word.substr (0, 4) == "gray")
else if (! word.compare (0, 4, "grey", 4) ||
! word.compare (0, 4, "gray", 4))
{
index = atoi (word.substr (4).c_str ());
if (index < 0 || index > 23)
@ -162,7 +162,7 @@ Color::Color (const std::string& spec)
}
// rgbRGB, where 0 <= R,G,B <= 5.
else if (word.substr (0, 3) == "rgb")
else if (! word.compare (0, 3, "rgb", 3))
{
index = atoi (word.substr (3).c_str ());
if (word.length () != 6 ||
@ -194,7 +194,7 @@ Color::Color (const std::string& spec)
}
// colorN, where 0 <= N <= 255.
else if (word.substr (0, 5) == "color")
else if (! word.compare (0, 5, "color", 5))
{
index = atoi (word.substr (5).c_str ());
if (index < 0 || index > 255)

View file

@ -73,7 +73,7 @@ bool DOM::get (const std::string& name, Variant& value)
// rc. --> context.config
if (len > 3 &&
name.substr (0, 3) == "rc.")
! name.compare (0, 3, "rc.", 3))
{
std::string key = name.substr (3);
auto c = context.config.find (key);
@ -88,7 +88,7 @@ bool DOM::get (const std::string& name, Variant& value)
// context.*
if (len > 8 &&
name.substr (0, 8) == "context.")
! name.compare (0, 8, "context.", 8))
{
if (name == "context.program")
{
@ -131,7 +131,7 @@ bool DOM::get (const std::string& name, Variant& value)
// system. --> Implement locally.
if (len > 7 &&
name.substr (0, 7) == "system.")
! name.compare (0, 7, "system.", 7))
{
// Taskwarrior version number.
if (name == "system.version")

View file

@ -534,7 +534,7 @@ bool Task::is_dueyear () const
bool Task::is_udaPresent () const
{
for (auto& col : context.columns)
if (col.first.substr (0, 11) != "annotation_")
if (col.first.compare (0, 11, "annotation_", 11) != 0)
if (col.second->is_uda () &&
has (col.first))
return true;
@ -546,7 +546,7 @@ bool Task::is_udaPresent () const
bool Task::is_orphanPresent () const
{
for (auto& att : *this)
if (att.first.substr (0, 11) != "annotation_")
if (att.first.compare (0, 11, "annotation_", 11) != 0)
if (context.columns.find (att.first) == context.columns.end ())
return true;
@ -624,7 +624,7 @@ void Task::parse (const std::string& input)
{
legacyAttributeMap (name);
if (name.substr (0, 11) == "annotation_")
if (! name.compare (0, 11, "annotation_", 11))
++annotation_count;
(*this)[name] = decode (json::decode (value));
@ -861,7 +861,7 @@ std::string Task::composeJSON (bool decorate /*= false*/) const
for (auto& i : *this)
{
// Annotations are not written out here.
if (i.first.substr (0, 11) == "annotation_")
if (! i.first.compare (0, 11, "annotation_", 11))
continue;
// If value is an empty string, do not ever output it
@ -968,7 +968,7 @@ std::string Task::composeJSON (bool decorate /*= false*/) const
int annotations_written = 0;
for (auto& i : *this)
{
if (i.first.substr (0, 11) == "annotation_")
if (! i.first.compare (0, 11, "annotation_", 11))
{
if (annotations_written)
out << ",";
@ -1035,7 +1035,7 @@ void Task::removeAnnotations ()
auto i = this->begin ();
while (i != this->end ())
{
if (i->first.substr (0, 11) == "annotation_")
if (! i->first.compare (0, 11, "annotation_", 11))
{
--annotation_count;
this->erase (i++);
@ -1053,7 +1053,7 @@ void Task::getAnnotations (std::map <std::string, std::string>& annotations) con
annotations.clear ();
for (auto& ann : *this)
if (ann.first.substr (0, 11) == "annotation_")
if (! ann.first.compare (0, 11, "annotation_", 11))
annotations.insert (ann);
}
@ -1296,7 +1296,7 @@ void Task::removeTag (const std::string& tag)
void Task::getUDAOrphans (std::vector <std::string>& names) const
{
for (auto& it : *this)
if (it.first.substr (0, 11) != "annotation_")
if (it.first.compare (0, 11, "annotation_", 11) != 0)
if (context.columns.find (it.first) == context.columns.end ())
names.push_back (it.first);
}
@ -1512,7 +1512,7 @@ void Task::validate (bool applyDefault /* = true */)
std::vector <std::string> udas;
for (auto& var : context.config)
{
if (var.first.substr (0, 4) == "uda." &&
if (! var.first.compare (0, 4, "uda.", 4) &&
var.first.find (".default") != std::string::npos)
{
auto period = var.first.find ('.', 4);
@ -1737,7 +1737,7 @@ float Task::urgency_c () const
{
if (fabs (var.second) > epsilon)
{
if (var.first.substr (0, 13) == "urgency.user.")
if (! var.first.compare (0, 13, "urgency.user.", 13))
{
// urgency.user.project.<project>.coefficient
auto end = std::string::npos;
@ -2281,9 +2281,7 @@ void Task::modify (modType type, bool text_required /* = false */)
}
}
else if (modCount == 0 && text_required)
{
throw std::string (STRING_CMD_MODIFY_NEED_TEXT);
}
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -55,7 +55,7 @@ void initializeColorRules ()
std::vector <std::string> rules;
for (auto& v : context.config)
{
if (v.first.substr (0, 6) == "color.")
if (! v.first.compare (0, 6, "color.", 6))
{
Color c (v.second);
gsColor[v.first] = c;
@ -194,7 +194,7 @@ static void colorizeKeyword (Task& task, const std::string& rule, const Color& b
{
for (auto& it : task)
{
if (it.first.substr (0, 11) == "annotation_" &&
if (! it.first.compare (0, 11, "annotation_", 11) &&
find (it.second, rule.substr (14), sensitive) != std::string::npos)
{
applyColor (base, c, merge);
@ -307,26 +307,26 @@ void autoColorize (Task& task, Color& c)
Color base = gsColor[*r];
if (base.nontrivial ())
{
if (*r == "color.blocked") colorizeBlocked (task, base, c, merge);
else if (*r == "color.blocking") colorizeBlocking (task, base, c, merge);
else if (*r == "color.tagged") colorizeTagged (task, base, c, merge);
else if (*r == "color.active") colorizeActive (task, base, c, merge);
else if (*r == "color.scheduled") colorizeScheduled (task, base, c, merge);
else if (*r == "color.until") colorizeUntil (task, base, c, merge);
else if (*r == "color.project.none") colorizeProjectNone (task, base, c, merge);
else if (*r == "color.tag.none") colorizeTagNone (task, base, c, merge);
else if (*r == "color.due") colorizeDue (task, base, c, merge);
else if (*r == "color.due.today") colorizeDueToday (task, base, c, merge);
else if (*r == "color.overdue") colorizeOverdue (task, base, c, merge);
else if (*r == "color.recurring") colorizeRecurring (task, base, c, merge);
else if (*r == "color.completed") colorizeCompleted (task, base, c, merge);
else if (*r == "color.deleted") colorizeDeleted (task, base, c, merge);
if (*r == "color.blocked") colorizeBlocked (task, base, c, merge);
else if (*r == "color.blocking") colorizeBlocking (task, base, c, merge);
else if (*r == "color.tagged") colorizeTagged (task, base, c, merge);
else if (*r == "color.active") colorizeActive (task, base, c, merge);
else if (*r == "color.scheduled") colorizeScheduled (task, base, c, merge);
else if (*r == "color.until") colorizeUntil (task, base, c, merge);
else if (*r == "color.project.none") colorizeProjectNone (task, base, c, merge);
else if (*r == "color.tag.none") colorizeTagNone (task, base, c, merge);
else if (*r == "color.due") colorizeDue (task, base, c, merge);
else if (*r == "color.due.today") colorizeDueToday (task, base, c, merge);
else if (*r == "color.overdue") colorizeOverdue (task, base, c, merge);
else if (*r == "color.recurring") colorizeRecurring (task, base, c, merge);
else if (*r == "color.completed") colorizeCompleted (task, base, c, merge);
else if (*r == "color.deleted") colorizeDeleted (task, base, c, merge);
// Wildcards
else if (r->substr (0, 10) == "color.tag.") colorizeTag (task, *r, base, c, merge);
else if (r->substr (0, 14) == "color.project.") colorizeProject (task, *r, base, c, merge);
else if (r->substr (0, 14) == "color.keyword.") colorizeKeyword (task, *r, base, c, merge);
else if (r->substr (0, 10) == "color.uda.") colorizeUDA (task, *r, base, c, merge);
else if (! r->compare (0, 10, "color.tag.", 10)) colorizeTag (task, *r, base, c, merge);
else if (! r->compare (0, 14, "color.project.", 14)) colorizeProject (task, *r, base, c, merge);
else if (! r->compare (0, 14, "color.keyword.", 14)) colorizeKeyword (task, *r, base, c, merge);
else if (! r->compare (0, 10, "color.uda.", 10)) colorizeUDA (task, *r, base, c, merge);
}
}