From 0fe3b54ee180975f69b5c2fde658c4b7691384ac Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman <145775305+xokdvium@users.noreply.github.com> Date: Sat, 9 Nov 2024 22:35:18 +0300 Subject: [PATCH] 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. --- src/libcmd/installables.cc | 1 + src/libexpr/primops.cc | 1 + src/libstore/build/entry-points.cc | 1 + src/libstore/path-with-outputs.cc | 1 + src/libutil/executable-path.cc | 1 + src/nix/develop.cc | 1 + 6 files changed, 6 insertions(+) diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc index f9d6d8ce8..227bb64ed 100644 --- a/src/libcmd/installables.cc +++ b/src/libcmd/installables.cc @@ -857,6 +857,7 @@ std::vector RawInstallablesCommand::getFlakeRefsForCompletion() { applyDefaultInstallables(rawInstallables); std::vector res; + res.reserve(rawInstallables.size()); for (auto i : rawInstallables) res.push_back(parseFlakeRefWithFragment( fetchSettings, diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 84aa6bac9..45d9f86ac 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -91,6 +91,7 @@ StringMap EvalState::realiseContext(const NixStringContext & context, StorePathS /* Build/substitute the context. */ std::vector buildReqs; + buildReqs.reserve(drvs.size()); for (auto & d : drvs) buildReqs.emplace_back(DerivedPath { d }); buildStore->buildPaths(buildReqs, bmNormal, store); diff --git a/src/libstore/build/entry-points.cc b/src/libstore/build/entry-points.cc index 4c1373bfa..3bf22320e 100644 --- a/src/libstore/build/entry-points.cc +++ b/src/libstore/build/entry-points.cc @@ -66,6 +66,7 @@ std::vector Store::buildPathsWithResults( worker.run(goals); std::vector results; + results.reserve(state.size()); for (auto & [req, goalPtr] : state) results.emplace_back(KeyedBuildResult { diff --git a/src/libstore/path-with-outputs.cc b/src/libstore/path-with-outputs.cc index 161d023d1..e526b1ff6 100644 --- a/src/libstore/path-with-outputs.cc +++ b/src/libstore/path-with-outputs.cc @@ -37,6 +37,7 @@ DerivedPath StorePathWithOutputs::toDerivedPath() const std::vector toDerivedPaths(const std::vector ss) { std::vector reqs; + reqs.reserve(ss.size()); for (auto & s : ss) reqs.push_back(s.toDerivedPath()); return reqs; } diff --git a/src/libutil/executable-path.cc b/src/libutil/executable-path.cc index 9fb5214b2..e75e147b5 100644 --- a/src/libutil/executable-path.cc +++ b/src/libutil/executable-path.cc @@ -56,6 +56,7 @@ ExecutablePath ExecutablePath::parse(const OsString & path) OsString ExecutablePath::render() const { std::vector path2; + path2.reserve(directories.size()); for (auto & p : directories) path2.push_back(p.native()); return basicConcatStringsSep(path_var_separator, path2); diff --git a/src/nix/develop.cc b/src/nix/develop.cc index c7a733025..9a95bc695 100644 --- a/src/nix/develop.cc +++ b/src/nix/develop.cc @@ -610,6 +610,7 @@ struct CmdDevelop : Common, MixEnvironment else if (!command.empty()) { std::vector args; + args.reserve(command.size()); for (auto s : command) args.push_back(shellEscape(s)); script += fmt("exec %s\n", concatStringsSep(" ", args));