refactor(treewide): reserve vector capacity when final size is known

In these trivial cases the final vector size (or lower bound on the size) is known,
so we can avoid some vector reallocations. This is not very important, but is just
good practice and general hygiene.
This commit is contained in:
Sergei Zimmerman 2024-11-09 22:35:18 +03:00
parent 492c678162
commit 0fe3b54ee1
6 changed files with 6 additions and 0 deletions

View file

@ -857,6 +857,7 @@ std::vector<FlakeRef> RawInstallablesCommand::getFlakeRefsForCompletion()
{
applyDefaultInstallables(rawInstallables);
std::vector<FlakeRef> res;
res.reserve(rawInstallables.size());
for (auto i : rawInstallables)
res.push_back(parseFlakeRefWithFragment(
fetchSettings,

View file

@ -91,6 +91,7 @@ StringMap EvalState::realiseContext(const NixStringContext & context, StorePathS
/* Build/substitute the context. */
std::vector<DerivedPath> buildReqs;
buildReqs.reserve(drvs.size());
for (auto & d : drvs) buildReqs.emplace_back(DerivedPath { d });
buildStore->buildPaths(buildReqs, bmNormal, store);

View file

@ -66,6 +66,7 @@ std::vector<KeyedBuildResult> Store::buildPathsWithResults(
worker.run(goals);
std::vector<KeyedBuildResult> results;
results.reserve(state.size());
for (auto & [req, goalPtr] : state)
results.emplace_back(KeyedBuildResult {

View file

@ -37,6 +37,7 @@ DerivedPath StorePathWithOutputs::toDerivedPath() const
std::vector<DerivedPath> toDerivedPaths(const std::vector<StorePathWithOutputs> ss)
{
std::vector<DerivedPath> reqs;
reqs.reserve(ss.size());
for (auto & s : ss) reqs.push_back(s.toDerivedPath());
return reqs;
}

View file

@ -56,6 +56,7 @@ ExecutablePath ExecutablePath::parse(const OsString & path)
OsString ExecutablePath::render() const
{
std::vector<PathViewNG> path2;
path2.reserve(directories.size());
for (auto & p : directories)
path2.push_back(p.native());
return basicConcatStringsSep(path_var_separator, path2);

View file

@ -610,6 +610,7 @@ struct CmdDevelop : Common, MixEnvironment
else if (!command.empty()) {
std::vector<std::string> args;
args.reserve(command.size());
for (auto s : command)
args.push_back(shellEscape(s));
script += fmt("exec %s\n", concatStringsSep(" ", args));