Reserve vector capacity at compile time where applicable & declare variables only when they're to be used

This commit is contained in:
Yonas Habteab 2022-08-26 16:42:43 +02:00
parent d8445ca41a
commit f94e2c44e7
14 changed files with 26 additions and 18 deletions

View File

@ -45,6 +45,7 @@ static HANDLE l_Job;
static std::vector<String> GetLogLevelCompletionSuggestions(const String& arg)
{
std::vector<String> result;
result.reserve(5);
String debugLevel = "debug";
if (debugLevel.Find(arg) == 0)

View File

@ -212,8 +212,6 @@ void ConfigObject::RestoreAttribute(const String& attr, bool updateVersion)
int fid = type->GetFieldId(fieldName);
Value currentValue = GetField(fid);
Dictionary::Ptr original_attributes = GetOriginalAttributes();
if (!original_attributes)
@ -223,6 +221,7 @@ void ConfigObject::RestoreAttribute(const String& attr, bool updateVersion)
Value newValue;
if (tokens.size() > 1) {
Value currentValue = GetField(fid);
newValue = currentValue.Clone();
Value current = newValue;

View File

@ -685,10 +685,8 @@ std::shared_ptr<X509> CreateCert(EVP_PKEY *pubkey, X509_NAME *subject, X509_NAME
X509_EXTENSION_free(basicConstraintsExt);
}
String cn = GetX509NameCN(subject);
if (!ca) {
String san = "DNS:" + cn;
String san = "DNS:" + GetX509NameCN(subject);
X509_EXTENSION *subjectAltNameExt = X509V3_EXT_conf_nid(nullptr, &ctx, NID_subject_alt_name, const_cast<char *>(san.CStr()));
if (subjectAltNameExt) {
X509_add_ext(cert, subjectAltNameExt, -1);

View File

@ -1015,6 +1015,8 @@ String Utility::FormatDuration(double duration)
std::vector<String> tokens;
String result;
tokens.reserve(4);
if (duration >= 86400) {
int days = duration / 86400;
tokens.emplace_back(Convert::ToString(days) + (days != 1 ? " days" : " day"));
@ -1038,7 +1040,7 @@ String Utility::FormatDuration(double duration)
tokens.emplace_back(Convert::ToString(seconds) + (seconds != 1 ? " seconds" : " second"));
}
if (tokens.size() == 0) {
if (tokens.empty()) {
int milliseconds = std::floor(duration * 1000);
if (milliseconds >= 1)
tokens.emplace_back(Convert::ToString(milliseconds) + (milliseconds != 1 ? " milliseconds" : " millisecond"));
@ -1570,7 +1572,7 @@ static bool ReleaseHelper(String *platformName, String *platformVersion)
if (release.is_open()) {
std::string release_line;
while (getline(release, release_line)) {
std::string::size_type pos = release_line.find("=");
std::string::size_type pos = release_line.find('=');
if (pos == std::string::npos)
continue;
@ -1578,12 +1580,12 @@ static bool ReleaseHelper(String *platformName, String *platformVersion)
std::string key = release_line.substr(0, pos);
std::string value = release_line.substr(pos + 1);
std::string::size_type firstQuote = value.find("\"");
std::string::size_type firstQuote = value.find('"');
if (firstQuote != std::string::npos)
value.erase(0, firstQuote + 1);
std::string::size_type lastQuote = value.rfind("\"");
std::string::size_type lastQuote = value.rfind('"');
if (lastQuote != std::string::npos)
value.erase(lastQuote);

View File

@ -52,6 +52,7 @@ void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult
tie(host, service) = GetHostService(checkable);
MacroProcessor::ResolverList resolvers;
resolvers.reserve(5);
if (MacroResolver::OverrideMacros)
resolvers.emplace_back("override", MacroResolver::OverrideMacros);

View File

@ -654,6 +654,8 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, cons
MacroProcessor::ResolverList resolvers;
Value macros;
resolvers.reserve(4);
if (params->Contains("macros")) {
macros = HttpUtility::GetLastParameter(params, "macros");
if (macros.IsObjectType<Dictionary>()) {

View File

@ -69,14 +69,13 @@ String CompatUtility::GetCheckableCommandArgs(const Checkable::Ptr& checkable)
{
CheckCommand::Ptr command = checkable->GetCheckCommand();
Dictionary::Ptr args = new Dictionary();
if (command) {
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
String command_line = GetCommandLine(command);
Dictionary::Ptr args = new Dictionary();
Dictionary::Ptr command_vars = command->GetVars();
if (command_vars) {

View File

@ -54,6 +54,8 @@ void IcingadbCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckR
MacroProcessor::ResolverList resolvers;
String silenceMissingMacroWarning;
resolvers.reserve(5);
if (MacroResolver::OverrideMacros)
resolvers.emplace_back("override", MacroResolver::OverrideMacros);
@ -211,7 +213,7 @@ void IcingadbCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckR
critmsgs << " ERROR: " << errMsg << "!";
}
perfdata->Add(new PerfdataValue("error_for", errFor * (err ? 1 : -1), false, "seconds", Empty, errForCritical, 0));
perfdata->Add(new PerfdataValue("error_for", errFor, false, "seconds", Empty, errForCritical, 0));
}
if (!down) {

View File

@ -55,6 +55,7 @@ void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Che
tie(host, service) = GetHostService(checkable);
MacroProcessor::ResolverList resolvers;
resolvers.reserve(5);
if (MacroResolver::OverrideMacros)
resolvers.emplace_back("override", MacroResolver::OverrideMacros);

View File

@ -29,6 +29,7 @@ void DummyCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
tie(host, service) = GetHostService(checkable);
MacroProcessor::ResolverList resolvers;
resolvers.reserve(5);
if (MacroResolver::OverrideMacros)
resolvers.emplace_back("override", MacroResolver::OverrideMacros);
@ -48,9 +49,6 @@ void DummyCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
if (resolvedMacros && !useResolvedMacros)
return;
/* Parse output and performance data. */
std::pair<String, String> co = PluginUtility::ParseCheckOutput(dummyText);
double now = Utility::GetTime();
String commandName = command->GetName();
@ -64,6 +62,9 @@ void DummyCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
Checkable::ExecuteCommandProcessFinishedHandler(commandName, pr);
} else {
/* Parse output and performance data. */
std::pair<String, String> co = PluginUtility::ParseCheckOutput(dummyText);
cr->SetOutput(co.first);
cr->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
cr->SetState(PluginUtility::ExitStatusToState(dummyState));

View File

@ -36,6 +36,6 @@ void ExceptionCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const Check
Checkable::ExecuteCommandProcessFinishedHandler("", pr);
} else {
BOOST_THROW_EXCEPTION(ScriptError("Test") << boost::errinfo_api_function("Test"));
BOOST_THROW_EXCEPTION(scriptError);
}
}

View File

@ -33,6 +33,7 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes
tie(host, service) = GetHostService(checkable);
MacroProcessor::ResolverList resolvers;
resolvers.reserve(5);
if (MacroResolver::OverrideMacros)
resolvers.emplace_back("override", MacroResolver::OverrideMacros);

View File

@ -47,7 +47,6 @@ void SleepCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
double now = Utility::GetTime();
CheckCommand::Ptr command = checkable->GetCheckCommand();
String commandName = command->GetName();
if (Checkable::ExecuteCommandProcessFinishedHandler) {
ProcessResult pr;
@ -62,7 +61,7 @@ void SleepCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResu
cr->SetOutput(output);
cr->SetExecutionStart(now);
cr->SetExecutionEnd(now);
cr->SetCommand(commandName);
cr->SetCommand(command->GetName());
checkable->ProcessCheckResult(cr);
}

View File

@ -113,6 +113,8 @@ void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C
host = static_pointer_cast<Host>(checkable);
MacroProcessor::ResolverList resolvers;
resolvers.reserve(3);
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);