From 7b89bc92e1039b62b38428dcfd057beb7e8e3ddd Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 26 May 2013 11:40:18 -0400 Subject: [PATCH] Performance - It it not necessary to make a copy of all configuration variable names, if the Config::const_iterator is sufficient. (cherry picked from commit 5708cb90780f1f6c8f58f5b549796c4af612b1ab) --- src/Hooks.cpp | 14 +++----- src/Task.cpp | 15 ++++---- src/columns/Column.cpp | 12 +++---- src/commands/CmdCalendar.cpp | 27 ++++++-------- src/commands/CmdColor.cpp | 19 +++++----- src/commands/CmdReports.cpp | 11 +++--- src/commands/CmdShow.cpp | 70 ++++++++++++++++++------------------ src/commands/CmdUDAs.cpp | 30 +++++++--------- src/commands/Command.cpp | 11 +++--- src/rules.cpp | 14 ++++---- 10 files changed, 95 insertions(+), 128 deletions(-) diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 9358e2307..807f107ce 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -105,25 +105,21 @@ void Hooks::initialize () bool big_red_switch = context.config.getBoolean ("extensions"); if (big_red_switch) { - std::vector vars; - context.config.all (vars); - - std::vector ::iterator it; - for (it = vars.begin (); it != vars.end (); ++it) + Config::const_iterator it; + for (it = context.config.begin (); it != context.config.end (); ++it) { std::string type; std::string name; std::string value; // "." - Nibbler n (*it); + Nibbler n (it->first); if (n.getUntil ('.', type) && type == "hook" && n.skip ('.') && n.getUntilEOS (name)) { - std::string value = context.config.get (*it); - Nibbler n (value); + Nibbler n (it->second); // : [, ...] while (!n.depleted ()) @@ -141,7 +137,7 @@ void Hooks::initialize () (void) n.skip (','); } else - ; // Was: throw std::string (format ("Malformed hook definition '{1}'.", *it)); + ; // Was: throw std::string (format ("Malformed hook definition '{1}'.", it->first)); } } } diff --git a/src/Task.cpp b/src/Task.cpp index 03b496092..7f9b5a7cb 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -1316,19 +1316,16 @@ void Task::validate (bool applyDefault /* = true */) if (applyDefault) { // Gather a list of all UDAs with a .default value - std::vector names; - context.config.all (names); - std::vector udas; - std::vector ::iterator name; - for (name = names.begin (); name != names.end (); ++name) + Config::const_iterator var; + for (var = context.config.begin (); var != context.config.end (); ++var) { - if (name->substr (0, 4) == "uda." && - name->find (".default") != std::string::npos) + if (var->first.substr (0, 4) == "uda." && + var->first.find (".default") != std::string::npos) { - std::string::size_type period = name->find ('.', 4); + std::string::size_type period = var->first.find ('.', 4); if (period != std::string::npos) - udas.push_back (name->substr (4, period - 4)); + udas.push_back (var->first.substr (4, period - 4)); } } diff --git a/src/columns/Column.cpp b/src/columns/Column.cpp index d666c0fb2..81bf92764 100644 --- a/src/columns/Column.cpp +++ b/src/columns/Column.cpp @@ -151,17 +151,15 @@ void Column::uda (std::map & all) { // For each UDA, instantiate and initialize ColumnUDA(). std::map udas; - std::vector names; - context.config.all (names); - std::vector ::iterator i; - for (i = names.begin (); i != names.end (); ++i) + Config::const_iterator i; + for (i = context.config.begin (); i != context.config.end (); ++i) { - if (i->substr (0, 4) == "uda.") + if (i->first.substr (0, 4) == "uda.") { std::string::size_type period = 4; - if ((period = i->find ('.', period)) != std::string::npos) - udas[i->substr (4, period - 4)] = 0; + if ((period = i->first.find ('.', period)) != std::string::npos) + udas[i->first.substr (4, period - 4)] = 0; } } diff --git a/src/commands/CmdCalendar.cpp b/src/commands/CmdCalendar.cpp index b50a40666..88ca0d440 100644 --- a/src/commands/CmdCalendar.cpp +++ b/src/commands/CmdCalendar.cpp @@ -351,22 +351,19 @@ int CmdCalendar::execute (std::string& output) // Table with holiday information if (context.config.get ("calendar.holidays") == "full") { - std::vector holidays; - context.config.all (holidays); - ViewText holTable; holTable.width (context.getWidth ()); holTable.add (Column::factory ("string", STRING_CMD_CAL_LABEL_DATE)); holTable.add (Column::factory ("string", STRING_CMD_CAL_LABEL_HOL)); - std::vector ::iterator it; + Config::const_iterator it; std::map > hm; // we need to store multiple holidays per day - for (it = holidays.begin (); it != holidays.end (); ++it) - if (it->substr (0, 8) == "holiday.") - if (it->substr (it->size () - 4) == "name") + for (it = context.config.begin (); it != context.config.end (); ++it) + if (it->first.substr (0, 8) == "holiday.") + if (it->first.substr (it->first.size () - 4) == "name") { - std::string holName = context.config.get ("holiday." + it->substr (8, it->size () - 13) + ".name"); - std::string holDate = context.config.get ("holiday." + it->substr (8, it->size () - 13) + ".date"); + std::string holName = context.config.get ("holiday." + it->first.substr (8, it->first.size () - 13) + ".name"); + std::string holDate = context.config.get ("holiday." + it->first.substr (8, it->first.size () - 13) + ".date"); Date hDate (holDate.c_str (), context.config.get ("dateformat.holiday")); if (date_after < hDate && hDate < date_before) @@ -526,14 +523,12 @@ std::string CmdCalendar::renderMonths ( // colorize holidays if (context.config.get ("calendar.holidays") != "none") { - std::vector holidays; - context.config.all (holidays); - std::vector ::iterator hol; - for (hol = holidays.begin (); hol != holidays.end (); ++hol) - if (hol->substr (0, 8) == "holiday.") - if (hol->substr (hol->size () - 4) == "date") + Config::const_iterator hol; + for (hol = context.config.begin (); hol != context.config.end (); ++hol) + if (hol->first.substr (0, 8) == "holiday.") + if (hol->first.substr (hol->first.size () - 4) == "date") { - std::string value = context.config.get (*hol); + std::string value = hol->second; Date holDate (value.c_str (), context.config.get ("dateformat.holiday")); if (holDate.day () == d && holDate.month () == months[mpl] && diff --git a/src/commands/CmdColor.cpp b/src/commands/CmdColor.cpp index 874ae6297..1abe11591 100644 --- a/src/commands/CmdColor.cpp +++ b/src/commands/CmdColor.cpp @@ -69,27 +69,24 @@ int CmdColor::execute (std::string& output) { out << "\n" << STRING_CMD_COLOR_HERE << "\n"; - std::vector all; - context.config.all (all); - ViewText view; view.width (context.getWidth ()); view.add (Column::factory ("string", STRING_CMD_COLOR_COLOR)); view.add (Column::factory ("string", STRING_CMD_COLOR_DEFINITION)); - std::vector ::iterator item; - for (item = all.begin (); item != all.end (); ++item) + Config::const_iterator item; + for (item = context.config.begin (); item != context.config.end (); ++item) { // Skip items with 'color' in their name, that are not referring to // actual colors. - if (*item != "_forcecolor" && - *item != "color" && - item->find ("color") == 0) + if (item->first != "_forcecolor" && + item->first != "color" && + item->first.find ("color") == 0) { - Color color (context.config.get (*item)); + Color color (context.config.get (item->first)); int row = view.addRow (); - view.set (row, 0, *item, color); - view.set (row, 1, context.config.get (*item), color); + view.set (row, 0, item->first, color); + view.set (row, 1, item->second, color); } } diff --git a/src/commands/CmdReports.cpp b/src/commands/CmdReports.cpp index 6f548da19..a8c500934 100644 --- a/src/commands/CmdReports.cpp +++ b/src/commands/CmdReports.cpp @@ -51,15 +51,12 @@ int CmdReports::execute (std::string& output) std::vector reports; // Add custom reports. - std::vector vars; - context.config.all (vars); - - std::vector ::iterator i; - for (i = vars.begin (); i != vars.end (); ++i) + Config::const_iterator i; + for (i = context.config.begin (); i != context.config.end (); ++i) { - if (i->substr (0, 7) == "report.") + if (i->first.substr (0, 7) == "report.") { - std::string report = i->substr (7); + std::string report = i->first.substr (7); std::string::size_type columns = report.find (".columns"); if (columns != std::string::npos) reports.push_back (report.substr (0, columns)); diff --git a/src/commands/CmdShow.cpp b/src/commands/CmdShow.cpp index 0d8cc3225..c5fea0d99 100644 --- a/src/commands/CmdShow.cpp +++ b/src/commands/CmdShow.cpp @@ -214,38 +214,35 @@ int CmdShow::execute (std::string& output) // is redirected to a file, or stdout is not a tty. recognized += "_forcecolor "; - std::vector all; - context.config.all (all); - std::vector unrecognized; - std::vector ::iterator i; - for (i = all.begin (); i != all.end (); ++i) + Config::const_iterator i; + for (i = context.config.begin (); i != context.config.end (); ++i) { // Disallow partial matches by tacking a leading and trailing space on each // variable name. - std::string pattern = " " + *i + " "; + std::string pattern = " " + i->first + " "; if (recognized.find (pattern) == std::string::npos) { // These are special configuration variables, because their name is // dynamic. - if (i->substr (0, 14) != "color.keyword." && - i->substr (0, 14) != "color.project." && - i->substr (0, 10) != "color.tag." && - i->substr (0, 10) != "color.uda." && - i->substr (0, 8) != "holiday." && - i->substr (0, 7) != "report." && - i->substr (0, 6) != "alias." && - i->substr (0, 5) != "hook." && - i->substr (0, 5) != "push." && - i->substr (0, 5) != "pull." && - i->substr (0, 6) != "merge." && - i->substr (0, 4) != "uda." && - i->substr (0, 4) != "default." && - i->substr (0, 21) != "urgency.user.project." && - i->substr (0, 17) != "urgency.user.tag." && - i->substr (0, 12) != "urgency.uda.") + if (i->first.substr (0, 14) != "color.keyword." && + i->first.substr (0, 14) != "color.project." && + i->first.substr (0, 10) != "color.tag." && + i->first.substr (0, 10) != "color.uda." && + i->first.substr (0, 8) != "holiday." && + i->first.substr (0, 7) != "report." && + i->first.substr (0, 6) != "alias." && + i->first.substr (0, 5) != "hook." && + i->first.substr (0, 5) != "push." && + i->first.substr (0, 5) != "pull." && + i->first.substr (0, 6) != "merge." && + i->first.substr (0, 4) != "uda." && + i->first.substr (0, 4) != "default." && + i->first.substr (0, 21) != "urgency.user.project." && + i->first.substr (0, 17) != "urgency.user.tag." && + i->first.substr (0, 12) != "urgency.uda.") { - unrecognized.push_back (*i); + unrecognized.push_back (i->first); } } } @@ -255,9 +252,9 @@ int CmdShow::execute (std::string& output) Config default_config; default_config.setDefaults (); - for (i = all.begin (); i != all.end (); ++i) - if (context.config.get (*i) != default_config.get (*i)) - default_values.push_back (*i); + for (i = context.config.begin (); i != context.config.end (); ++i) + if (i->second != default_config.get (i->first)) + default_values.push_back (i->first); // Create output view. ViewText view; @@ -281,29 +278,29 @@ int CmdShow::execute (std::string& output) section = ""; std::string::size_type loc; - for (i = all.begin (); i != all.end (); ++i) + for (i = context.config.begin (); i != context.config.end (); ++i) { - loc = i->find (section, 0); + loc = i->first.find (section, 0); if (loc != std::string::npos) { // Look for unrecognized. Color color; - if (std::find (unrecognized.begin (), unrecognized.end (), *i) != unrecognized.end ()) + if (std::find (unrecognized.begin (), unrecognized.end (), i->first) != unrecognized.end ()) { issue_error = true; color = error; } - else if (std::find (default_values.begin (), default_values.end (), *i) != default_values.end ()) + else if (std::find (default_values.begin (), default_values.end (), i->first) != default_values.end ()) { issue_warning = true; color = warning; } - std::string value = context.config.get (*i); + std::string value = i->second; // hide sensible information - if ( (i->substr (0, 5) == "push." || - i->substr (0, 5) == "pull." || - i->substr (0, 6) == "merge.") && (i->find (".uri") != std::string::npos) ) { + if ( (i->first.substr (0, 5) == "push." || + i->first.substr (0, 5) == "pull." || + i->first.substr (0, 6) == "merge.") && (i->first.find (".uri") != std::string::npos) ) { Uri uri (value); uri.parse (); @@ -311,7 +308,7 @@ int CmdShow::execute (std::string& output) } int row = view.addRow (); - view.set (row, 0, *i, color); + view.set (row, 0, i->first, color); view.set (row, 1, value, color); } } @@ -336,6 +333,7 @@ int CmdShow::execute (std::string& output) { out << STRING_CMD_SHOW_UNREC << "\n"; + std::vector ::iterator i; for (i = unrecognized.begin (); i != unrecognized.end (); ++i) out << " " << *i << "\n"; @@ -381,7 +379,7 @@ int CmdShow::execute (std::string& output) // Verify installation. This is mentioned in the documentation as the way // to ensure everything is properly installed. - if (all.size () == 0) + if (context.config.size () == 0) { out << STRING_CMD_SHOW_EMPTY << "\n"; rc = 1; diff --git a/src/commands/CmdUDAs.cpp b/src/commands/CmdUDAs.cpp index 64fd10bef..14154e2a8 100644 --- a/src/commands/CmdUDAs.cpp +++ b/src/commands/CmdUDAs.cpp @@ -53,19 +53,16 @@ int CmdUDAs::execute (std::string& output) int rc = 0; std::stringstream out; - std::vector names; - context.config.all (names); - std::vector udas; - std::vector ::iterator name; - for (name = names.begin (); name != names.end (); ++name) + Config::const_iterator name; + for (name = context.config.begin (); name != context.config.end (); ++name) { - if (name->substr (0, 4) == "uda." && - name->find (".type") != std::string::npos) + if (name->first.substr (0, 4) == "uda." && + name->first.find (".type") != std::string::npos) { - std::string::size_type period = name->find ('.', 4); + std::string::size_type period = name->first.find ('.', 4); if (period != std::string::npos) - udas.push_back (name->substr (4, period - 4)); + udas.push_back (name->first.substr (4, period - 4)); } } @@ -182,19 +179,16 @@ CmdCompletionUDAs::CmdCompletionUDAs () //////////////////////////////////////////////////////////////////////////////// int CmdCompletionUDAs::execute (std::string& output) { - std::vector names; - context.config.all (names); - std::vector udas; - std::vector ::iterator name; - for (name = names.begin (); name != names.end (); ++name) + Config::const_iterator name; + for (name = context.config.begin (); name != context.config.end (); ++name) { - if (name->substr (0, 4) == "uda." && - name->find (".type") != std::string::npos) + if (name->first.substr (0, 4) == "uda." && + name->first.find (".type") != std::string::npos) { - std::string::size_type period = name->find ('.', 4); + std::string::size_type period = name->first.find ('.', 4); if (period != std::string::npos) - udas.push_back (name->substr (4, period - 4)); + udas.push_back (name->first.substr (4, period - 4)); } } diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index 2dfc97178..5f8ca16c6 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -168,16 +168,13 @@ void Command::factory (std::map & all) c = new CmdZshCompletionUuids (); all[c->keyword ()] = c; // Instantiate a command object for each custom report. - std::vector variables; - context.config.all (variables); - std::vector reports; - std::vector ::iterator i; - for (i = variables.begin (); i != variables.end (); ++i) + Config::const_iterator i; + for (i = context.config.begin (); i != context.config.end (); ++i) { - if (i->substr (0, 7) == "report.") + if (i->first.substr (0, 7) == "report.") { - std::string report = i->substr (7); + std::string report = i->first.substr (7); std::string::size_type columns = report.find (".columns"); if (columns != std::string::npos) reports.push_back (report.substr (0, columns)); diff --git a/src/rules.cpp b/src/rules.cpp index 093a73f4c..40185ef12 100644 --- a/src/rules.cpp +++ b/src/rules.cpp @@ -47,17 +47,15 @@ void initializeColorRules () // Load all the configuration values, filter to only the ones that begin with // "color.", then store name/value in gsColor, and name in rules. std::vector rules; - std::vector variables; - context.config.all (variables); - std::vector ::iterator v; - for (v = variables.begin (); v != variables.end (); ++v) + Config::const_iterator v; + for (v = context.config.begin (); v != context.config.end (); ++v) { - if (v->substr (0, 6) == "color.") + if (v->first.substr (0, 6) == "color.") { - Color c (context.config.get (*v)); - gsColor[*v] = c; + Color c (v->second); + gsColor[v->first] = c; - rules.push_back (*v); + rules.push_back (v->first); } }